UNPKG

wix-style-react

Version:
98 lines (81 loc) 4.25 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 _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; } /* eslint-disable no-console */ import React from 'react'; import MultiSelect from 'wix-style-react/MultiSelect'; var options = [{ id: '1', name: 'One', value: 'One' }, { id: '2', name: 'Two', value: 'Two' }, { id: '3', name: 'Three', value: 'Three' }]; var ExampleReorderable = function (_React$Component) { _inherits(ExampleReorderable, _React$Component); function ExampleReorderable(props) { _classCallCheck(this, ExampleReorderable); var _this = _possibleConstructorReturn(this, (ExampleReorderable.__proto__ || Object.getPrototypeOf(ExampleReorderable)).call(this, props)); _this.state = { tags: [], options: options, inputValue: '' }; _this.handleOnSelect = _this.handleOnSelect.bind(_this); _this.handleOnRemoveTag = _this.handleOnRemoveTag.bind(_this); _this.handleOnChange = _this.handleOnChange.bind(_this); return _this; } _createClass(ExampleReorderable, [{ key: 'handleOnSelect', value: function handleOnSelect(option) { var newTag = { id: option.id, label: React.createElement( 'span', null, option.name ) }; this.setState({ tags: [].concat(_toConsumableArray(this.state.tags), [newTag]) }); } }, { key: 'handleOnRemoveTag', value: function handleOnRemoveTag(tagId) { this.setState({ tags: this.state.tags.filter(function (currTag) { return currTag.id !== tagId; }) }); } }, { key: 'handleOnChange', value: function handleOnChange(event) { this.setState({ inputValue: event.target.value }); } }, { key: 'render', value: function render() { var _this2 = this; var tags = this.state.tags; return React.createElement(MultiSelect, { mode: 'select', dataHook: 'multi-select-reorderable', tags: this.state.tags, onSelect: this.handleOnSelect, onRemoveTag: this.handleOnRemoveTag, onReorder: function onReorder(_ref) { var addedIndex = _ref.addedIndex, removedIndex = _ref.removedIndex; var nextTags = tags.slice(); nextTags.splice.apply(nextTags, [addedIndex, 0].concat(_toConsumableArray(nextTags.splice(removedIndex, 1)))); _this2.setState({ tags: nextTags }); }, value: this.state.inputValue, onChange: this.handleOnChange, options: options, upgrade: true }); } }]); return ExampleReorderable; }(React.Component); export default ExampleReorderable;