Search.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { ref, defineComponent, createVNode as _createVNode, mergeProps as _mergeProps } from "vue";
  2. import { pick, extend, truthProp, preventDefault, makeStringProp, createNamespace } from "../utils/index.mjs";
  3. import { fieldSharedProps } from "../field/Field.mjs";
  4. import { useId } from "../composables/use-id.mjs";
  5. import { useExpose } from "../composables/use-expose.mjs";
  6. import { Field } from "../field/index.mjs";
  7. const [name, bem, t] = createNamespace("search");
  8. const searchProps = extend({}, fieldSharedProps, {
  9. label: String,
  10. shape: makeStringProp("square"),
  11. leftIcon: makeStringProp("search"),
  12. clearable: truthProp,
  13. actionText: String,
  14. background: String,
  15. showAction: Boolean
  16. });
  17. var stdin_default = defineComponent({
  18. name,
  19. props: searchProps,
  20. emits: ["blur", "focus", "clear", "search", "cancel", "clickInput", "clickLeftIcon", "clickRightIcon", "update:modelValue"],
  21. setup(props, {
  22. emit,
  23. slots,
  24. attrs
  25. }) {
  26. const id = useId();
  27. const fieldRef = ref();
  28. const onCancel = () => {
  29. if (!slots.action) {
  30. emit("update:modelValue", "");
  31. emit("cancel");
  32. }
  33. };
  34. const onKeypress = (event) => {
  35. const ENTER_CODE = 13;
  36. if (event.keyCode === ENTER_CODE) {
  37. preventDefault(event);
  38. emit("search", props.modelValue);
  39. }
  40. };
  41. const getInputId = () => props.id || `${id}-input`;
  42. const renderLabel = () => {
  43. if (slots.label || props.label) {
  44. return _createVNode("label", {
  45. "class": bem("label"),
  46. "for": getInputId(),
  47. "data-allow-mismatch": "attribute"
  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 = fieldRef.value) == null ? void 0 : _a.blur();
  65. };
  66. const focus = () => {
  67. var _a;
  68. return (_a = fieldRef.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("clickInput", event);
  74. const onClickLeftIcon = (event) => emit("clickLeftIcon", event);
  75. const onClickRightIcon = (event) => emit("clickRightIcon", 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": fieldRef,
  84. "type": "search",
  85. "class": bem("field", {
  86. "with-message": fieldAttrs.errorMessage
  87. }),
  88. "border": false,
  89. "onBlur": onBlur,
  90. "onFocus": onFocus,
  91. "onClear": onClear,
  92. "onKeypress": onKeypress,
  93. "onClickInput": onClickInput,
  94. "onClickLeftIcon": onClickLeftIcon,
  95. "onClickRightIcon": onClickRightIcon,
  96. "onUpdate:modelValue": onInput
  97. }, fieldAttrs), pick(slots, ["left-icon", "right-icon"]));
  98. };
  99. useExpose({
  100. focus,
  101. blur
  102. });
  103. return () => {
  104. var _a;
  105. return _createVNode("div", {
  106. "class": bem({
  107. "show-action": props.showAction
  108. }),
  109. "style": {
  110. background: props.background
  111. }
  112. }, [(_a = slots.left) == null ? void 0 : _a.call(slots), _createVNode("div", {
  113. "class": bem("content", props.shape)
  114. }, [renderLabel(), renderField()]), renderAction()]);
  115. };
  116. }
  117. });
  118. export {
  119. stdin_default as default,
  120. searchProps
  121. };