UNPKG

wix-style-react

Version:
142 lines (115 loc) • 6.15 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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 _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; }; /* eslint-disable no-console */ var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _MultiSelect = require('wix-style-react/MultiSelect'); var _MultiSelect2 = _interopRequireDefault(_MultiSelect); var _ContactItemBuilder = require('wix-style-react/ContactItemBuilder'); 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 contacts = [{ name: 'David Fincher', email: 'davidf@wix.com' }, { name: 'John Doe', email: 'johnd@wix.com' }, { name: 'Jane Martin', email: 'janem@wix.com' }, { name: 'David ', email: 'davidf@gmail.com' }, { name: 'John Doe', email: 'johnd@gmail.com' }, { name: 'Jane Martin', email: 'janem@gmail.com' }]; var options = contacts.map(function (contact) { return _extends({}, contact, (0, _ContactItemBuilder.contactItemBuilder)({ id: contact.email, title: contact.name, subtitle: contact.email, imageUrl: 'https://randomuser.me/api/portraits/women/39.jpg' })); }); var ContactsInput = function (_React$Component) { _inherits(ContactsInput, _React$Component); function ContactsInput(props) { _classCallCheck(this, ContactsInput); var _this = _possibleConstructorReturn(this, (ContactsInput.__proto__ || Object.getPrototypeOf(ContactsInput)).call(this, props)); _this.nextId = 0; _this.state = { tags: [], inputValue: '' }; _this.handleOnSelect = _this.handleOnSelect.bind(_this); _this.handleOnManuallyInput = _this.handleOnManuallyInput.bind(_this); _this.handleOnRemoveTag = _this.handleOnRemoveTag.bind(_this); _this.handleOnChange = _this.handleOnChange.bind(_this); _this.predicate = _this.predicate.bind(_this); return _this; } _createClass(ContactsInput, [{ key: 'createTag', value: function createTag(_ref) { var name = _ref.name, email = _ref.email; return { id: String(this.nextId++), label: name ? email + ' (' + name + ')' : email }; } }, { key: 'handleOnSelect', value: function handleOnSelect(option) { console.log('onSelect(option): option=', option); var newTag = this.createTag({ name: option.name, email: option.email }); this.setState({ tags: [].concat(_toConsumableArray(this.state.tags), [newTag]) }); } }, { key: 'handleOnRemoveTag', value: function handleOnRemoveTag(tagId) { console.log('onRemoveTag(tagId): tagId=' + tagId + ')'); this.setState({ tags: this.state.tags.filter(function (currTag) { return currTag.id !== tagId; }) }); } }, { key: 'handleOnChange', value: function handleOnChange(event) { console.log('onChange(\'' + event.target.value + '\')'); this.setState({ inputValue: event.target.value }); } }, { key: 'handleOnManuallyInput', value: function handleOnManuallyInput(values) { var _this2 = this; console.log('onManuallyInput(values): values=' + values); var tags = values.map(function (value) { return _this2.createTag({ email: value }); }); this.setState({ tags: [].concat(_toConsumableArray(this.state.tags), _toConsumableArray(tags)) }); } }, { key: 'predicate', value: function predicate(option) { return (option.name + ' + ' + option.emial).toLowerCase().includes(this.state.inputValue.toLowerCase()); } }, { key: 'render', value: function render() { return _react2.default.createElement(_MultiSelect2.default // If I remove this comment - the ReactLive get stuck , { dataHook: 'multi-select-standard', value: this.state.inputValue, onChange: this.handleOnChange, options: options, tags: this.state.tags, onManuallyInput: this.handleOnManuallyInput, onSelect: this.handleOnSelect, onRemoveTag: this.handleOnRemoveTag, predicate: this.predicate, upgrade: true, highlight: false // FIXME: This is a workaround a current bug }); } }]); return ContactsInput; }(_react2.default.Component); exports.default = ContactsInput;