Search.mjs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { mergeProps as _mergeProps, createVNode as _createVNode } from "vue";
  2. import { ref, defineComponent } from "vue";
  3. import { pick, extend, truthProp, preventDefault, makeStringProp, createNamespace } from "../utils/index.mjs";
  4. import { fieldSharedProps } from "../field/Field.mjs";
  5. import { useId } from "../composables/use-id.mjs";
  6. import { useExpose } from "../composables/use-expose.mjs";
  7. import { Field } from "../field/index.mjs";
  8. const [name, bem, t] = createNamespace("search");
  9. const searchProps = extend({}, fieldSharedProps, {
  10. label: String,
  11. shape: makeStringProp("square"),
  12. leftIcon: makeStringProp("search"),
  13. clearable: truthProp,
  14. actionText: String,
  15. background: String,
  16. showAction: Boolean
  17. });
  18. var stdin_default = defineComponent({
  19. name,
  20. props: searchProps,
  21. emits: ["blur", "focus", "clear", "search", "cancel", "click-input", "click-left-icon", "click-right-icon", "update:modelValue"],
  22. setup(props, {
  23. emit,
  24. slots,
  25. attrs
  26. }) {
  27. const id = useId();
  28. const filedRef = ref();
  29. const onCancel = () => {
  30. if (!slots.action) {
  31. emit("update:modelValue", "");
  32. emit("cancel");
  33. }
  34. };
  35. const onKeypress = (event) => {
  36. const ENTER_CODE = 13;
  37. if (event.keyCode === ENTER_CODE) {
  38. preventDefault(event);
  39. emit("search", props.modelValue);
  40. }
  41. };
  42. const getInputId = () => props.id || `${id}-input`;
  43. const renderLabel = () => {
  44. if (slots.label || props.label) {
  45. return _createVNode("label", {
  46. "class": bem("label"),
  47. "for": getInputId()
  48. }, [slots.label ? slots.label() : props.label]);
  49. }
  50. };
  51. const renderAction = () => {
  52. if (props.showAction) {
  53. const text = props.actionText || t("cancel");
  54. return _createVNode("div", {
  55. "class": bem("action"),
  56. "role": "button",
  57. "tabindex": 0,
  58. "onClick": onCancel
  59. }, [slots.action ? slots.action() : text]);
  60. }
  61. };
  62. const blur = () => {
  63. var _a;
  64. return (_a = filedRef.value) == null ? void 0 : _a.blur();
  65. };
  66. const focus = () => {
  67. var _a;
  68. return (_a = filedRef.value) == null ? void 0 : _a.focus();
  69. };
  70. const onBlur = (event) => emit("blur", event);
  71. const onFocus = (event) => emit("focus", event);
  72. const onClear = (event) => emit("clear", event);
  73. const onClickInput = (event) => emit("click-input", event);
  74. const onClickLeftIcon = (event) => emit("click-left-icon", event);
  75. const onClickRightIcon = (event) => emit("click-right-icon", event);
  76. const fieldPropNames = Object.keys(fieldSharedProps);
  77. const renderField = () => {
  78. const fieldAttrs = extend({}, attrs, pick(props, fieldPropNames), {
  79. id: getInputId()
  80. });
  81. const onInput = (value) => emit("update:modelValue", value);
  82. return _createVNode(Field, _mergeProps({
  83. "ref": filedRef,
  84. "type": "search",
  85. "class": bem("field"),
  86. "border": false,
  87. "onBlur": onBlur,
  88. "onFocus": onFocus,
  89. "onClear": onClear,
  90. "onKeypress": onKeypress,
  91. "onClick-input": onClickInput,
  92. "onClick-left-icon": onClickLeftIcon,
  93. "onClick-right-icon": onClickRightIcon,
  94. "onUpdate:modelValue": onInput
  95. }, fieldAttrs), pick(slots, ["left-icon", "right-icon"]));
  96. };
  97. useExpose({
  98. focus,
  99. blur
  100. });
  101. return () => {
  102. var _a;
  103. return _createVNode("div", {
  104. "class": bem({
  105. "show-action": props.showAction
  106. }),
  107. "style": {
  108. background: props.background
  109. }
  110. }, [(_a = slots.left) == null ? void 0 : _a.call(slots), _createVNode("div", {
  111. "class": bem("content", props.shape)
  112. }, [renderLabel(), renderField()]), renderAction()]);
  113. };
  114. }
  115. });
  116. export {
  117. stdin_default as default
  118. };