UNPKG

@fesjs/fes-design

Version:
427 lines (422 loc) 17.3 kB
import _defineProperty from '@babel/runtime/helpers/esm/defineProperty'; import { defineComponent, ref, computed, watch, nextTick, resolveComponent, openBlock, createElementBlock, normalizeClass, createElementVNode, Fragment, createBlock, withCtx, createVNode, toDisplayString, createCommentVNode, renderList, createTextVNode, normalizeStyle, withModifiers, withDirectives, vShow } from 'vue'; import { isEqual, isNil } from 'lodash-es'; import getPrefixCls from '../_util/getPrefixCls'; import { useTheme } from '../_theme/useTheme'; import Ellipsis from '../ellipsis'; import Tooltip from '../tooltip'; import Tag from '../tag'; import Scrollbar from '../scrollbar'; import UpOutlined from '../icon/UpOutlined'; import DownOutlined from '../icon/DownOutlined'; import CloseCircleFilled from '../icon/CloseCircleFilled'; import RenderTag from './renderTag'; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } const prefixCls = getPrefixCls('select-trigger'); const selectTriggerProps = { selectedOptions: { type: Array, default() { return []; } }, disabled: Boolean, clearable: Boolean, isOpened: Boolean, multiple: Boolean, filterable: Boolean, placeholder: String, collapseTags: Boolean, collapseTagsLimit: Number, renderTag: Function, tagBordered: { type: Boolean, default: false } }; var script = defineComponent({ name: 'FSelect', components: { Tag, Scrollbar, Ellipsis, Tooltip, UpOutlined, DownOutlined, CloseCircleFilled, RenderTag }, props: selectTriggerProps, emits: ['remove', 'clear', 'focus', 'blur', 'input'], setup(props, _ref) { let { emit } = _ref; useTheme(); const inputHoveringRef = ref(false); const inputRef = ref(); const filterTextRef = ref(''); const isComposingRef = ref(false); const compositionValueRef = ref(''); const isFocusRef = ref(false); const unSelectedRef = computed(() => props.selectedOptions.length === 0); const hasClearRef = computed(() => !props.disabled && props.clearable && !unSelectedRef.value && inputHoveringRef.value); const inputWidthRef = computed(() => { const totalText = filterTextRef.value + compositionValueRef.value; return totalText.length ? `${totalText.length + 1}em` : '1em'; }); const triggerClass = computed(() => ({ [`${prefixCls}`]: true, 'is-active': props.isOpened || isFocusRef.value, 'is-disabled': props.disabled === true, 'is-multiple': props.multiple })); const genTag = option => { const { label, value } = option; let tagLabel = ''; if (!isNil(label)) { tagLabel = label; } else if (!isNil(value)) { if (typeof value === 'string' || typeof value === 'number') { tagLabel = value; } else { tagLabel = value.toString(); } } return _objectSpread(_objectSpread({}, option), {}, { label: tagLabel, closable: !props.disabled }); }; const labelTextRef = computed(() => { const options = props.selectedOptions; if (options.length) { var _ref2, _options$0$label; return `${(_ref2 = (_options$0$label = options[0].label) !== null && _options$0$label !== void 0 ? _options$0$label : options[0].value) !== null && _ref2 !== void 0 ? _ref2 : ''}`; } return ''; }); const multiLabelRef = computed(() => { const options = props.selectedOptions; const tags = []; if (props.collapseTags) { const showOptions = options.slice(0, props.collapseTagsLimit); const rest = options.slice(props.collapseTagsLimit); const restCount = rest.length; showOptions.forEach(option => tags.push(genTag(option))); if (restCount > 0) { tags.push({ isCollapsed: true, collapsedOptions: rest.map(genTag), value: null, label: `+ ${restCount}`, closable: false }); } } else { options.forEach(option => tags.push(genTag(option))); } return tags; }); const calcCollapseTagIndex = collapseTagIndex => collapseTagIndex + props.collapseTagsLimit; const hasTagBordered = computed(() => { return props.disabled || props.tagBordered; }); const handleFocus = event => { if (props.disabled) { return; } isFocusRef.value = true; emit('focus', event); }; const handleBlur = event => { if (props.disabled) { return; } isFocusRef.value = false; if (filterTextRef.value) { filterTextRef.value = ''; emit('input', filterTextRef.value, { isClear: true }); } emit('blur', event); }; const handleRemove = index => { if (props.disabled) { return; } emit('remove', props.selectedOptions[index].value); }; const handleClear = () => { if (props.disabled) { return; } emit('clear'); }; const handleInput = e => { if (e instanceof InputEvent && !e.isComposing) { isComposingRef.value = false; } if (props.disabled || isComposingRef.value) { return; } filterTextRef.value = e.target.value; emit('input', filterTextRef.value); }; const handleCompositionStart = () => { isComposingRef.value = true; compositionValueRef.value = ''; }; const handleCompositionUpdate = event => { if (isComposingRef.value) { compositionValueRef.value = event.target.value; } }; const handleCompositionEnd = event => { if (isComposingRef.value) { compositionValueRef.value = ''; isComposingRef.value = false; handleInput(event); } }; const handleMouseDown = e => { if (props.filterable && e.target !== inputRef.value) { e.preventDefault(); } }; watch(() => props.isOpened, isOpened => { if (isOpened) { nextTick(() => { if (!inputRef.value) { return; } if (!props.filterable) { return; } inputRef.value.focus(); }); } }); watch(() => props.selectedOptions, (val, oldVal) => { if (!isEqual(val, oldVal)) { filterTextRef.value = ''; } }, { deep: true }); return { prefixCls, inputHoveringRef, hasClearRef, triggerClass, unSelectedRef, handleRemove, handleClear, handleFocus, handleBlur, inputRef, filterTextRef, handleCompositionStart, handleCompositionUpdate, handleCompositionEnd, handleInput, labelTextRef, multiLabelRef, calcCollapseTagIndex, handleMouseDown, isComposingRef, compositionValueRef, inputWidthRef, hasTagBordered }; } }); const _hoisted_1 = ["value", "placeholder", "disabled"]; const _hoisted_2 = ["value", "disabled"]; function render(_ctx, _cache, $props, $setup, $data, $options) { const _component_Ellipsis = resolveComponent("Ellipsis"); const _component_RenderTag = resolveComponent("RenderTag"); const _component_Tag = resolveComponent("Tag"); const _component_Scrollbar = resolveComponent("Scrollbar"); const _component_Tooltip = resolveComponent("Tooltip"); const _component_UpOutlined = resolveComponent("UpOutlined"); const _component_DownOutlined = resolveComponent("DownOutlined"); const _component_CloseCircleFilled = resolveComponent("CloseCircleFilled"); return openBlock(), createElementBlock("div", { tabindex: "0", class: normalizeClass(_ctx.triggerClass), onMouseenter: _cache[9] || (_cache[9] = $event => _ctx.inputHoveringRef = true), onMouseleave: _cache[10] || (_cache[10] = $event => _ctx.inputHoveringRef = false), onFocusin: _cache[11] || (_cache[11] = function () { return _ctx.handleFocus && _ctx.handleFocus(...arguments); }), onFocusout: _cache[12] || (_cache[12] = function () { return _ctx.handleBlur && _ctx.handleBlur(...arguments); }), onMousedown: _cache[13] || (_cache[13] = function () { return _ctx.handleMouseDown && _ctx.handleMouseDown(...arguments); }) }, [createElementVNode("div", { class: normalizeClass([`${_ctx.prefixCls}-label`, _ctx.multiple && 'is-multiple', _ctx.unSelectedRef && 'is-selected-null']) }, [!_ctx.multiple ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [!_ctx.filterable ? (openBlock(), createElementBlock(Fragment, { key: 0 }, [!_ctx.unSelectedRef ? (openBlock(), createBlock(_component_RenderTag, { key: 0, renderTag: _ctx.renderTag, option: _ctx.selectedOptions[0] }, { default: withCtx(() => [createVNode(_component_Ellipsis, { class: normalizeClass(`${_ctx.prefixCls}-label-text`), content: _ctx.labelTextRef }, null, 8 /* PROPS */, ["class", "content"])]), _: 1 /* STABLE */ }, 8 /* PROPS */, ["renderTag", "option"])) : (openBlock(), createElementBlock("div", { key: 1, class: normalizeClass(`${_ctx.prefixCls}-label-placeholder`) }, toDisplayString(_ctx.placeholder), 3 /* TEXT, CLASS */))], 64 /* STABLE_FRAGMENT */)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [createElementVNode("input", { ref: "inputRef", value: _ctx.filterTextRef, placeholder: _ctx.isOpened || _ctx.unSelectedRef ? _ctx.labelTextRef || _ctx.placeholder : '', class: normalizeClass(`${_ctx.prefixCls}-label-input`), disabled: _ctx.disabled, onInput: _cache[0] || (_cache[0] = function () { return _ctx.handleInput && _ctx.handleInput(...arguments); }), onCompositionstart: _cache[1] || (_cache[1] = function () { return _ctx.handleCompositionStart && _ctx.handleCompositionStart(...arguments); }), onCompositionupdate: _cache[2] || (_cache[2] = function () { return _ctx.handleCompositionUpdate && _ctx.handleCompositionUpdate(...arguments); }), onCompositionend: _cache[3] || (_cache[3] = function () { return _ctx.handleCompositionEnd && _ctx.handleCompositionEnd(...arguments); }) }, null, 42 /* CLASS, PROPS, NEED_HYDRATION */, _hoisted_1), !(_ctx.unSelectedRef || _ctx.isOpened) ? (openBlock(), createElementBlock("div", { key: 0, class: normalizeClass(`${_ctx.prefixCls}-label-overlay`) }, [createVNode(_component_RenderTag, { renderTag: _ctx.renderTag, option: _ctx.selectedOptions[0] }, { default: withCtx(() => [createVNode(_component_Ellipsis, { class: normalizeClass(`${_ctx.prefixCls}-label-text`), content: _ctx.labelTextRef }, null, 8 /* PROPS */, ["class", "content"])]), _: 1 /* STABLE */ }, 8 /* PROPS */, ["renderTag", "option"])], 2 /* CLASS */)) : createCommentVNode("v-if", true)], 64 /* STABLE_FRAGMENT */))], 64 /* STABLE_FRAGMENT */)) : (openBlock(), createElementBlock(Fragment, { key: 1 }, [(openBlock(true), createElementBlock(Fragment, null, renderList(_ctx.multiLabelRef, (tag, index) => { return openBlock(), createBlock(_component_RenderTag, { key: index, renderTag: _ctx.renderTag, option: tag, onClose: $event => _ctx.handleRemove(index) }, { default: withCtx(() => [tag.isCollapsed ? (openBlock(), createBlock(_component_Tooltip, { key: 0, mode: "popover", placement: "top", trigger: "hover", offset: 11, "popper-class": `${_ctx.prefixCls}-collapsed-item-popper` }, { content: withCtx(() => [createVNode(_component_Scrollbar, { maxHeight: 500 }, { default: withCtx(() => [(openBlock(true), createElementBlock(Fragment, null, renderList(tag.collapsedOptions, (collapsedTag, collapsedTagIndex) => { return openBlock(), createBlock(_component_Tag, { key: collapsedTagIndex, type: "info", size: "small", class: normalizeClass([`${_ctx.prefixCls}-label-item`, `${_ctx.prefixCls}-label-collapsed-item`]), closable: collapsedTag.closable, bordered: _ctx.hasTagBordered, onClose: $event => _ctx.handleRemove(_ctx.calcCollapseTagIndex(collapsedTagIndex)) }, { default: withCtx(() => [createVNode(_component_Ellipsis, { class: normalizeClass(`${_ctx.prefixCls}-label-text`), content: collapsedTag.label }, null, 8 /* PROPS */, ["class", "content"])]), _: 2 /* DYNAMIC */ }, 1032 /* PROPS, DYNAMIC_SLOTS */, ["class", "closable", "bordered", "onClose"]); }), 128 /* KEYED_FRAGMENT */))]), _: 2 /* DYNAMIC */ }, 1024 /* DYNAMIC_SLOTS */)]), default: withCtx(() => [createVNode(_component_Tag, { type: "info", size: "small", closable: tag.closable, class: normalizeClass(`${_ctx.prefixCls}-label-item`), bordered: _ctx.hasTagBordered, onClose: $event => _ctx.handleRemove(index) }, { default: withCtx(() => [createTextVNode(toDisplayString(tag.label), 1 /* TEXT */)]), _: 2 /* DYNAMIC */ }, 1032 /* PROPS, DYNAMIC_SLOTS */, ["closable", "class", "bordered", "onClose"])]), _: 2 /* DYNAMIC */ }, 1032 /* PROPS, DYNAMIC_SLOTS */, ["popper-class"])) : (openBlock(), createBlock(_component_Tag, { key: 1, type: "info", size: "small", closable: tag.closable, class: normalizeClass(`${_ctx.prefixCls}-label-item`), bordered: _ctx.hasTagBordered, onClose: $event => _ctx.handleRemove(index) }, { default: withCtx(() => [createVNode(_component_Ellipsis, { class: normalizeClass(`${_ctx.prefixCls}-label-text`), content: tag.label }, null, 8 /* PROPS */, ["class", "content"])]), _: 2 /* DYNAMIC */ }, 1032 /* PROPS, DYNAMIC_SLOTS */, ["closable", "class", "bordered", "onClose"]))]), _: 2 /* DYNAMIC */ }, 1032 /* PROPS, DYNAMIC_SLOTS */, ["renderTag", "option", "onClose"]); }), 128 /* KEYED_FRAGMENT */)), _ctx.unSelectedRef && !_ctx.filterTextRef.length && !_ctx.isComposingRef ? (openBlock(), createElementBlock("div", { key: 0, class: normalizeClass([`${_ctx.prefixCls}-label-placeholder`, `${_ctx.prefixCls}-label-overlay`]) }, toDisplayString(_ctx.placeholder), 3 /* TEXT, CLASS */)) : createCommentVNode("v-if", true), _ctx.filterable ? (openBlock(), createElementBlock("input", { key: 1, ref: "inputRef", value: _ctx.filterTextRef, class: normalizeClass(`${_ctx.prefixCls}-label-input`), style: normalizeStyle({ width: _ctx.inputWidthRef }), disabled: _ctx.disabled, onCompositionstart: _cache[4] || (_cache[4] = function () { return _ctx.handleCompositionStart && _ctx.handleCompositionStart(...arguments); }), onCompositionend: _cache[5] || (_cache[5] = function () { return _ctx.handleCompositionEnd && _ctx.handleCompositionEnd(...arguments); }), onCompositionupdate: _cache[6] || (_cache[6] = function () { return _ctx.handleCompositionUpdate && _ctx.handleCompositionUpdate(...arguments); }), onInput: _cache[7] || (_cache[7] = function () { return _ctx.handleInput && _ctx.handleInput(...arguments); }) }, null, 46 /* CLASS, STYLE, PROPS, NEED_HYDRATION */, _hoisted_2)) : createCommentVNode("v-if", true)], 64 /* STABLE_FRAGMENT */))], 2 /* CLASS */), createElementVNode("div", { class: normalizeClass(`${_ctx.prefixCls}-icons`), onMousedown: _cache[8] || (_cache[8] = withModifiers(() => {}, ["prevent"])) }, [withDirectives(createVNode(_component_UpOutlined, { class: normalizeClass(`${_ctx.prefixCls}-icon`) }, null, 8 /* PROPS */, ["class"]), [[vShow, _ctx.isOpened && !_ctx.hasClearRef]]), withDirectives(createVNode(_component_DownOutlined, { class: normalizeClass(`${_ctx.prefixCls}-icon`) }, null, 8 /* PROPS */, ["class"]), [[vShow, !_ctx.isOpened && !_ctx.hasClearRef]]), _ctx.clearable ? withDirectives((openBlock(), createBlock(_component_CloseCircleFilled, { key: 0, class: normalizeClass(`${_ctx.prefixCls}-icon`), onClick: withModifiers(_ctx.handleClear, ["stop"]) }, null, 8 /* PROPS */, ["class", "onClick"])), [[vShow, _ctx.hasClearRef]]) : createCommentVNode("v-if", true)], 34 /* CLASS, NEED_HYDRATION */)], 34 /* CLASS, NEED_HYDRATION */); } script.render = render; script.__file = "components/select-trigger/selectTrigger.vue"; export { script as default };