SingleSelector.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { Fragment as _Fragment, createVNode as _createVNode } from "vue";
  2. import pickAttrs from '../../_util/pickAttrs';
  3. import Input from './Input';
  4. import { Fragment, computed, defineComponent, shallowRef, watch } from 'vue';
  5. import PropTypes from '../../_util/vue-types';
  6. import useInjectLegacySelectContext from '../../vc-tree-select/LegacyContext';
  7. const props = {
  8. inputElement: PropTypes.any,
  9. id: String,
  10. prefixCls: String,
  11. values: PropTypes.array,
  12. open: {
  13. type: Boolean,
  14. default: undefined
  15. },
  16. searchValue: String,
  17. inputRef: PropTypes.any,
  18. placeholder: PropTypes.any,
  19. compositionStatus: {
  20. type: Boolean,
  21. default: undefined
  22. },
  23. disabled: {
  24. type: Boolean,
  25. default: undefined
  26. },
  27. mode: String,
  28. showSearch: {
  29. type: Boolean,
  30. default: undefined
  31. },
  32. autofocus: {
  33. type: Boolean,
  34. default: undefined
  35. },
  36. autocomplete: String,
  37. activeDescendantId: String,
  38. tabindex: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  39. activeValue: String,
  40. backfill: {
  41. type: Boolean,
  42. default: undefined
  43. },
  44. optionLabelRender: Function,
  45. onInputChange: Function,
  46. onInputPaste: Function,
  47. onInputKeyDown: Function,
  48. onInputMouseDown: Function,
  49. onInputCompositionStart: Function,
  50. onInputCompositionEnd: Function
  51. };
  52. const SingleSelector = defineComponent({
  53. name: 'SingleSelector',
  54. setup(props) {
  55. const inputChanged = shallowRef(false);
  56. const combobox = computed(() => props.mode === 'combobox');
  57. const inputEditable = computed(() => combobox.value || props.showSearch);
  58. const inputValue = computed(() => {
  59. let inputValue = props.searchValue || '';
  60. if (combobox.value && props.activeValue && !inputChanged.value) {
  61. inputValue = props.activeValue;
  62. }
  63. return inputValue;
  64. });
  65. const legacyTreeSelectContext = useInjectLegacySelectContext();
  66. watch([combobox, () => props.activeValue], () => {
  67. if (combobox.value) {
  68. inputChanged.value = false;
  69. }
  70. }, {
  71. immediate: true
  72. });
  73. // Not show text when closed expect combobox mode
  74. const hasTextInput = computed(() => props.mode !== 'combobox' && !props.open && !props.showSearch ? false : !!inputValue.value || props.compositionStatus);
  75. const title = computed(() => {
  76. const item = props.values[0];
  77. return item && (typeof item.label === 'string' || typeof item.label === 'number') ? item.label.toString() : undefined;
  78. });
  79. const renderPlaceholder = () => {
  80. if (props.values[0]) {
  81. return null;
  82. }
  83. const hiddenStyle = hasTextInput.value ? {
  84. visibility: 'hidden'
  85. } : undefined;
  86. return _createVNode("span", {
  87. "class": `${props.prefixCls}-selection-placeholder`,
  88. "style": hiddenStyle
  89. }, [props.placeholder]);
  90. };
  91. const handleInput = e => {
  92. const composing = e.target.composing;
  93. if (!composing) {
  94. inputChanged.value = true;
  95. props.onInputChange(e);
  96. }
  97. };
  98. return () => {
  99. var _a, _b, _c, _d;
  100. const {
  101. inputElement,
  102. prefixCls,
  103. id,
  104. values,
  105. inputRef,
  106. disabled,
  107. autofocus,
  108. autocomplete,
  109. activeDescendantId,
  110. open,
  111. tabindex,
  112. optionLabelRender,
  113. onInputKeyDown,
  114. onInputMouseDown,
  115. onInputPaste,
  116. onInputCompositionStart,
  117. onInputCompositionEnd
  118. } = props;
  119. const item = values[0];
  120. let titleNode = null;
  121. // custom tree-select title by slot
  122. // For TreeSelect
  123. if (item && legacyTreeSelectContext.customSlots) {
  124. const key = (_a = item.key) !== null && _a !== void 0 ? _a : item.value;
  125. const originData = ((_b = legacyTreeSelectContext.keyEntities[key]) === null || _b === void 0 ? void 0 : _b.node) || {};
  126. titleNode = legacyTreeSelectContext.customSlots[(_c = originData.slots) === null || _c === void 0 ? void 0 : _c.title] || legacyTreeSelectContext.customSlots.title || item.label;
  127. if (typeof titleNode === 'function') {
  128. titleNode = titleNode(originData);
  129. }
  130. // else if (treeSelectContext.value.slots.titleRender) {
  131. // // 因历史 title 是覆盖逻辑,新增 titleRender,所有的 title 都走一遍 titleRender
  132. // titleNode = treeSelectContext.value.slots.titleRender(item.option?.data || {});
  133. // }
  134. } else {
  135. titleNode = optionLabelRender && item ? optionLabelRender(item.option) : item === null || item === void 0 ? void 0 : item.label;
  136. }
  137. return _createVNode(_Fragment, null, [_createVNode("span", {
  138. "class": `${prefixCls}-selection-search`
  139. }, [_createVNode(Input, {
  140. "inputRef": inputRef,
  141. "prefixCls": prefixCls,
  142. "id": id,
  143. "open": open,
  144. "inputElement": inputElement,
  145. "disabled": disabled,
  146. "autofocus": autofocus,
  147. "autocomplete": autocomplete,
  148. "editable": inputEditable.value,
  149. "activeDescendantId": activeDescendantId,
  150. "value": inputValue.value,
  151. "onKeydown": onInputKeyDown,
  152. "onMousedown": onInputMouseDown,
  153. "onChange": handleInput,
  154. "onPaste": onInputPaste,
  155. "onCompositionstart": onInputCompositionStart,
  156. "onCompositionend": onInputCompositionEnd,
  157. "tabindex": tabindex,
  158. "attrs": pickAttrs(props, true)
  159. }, null)]), !combobox.value && item && !hasTextInput.value && _createVNode("span", {
  160. "class": `${prefixCls}-selection-item`,
  161. "title": title.value
  162. }, [_createVNode(_Fragment, {
  163. "key": (_d = item.key) !== null && _d !== void 0 ? _d : item.value
  164. }, [titleNode])]), renderPlaceholder()]);
  165. };
  166. }
  167. });
  168. SingleSelector.props = props;
  169. SingleSelector.inheritAttrs = false;
  170. export default SingleSelector;