UNPKG

wix-style-react

Version:
122 lines (101 loc) • 5.76 kB
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 _class, _temp; 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, { Component } from 'react'; import PropTypes from 'prop-types'; import reactElementToJSXString from 'react-element-to-jsx-string'; import MultiSelectComposite from '../../src/MultiSelectComposite'; import MultiSelect from '../../src/MultiSelect'; import Label from '../../src/Label'; var options = [{ value: 'Alabama', id: 'Alabama' }, { value: 'Alaska', id: 'Alaska' }, { value: 'Arizona', id: 'Arizona' }, { value: 'Arkansas', id: 'Arkansas', tag: { label: 'Ark.' } }, { value: 'California', id: 'California' }, { value: 'California2', id: 'California2' }, { value: 'California3', id: 'California3' }, { value: 'California4', id: 'California4' }, { value: 'California5', id: 'California5' }, { value: 'California6', id: 'California6' }, { value: 'California7', id: 'California7' }, { value: 'Two words', id: 'Two words' }]; var valueParser = function valueParser(option) { return option.tag ? option.tag.label : option.value; }; var Form = (_temp = _class = function (_Component) { _inherits(Form, _Component); _createClass(Form, [{ key: 'componentDidUpdate', value: function componentDidUpdate(props) { props.onChange(reactElementToJSXString(this.getComponent())); } }, { key: 'componentDidMount', value: function componentDidMount() { this.props.onChange(reactElementToJSXString(this.getComponent())); } }]); function Form(props) { _classCallCheck(this, Form); var _this = _possibleConstructorReturn(this, (Form.__proto__ || Object.getPrototypeOf(Form)).call(this, props)); _this.getValue = function (option) { return typeof option.value === 'string' ? option.value : option.value.props.children[0].props.children; }; _this.handleOnSelect = function (tags) { return Array.isArray(tags) ? _this.setState({ tags: [].concat(_toConsumableArray(_this.state.tags), _toConsumableArray(tags)) }) : _this.setState({ tags: [].concat(_toConsumableArray(_this.state.tags), [tags]) }); }; _this.handleOnRemoveTag = function (tagId) { return _this.setState({ tags: _this.state.tags.filter(function (currTag) { return currTag.id !== tagId; }) }); }; _this.handleOnChange = function (event) { return _this.setState({ inputValue: event.target.value }); }; _this.predicate = function (option) { return _this.getValue(option).toLowerCase().includes(_this.state.inputValue.toLowerCase()); }; _this.state = { tags: [], options: options, inputValue: '' }; return _this; } _createClass(Form, [{ key: 'getComponent', value: function getComponent() { return React.createElement( MultiSelectComposite, { required: this.props.required, info: this.props.info }, this.props.withLabel ? React.createElement(Label, _extends({ 'for': 'firstName' }, this.props.label)) : null, React.createElement(MultiSelect, { tags: this.state.tags, onSelect: this.handleOnSelect, onRemoveTag: this.handleOnRemoveTag, onChange: this.handleOnChange, onManuallyInput: function onManuallyInput() { return console.log('NOW'); }, options: this.state.options, value: this.state.inputValue, predicate: this.predicate, valueParser: valueParser }) ); } }, { key: 'render', value: function render() { return this.getComponent(); } }]); return Form; }(Component), _class.propTypes = { onChange: PropTypes.func.isRequired, withLabel: PropTypes.bool, label: PropTypes.object, input: PropTypes.object, required: PropTypes.bool, info: PropTypes.string }, _temp); export { Form as default };