UNPKG

react-select

Version:

A Select control built with and for ReactJS

121 lines (98 loc) 5.62 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _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; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var _react2 = _interopRequireDefault(_react); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var manageState = function manageState(SelectComponent) { var _class, _temp2; return _temp2 = _class = function (_Component) { _inherits(StateManager, _Component); function StateManager() { var _ref; var _temp, _this, _ret; _classCallCheck(this, StateManager); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = StateManager.__proto__ || Object.getPrototypeOf(StateManager)).call.apply(_ref, [this].concat(args))), _this), _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 }); }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(StateManager, [{ key: 'focus', value: function focus() { this.select.focus(); } }, { key: 'blur', value: function blur() { this.select.blur(); } }, { key: 'getProp', value: function getProp(key) { return this.props[key] !== undefined ? this.props[key] : this.state[key]; } }, { key: 'callProp', value: function callProp(name) { if (typeof this.props[name] === 'function') { var _props; for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { args[_key2 - 1] = arguments[_key2]; } return (_props = this.props)[name].apply(_props, _toConsumableArray(args)); } } }, { key: 'render', value: function render() { var _this2 = this; return _react2.default.createElement(SelectComponent, _extends({}, this.props, { ref: function ref(_ref2) { _this2.select = _ref2; }, 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 = { defaultInputValue: '', defaultMenuIsOpen: false, defaultValue: null }, _temp2; }; exports.default = manageState;