UNPKG

react-select

Version:

A Select control built with and for ReactJS

135 lines (103 loc) 4.69 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var React = require('react'); var React__default = _interopDefault(React); function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } var defaultProps = { defaultInputValue: '', defaultMenuIsOpen: false, defaultValue: null }; var manageState = function manageState(SelectComponent) { var _class, _temp; return _temp = _class = /*#__PURE__*/ function (_Component) { _inheritsLoose(StateManager, _Component); function StateManager() { var _this; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _Component.call.apply(_Component, [this].concat(args)) || this; _this.select = void 0; _this.state = { inputValue: _this.props.inputValue !== undefined ? _this.props.inputValue : _this.props.defaultInputValue, menuIsOpen: _this.props.menuIsOpen !== undefined ? _this.props.menuIsOpen : _this.props.defaultMenuIsOpen, value: _this.props.value !== undefined ? _this.props.value : _this.props.defaultValue }; _this.onChange = function (value, actionMeta) { _this.callProp('onChange', value, actionMeta); _this.setState({ value: value }); }; _this.onInputChange = function (value, actionMeta) { // TODO: for backwards compatibility, we allow the prop to return a new // value, but now inputValue is a controllable prop we probably shouldn't var newValue = _this.callProp('onInputChange', value, actionMeta); _this.setState({ inputValue: newValue !== undefined ? newValue : value }); }; _this.onMenuOpen = function () { _this.callProp('onMenuOpen'); _this.setState({ menuIsOpen: true }); }; _this.onMenuClose = function () { _this.callProp('onMenuClose'); _this.setState({ menuIsOpen: false }); }; return _this; } var _proto = StateManager.prototype; _proto.focus = function focus() { this.select.focus(); }; _proto.blur = function blur() { this.select.blur(); } // FIXME: untyped flow code, return any ; _proto.getProp = function getProp(key) { return this.props[key] !== undefined ? this.props[key] : this.state[key]; } // FIXME: untyped flow code, return any ; _proto.callProp = function callProp(name) { if (typeof this.props[name] === 'function') { var _this$props; for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { args[_key2 - 1] = arguments[_key2]; } return (_this$props = this.props)[name].apply(_this$props, args); } }; _proto.render = function render() { var _this2 = this; var _this$props2 = this.props, defaultInputValue = _this$props2.defaultInputValue, defaultMenuIsOpen = _this$props2.defaultMenuIsOpen, defaultValue = _this$props2.defaultValue, props = _objectWithoutPropertiesLoose(_this$props2, ["defaultInputValue", "defaultMenuIsOpen", "defaultValue"]); return React__default.createElement(SelectComponent, _extends({}, props, { ref: function ref(_ref) { _this2.select = _ref; }, inputValue: this.getProp('inputValue'), menuIsOpen: this.getProp('menuIsOpen'), onChange: this.onChange, onInputChange: this.onInputChange, onMenuClose: this.onMenuClose, onMenuOpen: this.onMenuOpen, value: this.getProp('value') })); }; return StateManager; }(React.Component), _class.defaultProps = defaultProps, _temp; }; exports.manageState = manageState;