UNPKG

react-ions

Version:

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

102 lines (70 loc) 4.29 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.WithObjectValue = undefined; 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 _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); 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 WithObjectValue = exports.WithObjectValue = function WithObjectValue(WrappedComponent) { var _class, _temp; return _temp = _class = function (_Component) { _inherits(_class, _Component); function _class(props) { _classCallCheck(this, _class); var _this = _possibleConstructorReturn(this, (_class.__proto__ || Object.getPrototypeOf(_class)).call(this, props)); _this.flatten = function () { var inflatedValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; return inflatedValue.map(function (v) { return v[_this.props.valueProp]; }); }; _this.inflate = function () { var flatValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var inflatedValue = []; for (var index = 0; index < _this.props.options.length; index++) { var currentOption = _this.props.options[index]; // Push full option into inflatedValue if the value exists in the flatValue array if (flatValue.indexOf(currentOption[_this.props.valueProp]) !== -1) { inflatedValue.push(currentOption); } // Stop loop if all values have been found if (inflatedValue.length === flatValue.length) return inflatedValue; } return inflatedValue; }; _this.handleChanges = function (event) { if (typeof _this.props.changeCallback === 'function') { _this.props.changeCallback(_extends({}, event, { target: _extends({}, event.target, { value: _this.inflate(event.target.value) }) })); } }; _this.render = function () { var _this$props = _this.props, value = _this$props.value, changeCallback = _this$props.changeCallback, otherProps = _objectWithoutProperties(_this$props, ['value', 'changeCallback']); var flatValue = _this.flatten(value); return _react2.default.createElement(WrappedComponent, _extends({ value: flatValue, changeCallback: _this.handleChanges }, otherProps)); }; return _this; } // Convert full object values to shallow array based on value prop // Convert to shallow array to full object array by finding each valueProp in the options return _class; }(_react.Component), _class.props = { changeCallback: _propTypes.func, options: _propTypes.array, value: _propTypes.array, valueProp: _propTypes.string }, _temp; }; exports.default = WithObjectValue;