UNPKG

react-conventions

Version:

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

178 lines (136 loc) 6.09 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 _react = require('react'); var _react2 = _interopRequireDefault(_react); var _OptClass = require('../internal/OptClass'); var _OptClass2 = _interopRequireDefault(_OptClass); var _Icon = require('../Icon'); var _Icon2 = _interopRequireDefault(_Icon); var _Input = require('../Input'); var _Input2 = _interopRequireDefault(_Input); 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 _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 InputList = function (_React$Component) { _inherits(InputList, _React$Component); function InputList(props) { _classCallCheck(this, InputList); var _this = _possibleConstructorReturn(this, (InputList.__proto__ || Object.getPrototypeOf(InputList)).call(this, props)); _this.generateOptionsList = function (o) { var options = o.map(function (v) { return { display: v, value: v }; }); return options; }; _this.buildStatefromProps = function (value) { if (value instanceof Array && value.length > 0) { return { value: value, options: _this.generateOptionsList(value) }; } return { value: [], options: [] }; }; _this.state = _this.buildStatefromProps(_this.props.value); _this.componentWillReceiveProps = function (nextProps) { if (nextProps.value === _this.state.value) { return; } _this.setState(_this.buildStatefromProps(nextProps.value)); }; _this.callback = function () { if (_this.props.changeCallback) { _this.props.changeCallback({ target: { name: _this.props.name, value: _this.state.value } }); } }; _this.onRemove = function (index) { var arr = _this.state.value; arr.splice(index, 1); _this.setState({ value: arr, options: _this.generateOptionsList(arr) }, _this.callback); }; _this.clearInput = function () { _this.setState({ currentValue: '' }); }; _this.updateList = function (v) { var stateValue = _this.state.value; stateValue.push(v); var options = _this.generateOptionsList(stateValue); _this.setState({ value: stateValue, options: options }, function () { _this.callback(); _this.clearInput(); }); }; _this.handleKeyPress = function (event) { if (event.charCode === 13 && event.target.value) { _this.updateList(event.target.value); } }; _this.handleKeyUp = function (event) { _this.setState({ currentValue: event.target.value }); }; _this.handleClick = function () { if (_this.state.currentValue) { _this.updateList(_this.state.currentValue); } _this._input.focus(); }; return _this; } _createClass(InputList, [{ key: 'render', value: function render() { var _this2 = this; var inputListClasses = (0, _OptClass2.default)(_style2.default, 'input-list-wrapper', this.props.optClass); return _react2.default.createElement( 'div', { className: inputListClasses }, _react2.default.createElement(_Input2.default, { placeholder: this.props.placeholder, value: this.state.currentValue, onKeyUp: this.handleKeyUp, onKeyPress: this.handleKeyPress, ref: function ref(c) { return _this2._input = c; } }), _react2.default.createElement(_Icon2.default, { name: 'icon-add-1-1', className: _style2.default['input-list-add-item'], width: '14', height: '14', fill: '#9198A0', onClick: this.handleClick }), _react2.default.createElement(_TagList2.default, { tags: this.state.options, displayProp: 'display', onRemove: this.onRemove }) ); } }]); return InputList; }(_react2.default.Component); InputList.propTypes = { /** * The values of the options to be selected. */ value: _react2.default.PropTypes.array, /** * A callback function to be called when an option is selected. */ changeCallback: _react2.default.PropTypes.func, /** * Optional CSS class(es) to be used for local styles (string or array of strings) */ optClass: _react2.default.PropTypes.oneOfType([_react2.default.PropTypes.array, _react2.default.PropTypes.string]) }; exports.default = InputList;