wix-style-react
Version:
wix-style-react
109 lines (92 loc) • 5.34 kB
JavaScript
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; };
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 countries = [{ name: 'Alabama', code: 'AL' }, { name: 'Alaska', code: 'AK' }, { name: 'Arizona', code: 'AZ' }, { name: 'Arkansas', code: 'AR' }, { name: 'California', code: 'CA' }, { name: 'North Carolina', code: 'NC' }, { name: 'Colorado', code: 'CO' }, { name: 'Connecticut', code: 'CT' }, { name: 'Delaware', code: 'DL' }, { name: 'Florida', code: 'FL' }, { name: 'Georgia', code: 'GA' }, { name: 'Hawaii', code: 'HI' }, { name: 'Idaho', code: 'IL' }, { name: 'Illinois', code: 'IN' }, { name: 'Indiana', code: 'IA' }];
var options = countries.map(function (country) {
return _extends({}, country, {
value: country.name, // This can be any ReactNode
id: country.code
});
});
var CountrySelection = function (_React$Component) {
_inherits(CountrySelection, _React$Component);
function CountrySelection(props) {
_classCallCheck(this, CountrySelection);
var _this = _possibleConstructorReturn(this, (CountrySelection.__proto__ || Object.getPrototypeOf(CountrySelection)).call(this, props));
_this.nextId = 0;
_this.state = {
tags: [],
inputValue: ''
};
_this.handleOnSelect = _this.handleOnSelect.bind(_this);
_this.handleOnRemoveTag = _this.handleOnRemoveTag.bind(_this);
_this.handleOnChange = _this.handleOnChange.bind(_this);
_this.predicate = _this.predicate.bind(_this);
return _this;
}
_createClass(CountrySelection, [{
key: 'createTag',
value: function createTag(_ref) {
var countryName = _ref.countryName,
countryCode = _ref.countryCode;
return {
id: countryCode, // When tag ids correspond to option ids, then MultiSelect will show only unselected options.
label: countryName + ' (' + (countryCode || '?') + ')'
};
}
}, {
key: 'handleOnSelect',
value: function handleOnSelect(option) {
console.log('onSelect(option): option=', option);
var newTag = this.createTag({
countryName: option.name,
countryCode: option.code
});
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: 'predicate',
value: function predicate(option) {
return option.name && option.name.toLowerCase().includes(this.state.inputValue.toLowerCase());
}
}, {
key: 'render',
value: function render() {
return React.createElement(MultiSelect, {
dataHook: 'multi-select-only',
value: this.state.inputValue,
onChange: this.handleOnChange,
options: options,
tags: this.state.tags,
onSelect: this.handleOnSelect,
onRemoveTag: this.handleOnRemoveTag,
predicate: this.predicate,
upgrade: true
});
}
}]);
return CountrySelection;
}(React.Component);
export default CountrySelection;