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