UNPKG

@bigfishtv/cockpit

Version:

149 lines (127 loc) 5.57 kB
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 _class, _temp; 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; } import PropTypes from 'prop-types'; import React, { Component } from 'react'; import classnames from 'classnames'; import { titleCase } from '../../utils/stringUtils'; function toString(value) { return value === null ? '' : String(value); } function toValue(string, options) { return string === '' ? null : options.reduce(function (acc, _ref) { var value = _ref.value; return String(value) === string ? value : acc; }, string); } var SelectInput = (_temp = _class = function (_Component) { _inherits(SelectInput, _Component); function SelectInput() { _classCallCheck(this, SelectInput); return _possibleConstructorReturn(this, _Component.apply(this, arguments)); } SelectInput.prototype.componentDidMount = function componentDidMount() { if (this.props.coerceValueToOptions) { this.coerceValueToOptions(); } }; SelectInput.prototype.componentDidUpdate = function componentDidUpdate() { if (this.props.coerceValueToOptions) { this.coerceValueToOptions(); } }; SelectInput.prototype.coerceValueToOptions = function coerceValueToOptions() { // coerce value to defaultValue if value not found in options var _props = this.props, value = _props.value, defaultValue = _props.defaultValue, empty = _props.empty; var options = this.normalisedOptions(); var selectedOption = options.find(function (option) { return option.value === value; }); if (!selectedOption) { if (options.length && empty === false) { // select first item if we have options to choose from // and we have an "empty" option this.props.onChange(options[0].value); } else if (value !== undefined && value !== null && value !== defaultValue) { // set value to defaultValue this.props.onChange(defaultValue); } } }; SelectInput.prototype.normalisedOptions = function normalisedOptions() { return this.props.options.map(function (option) { return typeof option === 'string' ? { value: option, text: titleCase(option) } : option; }); }; SelectInput.prototype.renderOptions = function renderOptions() { var empty = this.props.empty !== false && React.createElement( 'option', { value: toString(this.props.defaultValue), key: 0 }, this.props.empty ); var options = this.normalisedOptions().map(function (_ref2, i) { var value = _ref2.value, text = _ref2.text; return React.createElement( 'option', { key: i + 1, value: value }, text ); }); return empty ? [empty].concat(options) : options; }; SelectInput.prototype.render = function render() { var _this2 = this; var _props2 = this.props, options = _props2.options, defaultValue = _props2.defaultValue, empty = _props2.empty, coerceValueToOptions = _props2.coerceValueToOptions, readOnly = _props2.readOnly, onChange = _props2.onChange, value = _props2.value, className = _props2.className, formValue = _props2.formValue, invalid = _props2.invalid, rest = _objectWithoutProperties(_props2, ['options', 'defaultValue', 'empty', 'coerceValueToOptions', 'readOnly', 'onChange', 'value', 'className', 'formValue', 'invalid']); return React.createElement( 'label', { className: classnames('select', className) }, React.createElement( 'select', _extends({}, rest, { value: toString(value), disabled: readOnly, onChange: function onChange(event) { return _this2.props.onChange(toValue(event.target.value, _this2.normalisedOptions())); } }), this.renderOptions() ) ); }; return SelectInput; }(Component), _class.propTypes = { options: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.shape({ value: PropTypes.any, text: PropTypes.node })])), empty: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]), value: PropTypes.any, defaultValue: PropTypes.any, onChange: PropTypes.func, coerceValueToOptions: PropTypes.bool, className: PropTypes.string }, _class.defaultProps = { options: [], empty: 'Please select ...', defaultValue: null, onChange: function onChange() {}, coerceValueToOptions: true }, _temp); export { SelectInput as default };