UNPKG

tdesign-mobile-vue

Version:
248 lines (244 loc) 9.12 kB
/** * tdesign v1.7.0 * (c) 2024 TDesign Group * @license MIT */ import _defineProperty from '@babel/runtime/helpers/defineProperty'; import _slicedToArray from '@babel/runtime/helpers/slicedToArray'; import { defineComponent, ref, computed, createVNode, mergeProps, h, nextTick } from 'vue'; import { SearchIcon, CloseCircleFilledIcon } from 'tdesign-icons-vue-next'; import { useFocus } from '@vueuse/core'; import config from '../config.js'; import { preventDefault } from '../shared/dom.js'; import searchProps from './props.js'; import { useDefault } from '../shared/useDefault/index.js'; import { usePrefixClass } from '../hooks/useClass.js'; import { useTNodeJSX } from '../hooks/tnode.js'; import useLengthLimit from '../hooks/useLengthLimit.js'; import { ENTER_REG } from '../_common/js/common.js'; import _Cell from '../cell/cell.js'; import 'lodash/isFunction'; import 'lodash/isString'; import 'lodash/camelCase'; import '../config-provider/useConfig.js'; import 'lodash/cloneDeep'; import '../config-provider/context.js'; import 'lodash/mergeWith'; import 'lodash/merge'; import 'lodash/isArray'; import '../_common/js/global-config/mobile/default-config.js'; import '../_common/js/global-config/mobile/locale/zh_CN.js'; import '../_chunks/dep-d5364bc4.js'; import '@babel/runtime/helpers/typeof'; import '../_chunks/dep-eb734424.js'; import 'dayjs'; import 'lodash/kebabCase'; import '../hooks/render-tnode.js'; import 'lodash/isEmpty'; import 'lodash/isObject'; import 'lodash/isNumber'; import '../_common/js/log/log.js'; import '../_common/js/utils/helper.js'; import '@babel/runtime/helpers/toConsumableArray'; import '@babel/runtime/helpers/objectWithoutProperties'; import 'lodash/isNull'; import 'lodash/isUndefined'; import '../cell/props.js'; import '../form/hooks.js'; import 'lodash/isBoolean'; import '../shared/hover.js'; var prefix = config.prefix; var _Search = defineComponent({ name: "".concat(prefix, "-search"), props: searchProps, setup: function setup(props, context) { var renderTNodeJSX = useTNodeJSX(); var searchClass = usePrefixClass("search"); var isShowResultList = ref(false); var isShowAction = ref(false); var inputRef = ref(); var _useFocus = useFocus(inputRef, { initialValue: props.focus }), focused = _useFocus.focused; var _useDefault = useDefault(props, context.emit, "value", "change"), _useDefault2 = _slicedToArray(_useDefault, 1), searchValue = _useDefault2[0]; var boxClasses = computed(function () { return ["".concat(searchClass.value, "__input-box"), "".concat(searchClass.value, "__input-box--").concat(props.shape), _defineProperty({}, "".concat(prefix, "-is-focused"), focused.value)]; }); var inputClasses = computed(function () { return ["".concat(prefix, "-input__keyword"), _defineProperty({}, "".concat(searchClass.value, "--center"), props.center)]; }); var limitParams = computed(function () { return { value: [void 0, null].includes(searchValue.value) ? void 0 : String(searchValue.value), maxlength: Number(props.maxlength), maxcharacter: props.maxcharacter, allowInputOverMax: false }; }); var _useLengthLimit = useLengthLimit(limitParams), getValueByLimitNumber = _useLengthLimit.getValueByLimitNumber; var setInputValue = function setInputValue(v) { var input = inputRef.value; var sV = String(v); if (!input) { return; } if (input.value !== sV) { input.value = sV; } }; var inputValueChangeHandle = function inputValueChangeHandle(e) { var value = e.target.value; searchValue.value = getValueByLimitNumber(value); nextTick(function () { return setInputValue(searchValue.value); }); }; var handleInput = function handleInput(e) { isShowResultList.value = true; if (e instanceof InputEvent) { var checkInputType = e.inputType && e.inputType === "insertCompositionText"; if (e.isComposing || checkInputType) return; } inputValueChangeHandle(e); }; var handleClear = function handleClear(e) { var _props$onClear; searchValue.value = ""; focused.value = true; (_props$onClear = props.onClear) === null || _props$onClear === void 0 || _props$onClear.call(props, { e: e }); }; var handleFocus = function handleFocus(e) { var _props$onFocus; isShowAction.value = true; (_props$onFocus = props.onFocus) === null || _props$onFocus === void 0 || _props$onFocus.call(props, { value: searchValue.value, e: e }); }; var handleBlur = function handleBlur(e) { var _props$onBlur; isShowAction.value = false; (_props$onBlur = props.onBlur) === null || _props$onBlur === void 0 || _props$onBlur.call(props, { value: searchValue.value, e: e }); }; var handleCompositionend = function handleCompositionend(e) { inputValueChangeHandle(e); }; var handleAction = function handleAction(e) { var _props$onActionClick; (_props$onActionClick = props.onActionClick) === null || _props$onActionClick === void 0 || _props$onActionClick.call(props, { e: e }); }; var handleSearch = function handleSearch(e) { if (ENTER_REG.test(e.code) || ENTER_REG.test(e.key)) { var _inputRef$value, _props$onSubmit; preventDefault(e, false); (_inputRef$value = inputRef.value) === null || _inputRef$value === void 0 || _inputRef$value.blur(); (_props$onSubmit = props.onSubmit) === null || _props$onSubmit === void 0 || _props$onSubmit.call(props, { value: searchValue.value, e: e }); } }; return function () { var readerLeftIcon = function readerLeftIcon() { var leftIcon = renderTNodeJSX("leftIcon"); if (leftIcon === "search") { return createVNode(SearchIcon, { "size": "24px" }, null); } return renderTNodeJSX("leftIcon"); }; var readerClear = function readerClear() { if (props.clearable && searchValue.value) { return createVNode("div", { "class": "".concat(searchClass.value, "__clear"), "onClick": handleClear }, [createVNode(CloseCircleFilledIcon, { "size": "24" }, null)]); } return null; }; var readerAction = function readerAction() { var action = renderTNodeJSX("action"); if (action && isShowAction.value) { return createVNode("div", { "class": "".concat(searchClass.value, "__search-action ").concat(prefix, "-class-action"), "onClick": handleAction }, [action]); } return null; }; var onSelectResultItem = function onSelectResultItem(params) { isShowResultList.value = false; searchValue.value = params.item; }; var highlightSearchValue = function highlightSearchValue(item, searchValue2) { var parts = item.split(new RegExp("(".concat(searchValue2, ")"), "gi")); return parts.map(function (part, index) { return part.toLowerCase() === searchValue2.toLowerCase() ? createVNode("span", { "key": index, "class": "".concat(searchClass.value, "__result-item--highLight") }, [part]) : part; }); }; var listNodes = function listNodes(params) { return h(_Cell, { key: params.index, class: "".concat(searchClass.value, "__result-item"), onClick: function onClick() { return onSelectResultItem({ item: params.item }); } }, { title: function title() { return highlightSearchValue(params.item, searchValue.value); } }); }; var extraProps = { enterkeyhint: "search" }; return createVNode("div", null, [createVNode("div", { "class": "".concat(searchClass.value) }, [createVNode("div", { "class": boxClasses.value }, [readerLeftIcon(), createVNode("input", mergeProps({ "ref": inputRef, "value": searchValue.value, "type": "search", "class": inputClasses.value, "autofocus": props.focus, "placeholder": props.placeholder, "readonly": props.readonly, "disabled": props.disabled, "onKeypress": handleSearch, "onFocus": handleFocus, "onBlur": handleBlur, "onInput": handleInput, "onCompositionend": handleCompositionend }, extraProps), null), readerClear()]), readerAction()]), isShowResultList.value && createVNode("div", { "class": "".concat(searchClass.value, "__result-list") }, [props.resultList.map(function (item, index) { return listNodes({ item: item, index: index }); })])]); }; } }); export { _Search as default }; //# sourceMappingURL=search.js.map