UNPKG

wix-style-react

Version:
141 lines (119 loc) 4.97 kB
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; }; }(); 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; } /* eslint-disable */ var ExampleControlledInput = function (_React$Component) { _inherits(ExampleControlledInput, _React$Component); function ExampleControlledInput(props) { _classCallCheck(this, ExampleControlledInput); var _this = _possibleConstructorReturn(this, (ExampleControlledInput.__proto__ || Object.getPrototypeOf(ExampleControlledInput)).call(this, props)); _this.state = { open: false, selectedId: -1, value: '' }; _this._open = _this._open.bind(_this); _this._close = _this._close.bind(_this); _this._toggle = _this._toggle.bind(_this); _this._onSelect = _this._onSelect.bind(_this); _this._onChange = _this._onChange.bind(_this); _this._onKeyDown = _this._onKeyDown.bind(_this); return _this; } _createClass(ExampleControlledInput, [{ key: '_open', value: function _open() { this.setState({ open: true }); } }, { key: '_close', value: function _close() { this.setState({ open: false }); } }, { key: '_toggle', value: function _toggle() { this.setState(function (_ref) { var open = _ref.open; return { open: !open }; }); } }, { key: '_onSelect', value: function _onSelect(selectedOption) { console.log('Select option:', selectedOption); this.setState({ selectedId: selectedOption.id, open: false, value: selectedOption.value }); } }, { key: '_onChange', value: function _onChange(e) { var value = e.target.value; this.setState({ value: e.target.value }); if (value.trim()) { this._open(); } } }, { key: '_onKeyDown', value: function _onKeyDown(e, delegateKeyDown) { var eventWasHandled = delegateKeyDown(e); // We'll open the list when pressing the ArrowDown key if (!eventWasHandled && e.key === 'ArrowDown') { this._open(); e.preventDefault(); return; } // Close on escape if (e.key === 'Escape') { this._close(); e.preventDefault(); } } }, { key: 'render', value: function render() { var _this2 = this; var _state = this.state, open = _state.open, selectedId = _state.selectedId, value = _state.value; return React.createElement( DropdownBase, { 'data-hook': 'story-dropdown-base-controlled-input', open: open, onClickOutside: this._close, options: [{ id: 0, value: 'First option' }, { id: 1, value: 'Second option' }, { id: 2, value: 'Third option' }, { id: 3, value: 'Fourth option' }, { id: 4, value: 'Fifth option' }, { id: 5, value: 'Sixth option' }], selectedId: selectedId, onSelect: this._onSelect }, function (_ref2) { var delegateKeyDown = _ref2.delegateKeyDown; return React.createElement(Input, { menuArrow: true, placeholder: 'Type something', value: value, onChange: _this2._onChange, onInputClicked: _this2._open, onKeyDown: function onKeyDown(e) { return _this2._onKeyDown(e, delegateKeyDown); } }); } ); } }]); return ExampleControlledInput; }(React.Component); render(React.createElement( 'div', { style: { textAlign: 'center' } }, React.createElement(ExampleControlledInput, null) ));