index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import _typeof from "@babel/runtime/helpers/esm/typeof";
  3. import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
  4. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  5. import { resolveDirective as _resolveDirective, createVNode as _createVNode } from "vue";
  6. import { defineComponent, ref } from 'vue';
  7. import Select, { selectProps } from '../select';
  8. import { isValidElement, flattenChildren } from '../_util/props-util';
  9. import warning from '../_util/warning';
  10. import Option from './Option';
  11. import OptGroup from './OptGroup';
  12. import omit from '../_util/omit';
  13. import useConfigInject from '../_util/hooks/useConfigInject';
  14. function isSelectOptionOrSelectOptGroup(child) {
  15. var _child$type, _child$type2;
  16. return (child === null || child === void 0 ? void 0 : (_child$type = child.type) === null || _child$type === void 0 ? void 0 : _child$type.isSelectOption) || (child === null || child === void 0 ? void 0 : (_child$type2 = child.type) === null || _child$type2 === void 0 ? void 0 : _child$type2.isSelectOptGroup);
  17. }
  18. export var autoCompleteProps = function autoCompleteProps() {
  19. return _objectSpread(_objectSpread({}, omit(selectProps(), ['loading', 'mode', 'optionLabelProp', 'labelInValue'])), {}, {
  20. dataSource: Array,
  21. dropdownMenuStyle: {
  22. type: Object,
  23. default: undefined
  24. },
  25. // optionLabelProp: String,
  26. dropdownMatchSelectWidth: {
  27. type: [Number, Boolean],
  28. default: true
  29. },
  30. prefixCls: String,
  31. showSearch: {
  32. type: Boolean,
  33. default: undefined
  34. },
  35. transitionName: String,
  36. choiceTransitionName: {
  37. type: String,
  38. default: 'zoom'
  39. },
  40. autofocus: {
  41. type: Boolean,
  42. default: undefined
  43. },
  44. backfill: {
  45. type: Boolean,
  46. default: undefined
  47. },
  48. // optionLabelProp: PropTypes.string.def('children'),
  49. filterOption: {
  50. type: [Boolean, Function],
  51. default: false
  52. },
  53. defaultActiveFirstOption: {
  54. type: Boolean,
  55. default: true
  56. }
  57. });
  58. };
  59. export var AutoCompleteOption = Option;
  60. export var AutoCompleteOptGroup = OptGroup;
  61. var AutoComplete = defineComponent({
  62. compatConfig: {
  63. MODE: 3
  64. },
  65. name: 'AAutoComplete',
  66. inheritAttrs: false,
  67. props: autoCompleteProps(),
  68. // emits: ['change', 'select', 'focus', 'blur'],
  69. slots: ['option'],
  70. setup: function setup(props, _ref) {
  71. var slots = _ref.slots,
  72. attrs = _ref.attrs,
  73. expose = _ref.expose;
  74. warning(!('dataSource' in slots), 'AutoComplete', '`dataSource` slot is deprecated, please use props `options` instead.');
  75. warning(!('options' in slots), 'AutoComplete', '`options` slot is deprecated, please use props `options` instead.');
  76. var selectRef = ref();
  77. var getInputElement = function getInputElement() {
  78. var _slots$default;
  79. var children = flattenChildren((_slots$default = slots.default) === null || _slots$default === void 0 ? void 0 : _slots$default.call(slots));
  80. var element = children.length ? children[0] : undefined;
  81. return element;
  82. };
  83. var focus = function focus() {
  84. var _selectRef$value;
  85. (_selectRef$value = selectRef.value) === null || _selectRef$value === void 0 ? void 0 : _selectRef$value.focus();
  86. };
  87. var blur = function blur() {
  88. var _selectRef$value2;
  89. (_selectRef$value2 = selectRef.value) === null || _selectRef$value2 === void 0 ? void 0 : _selectRef$value2.blur();
  90. };
  91. expose({
  92. focus: focus,
  93. blur: blur
  94. });
  95. var _useConfigInject = useConfigInject('select', props),
  96. prefixCls = _useConfigInject.prefixCls;
  97. return function () {
  98. var _slots$notFoundConten, _cls;
  99. var size = props.size,
  100. dataSource = props.dataSource,
  101. _props$notFoundConten = props.notFoundContent,
  102. notFoundContent = _props$notFoundConten === void 0 ? (_slots$notFoundConten = slots.notFoundContent) === null || _slots$notFoundConten === void 0 ? void 0 : _slots$notFoundConten.call(slots) : _props$notFoundConten;
  103. var optionChildren;
  104. var className = attrs.class;
  105. var cls = (_cls = {}, _defineProperty(_cls, className, !!className), _defineProperty(_cls, "".concat(prefixCls.value, "-lg"), size === 'large'), _defineProperty(_cls, "".concat(prefixCls.value, "-sm"), size === 'small'), _defineProperty(_cls, "".concat(prefixCls.value, "-show-search"), true), _defineProperty(_cls, "".concat(prefixCls.value, "-auto-complete"), true), _cls);
  106. if (props.options === undefined) {
  107. var _slots$dataSource, _slots$options;
  108. var childArray = ((_slots$dataSource = slots.dataSource) === null || _slots$dataSource === void 0 ? void 0 : _slots$dataSource.call(slots)) || ((_slots$options = slots.options) === null || _slots$options === void 0 ? void 0 : _slots$options.call(slots)) || [];
  109. if (childArray.length && isSelectOptionOrSelectOptGroup(childArray[0])) {
  110. optionChildren = childArray;
  111. } else {
  112. optionChildren = dataSource ? dataSource.map(function (item) {
  113. if (isValidElement(item)) {
  114. return item;
  115. }
  116. switch (_typeof(item)) {
  117. case 'string':
  118. return _createVNode(Option, {
  119. "key": item,
  120. "value": item
  121. }, {
  122. default: function _default() {
  123. return [item];
  124. }
  125. });
  126. case 'object':
  127. return _createVNode(Option, {
  128. "key": item.value,
  129. "value": item.value
  130. }, {
  131. default: function _default() {
  132. return [item.text];
  133. }
  134. });
  135. default:
  136. throw new Error('AutoComplete[dataSource] only supports type `string[] | Object[]`.');
  137. }
  138. }) : [];
  139. }
  140. }
  141. var selectProps = omit(_objectSpread(_objectSpread(_objectSpread({}, props), attrs), {}, {
  142. mode: Select.SECRET_COMBOBOX_MODE_DO_NOT_USE,
  143. // optionLabelProp,
  144. getInputElement: getInputElement,
  145. notFoundContent: notFoundContent,
  146. // placeholder: '',
  147. class: cls,
  148. ref: selectRef
  149. }), ['dataSource', 'loading']);
  150. return _createVNode(Select, selectProps, _objectSpread({
  151. default: function _default() {
  152. return [optionChildren];
  153. }
  154. }, omit(slots, ['default', 'dataSource', 'options'])));
  155. };
  156. }
  157. });
  158. /* istanbul ignore next */
  159. export default _extends(AutoComplete, {
  160. Option: Option,
  161. OptGroup: OptGroup,
  162. install: function install(app) {
  163. app.component(AutoComplete.name, AutoComplete);
  164. app.component(Option.displayName, Option);
  165. app.component(OptGroup.displayName, OptGroup);
  166. return app;
  167. }
  168. });