UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

175 lines (147 loc) 5.66 kB
import _typeof from "@babel/runtime/helpers/typeof"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _extends from "@babel/runtime/helpers/extends"; import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; function _createSuper(Derived) { function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } return function () { var Super = _getPrototypeOf(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } import React, { Children, Component, isValidElement } from 'react'; import classNames from 'classnames'; import Select from '../select'; import Input from '../input'; import InputElement from './InputElement'; import { OptGroup, Option } from '../rc-components/select'; import { Size } from '../_util/enum'; import { SelectMode } from '../select/enum'; import ConfigContext from '../config-provider/ConfigContext'; function isSelectOptionOrSelectOptGroup(child) { return child && child.type && (child.type.isSelectOption || child.type.isSelectOptGroup); } var AutoComplete = /*#__PURE__*/ function (_Component) { _inherits(AutoComplete, _Component); var _super = _createSuper(AutoComplete); function AutoComplete() { var _this; _classCallCheck(this, AutoComplete); _this = _super.apply(this, arguments); _this.getInputElement = function () { var children = _this.props.children; var element = children && isValidElement(children) && children.type !== Option ? Children.only(children) : React.createElement(Input, { border: false }); var elementProps = _objectSpread({}, element.props); delete elementProps.children; return React.createElement(InputElement, _extends({}, elementProps), element); }; _this.saveSelect = function (node) { _this.select = node; }; return _this; } _createClass(AutoComplete, [{ key: "focus", value: function focus() { this.select.focus(); } }, { key: "blur", value: function blur() { this.select.blur(); } }, { key: "render", value: function render() { var _classNames; var _this$props = this.props, size = _this$props.size, _this$props$className = _this$props.className, className = _this$props$className === void 0 ? '' : _this$props$className, notFoundContent = _this$props.notFoundContent, customizePrefixCls = _this$props.prefixCls, optionLabelProp = _this$props.optionLabelProp, dataSource = _this$props.dataSource, children = _this$props.children; var getPrefixCls = this.context.getPrefixCls; var prefixCls = getPrefixCls('select', customizePrefixCls); var cls = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-lg"), size === Size.large), _defineProperty(_classNames, "".concat(prefixCls, "-sm"), size === Size.small), _defineProperty(_classNames, className, !!className), _defineProperty(_classNames, "".concat(prefixCls, "-show-search"), true), _defineProperty(_classNames, "".concat(prefixCls, "-auto-complete"), true), _classNames)); var options; var childArray = Children.toArray(children); if (childArray.length && isSelectOptionOrSelectOptGroup(childArray[0])) { options = children; } else { options = dataSource ? dataSource.map(function (item) { if (isValidElement(item)) { return item; } switch (_typeof(item)) { case 'string': return React.createElement(Option, { key: item }, item); case 'object': return React.createElement(Option, { key: item.value }, item.text); default: throw new Error('AutoComplete[dataSource] only supports type `string[] | Object[]`.'); } }) : []; } return React.createElement(Select, _extends({}, this.props, { prefixCls: prefixCls, className: cls, mode: SelectMode.combobox, optionLabelProp: optionLabelProp, getInputElement: this.getInputElement, notFoundContent: notFoundContent, ref: this.saveSelect }), options); } }], [{ key: "contextType", get: function get() { return ConfigContext; } }]); return AutoComplete; }(Component); export { AutoComplete as default }; AutoComplete.displayName = 'AutoComplete'; AutoComplete.Option = Option; AutoComplete.OptGroup = OptGroup; AutoComplete.defaultProps = { transitionName: 'slide-up', optionLabelProp: 'children', choiceTransitionName: 'zoom', showSearch: false, filterOption: false }; //# sourceMappingURL=index.js.map