UNPKG

sc-react-ions

Version:

An open source set of React components that implement Ambassador's Design and UX patterns.

250 lines (196 loc) 8.85 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); 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 _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _OptClass = require('../internal/OptClass'); var _OptClass2 = _interopRequireDefault(_OptClass); var _TagList = require('../internal/TagList'); var _TagList2 = _interopRequireDefault(_TagList); var _style = require('./style.scss'); var _style2 = _interopRequireDefault(_style); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } 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 MultiSelect = function (_React$Component) { _inherits(MultiSelect, _React$Component); function MultiSelect(props) { _classCallCheck(this, MultiSelect); var _this = _possibleConstructorReturn(this, (MultiSelect.__proto__ || Object.getPrototypeOf(MultiSelect)).call(this, props)); _initialiseProps.call(_this); return _this; } _createClass(MultiSelect, [{ key: 'render', value: function render() { var multiSelectClasses = (0, _OptClass2.default)(_style2.default, 'multi-select', this.props.optClass); var elements = this.getElements(this.props.children); return _react2.default.createElement( 'div', { className: multiSelectClasses }, elements, _react2.default.createElement(_TagList2.default, { tags: this.state.selected, displayProp: this.props.displayProp, onRemove: this.onRemove }) ); } }]); return MultiSelect; }(_react2.default.Component); MultiSelect.defaultProps = { disabled: false }; MultiSelect.propTypes = { /** * A string to display as the placeholder text. */ placeholder: _propTypes2.default.string, /** * An array of objects which will be used as the options for the MultiSelect component. */ options: _propTypes2.default.array.isRequired, /** * The values of the options to be selected. */ value: _propTypes2.default.array, /** * Which field in the option object will be used as the value of the MultiSelect component. */ valueProp: _propTypes2.default.string.isRequired, /** * Which field in the option object will be used as the display of the MultiSelect component. */ displayProp: _propTypes2.default.string.isRequired, /** * Whether the MultiSelect component is disabled. */ disabled: _propTypes2.default.bool, /** * A callback function to be called when an option is selected. */ changeCallback: _propTypes2.default.func, /** * Optional CSS class(es) to be used for local styles (string or array of strings) */ optClass: _propTypes2.default.oneOfType([_propTypes2.default.array, _propTypes2.default.string]) }; var _initialiseProps = function _initialiseProps() { var _this2 = this; this.state = { isOpen: false, value: this.props.value || [] }; this.componentWillMount = function () { // Set state if (_this2.state.value instanceof Array && _this2.state.value.length > 0 && _this2.containsValidValue(_this2.state.value, _this2.props.options)) { _this2.setState({ selected: _this2.getSelectedOptions(_this2.state.value), value: _this2.state.value }); } // No value is passed in else { _this2.setState({ selected: [], value: [] }); } }; this.componentWillReceiveProps = function (nextProps) { if (nextProps.value !== _this2.state.value) { // Set state if (nextProps.value instanceof Array && (_this2.containsValidValue(nextProps.value, nextProps.options) || nextProps.value.length === 0)) { _this2.setState({ selected: _this2.getSelectedOptions(nextProps.value), value: nextProps.value }); } // No value is passed in else { _this2.setState({ selected: [], value: [] }); } } }; this.getIndex = function (value) { var optionIndex = -1; _this2.props.options.map(function (option, index) { if (option[_this2.props.valueProp] === value) { optionIndex = index; } }); return optionIndex; }; this.containsValidValue = function (values, options) { var isValid = false; for (var i = 0; i < values.length; i++) { if (_this2.getIndex(values[i], options) > -1) { isValid = true; } } return isValid; }; this.getSelectedOptions = function (values) { var selectedOptions = []; values.map(function (value, index) { _this2.props.options.map(function (option, index) { if (option[_this2.props.valueProp] === value) { selectedOptions.push(option); } }); }); return selectedOptions; }; this.handleChange = function (event) { // when value & option are empty it means that reset button was clicked if (event.target.value !== '' && event.target.option !== '') { var values = _this2.state.value; values.push(event.target.value); _this2.setState({ selected: _this2.getSelectedOptions(values), value: values }, function () { if (_this2.props.changeCallback) { _this2.props.changeCallback({ target: { name: _this2.props.name, value: _this2.state.value, options: _this2.state.selected } }); } }); } }; this.filterOptions = function (option) { var optionValue = option[_this2.props.valueProp]; return _this2.state.value.indexOf(optionValue) === -1; }; this.getElements = function (children) { var _props = _this2.props, options = _props.options, value = _props.value, props = _objectWithoutProperties(_props, ['options', 'value']); props.options = _this2.props.options.filter(_this2.filterOptions); props.changeCallback = _this2.handleChange; if (['WrappedTypeahead', 'Wrappedt'].indexOf(children.type.displayName) !== -1) { props.resetAfterSelection = true; props.optionsFilterPredicate = _this2.filterOptions; } return _react2.default.Children.map(children, function (child) { var _child$props = child.props, options = _child$props.options, value = _child$props.value, childProps = _objectWithoutProperties(_child$props, ['options', 'value']); return _react2.default.cloneElement(child, _extends(childProps, props)); }); }; this.onRemove = function (index) { var values = _this2.state.value.slice(); values.splice(index, 1); _this2.setState({ selected: _this2.getSelectedOptions(values), value: values }, function () { if (_this2.props.changeCallback) { _this2.props.changeCallback({ target: { name: _this2.props.name, value: _this2.state.value, options: _this2.state.selected } }); } }); }; }; exports.default = MultiSelect;