UNPKG

baseui-final-form

Version:

Adapter between `react-final-form` and `baseui`.

146 lines (124 loc) 5.94 kB
var _excluded = ["meta", "options", "input", "disabled", "softDefaultValue"], _excluded2 = ["meta", "options", "input", "disabled"]; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import { uniqueConcat } from './unique-concat'; export var adaptToSingleSelect = function adaptToSingleSelect(props, adaptOptions) { var _ref2; var valueKey = 'id'; var labelKey = 'label'; if (adaptOptions) { if (adaptOptions.valueKey) { valueKey = adaptOptions.valueKey; } if (adaptOptions.labelKey) { labelKey = adaptOptions.labelKey; } } var _ref = props, meta = _ref.meta, options = _ref.options, input = _ref.input, disabled = _ref.disabled, softDefaultValue = _ref.softDefaultValue, restProps = _objectWithoutProperties(_ref, _excluded); if (!options || !Array.isArray(options)) { throw new Error("Invalid options in \"".concat(input.name, "\", expects options to be Array<OptionT>.")); } var newOptions = uniqueConcat(options, input.value ? [(_ref2 = {}, _defineProperty(_ref2, valueKey, input.value), _defineProperty(_ref2, labelKey, input.value), _ref2)] : [], valueKey); // $FlowFixMe var selectedOption = newOptions.filter(function (option) { return input.value === option[valueKey]; }); var defaultOption = softDefaultValue && newOptions.find(function (option) { return softDefaultValue === option[valueKey]; }); return _objectSpread(_objectSpread({}, restProps), {}, { id: input.name, options: newOptions, disabled: disabled, multi: false, onChange: function onChange(_ref3) { var value = _ref3.value, option = _ref3.option, type = _ref3.type; if (input.onChange) { input.onChange(Array.isArray(value) && value[0] ? value[0][valueKey] : undefined); } }, onBlur: function onBlur(ev) { if (input.onBlur) { input.onBlur(ev); } }, onFocus: function onFocus(ev) { if (input.onFocus) { input.onFocus(ev); } }, value: meta.pristine && !input.value && defaultOption ? [defaultOption] : selectedOption, error: meta.error && meta.touched }); }; export var adaptToMultiSelect = function adaptToMultiSelect(props, adaptOptions) { var valueKey = 'id'; var labelKey = 'label'; if (adaptOptions) { if (adaptOptions.valueKey) { valueKey = adaptOptions.valueKey; } if (adaptOptions.labelKey) { labelKey = adaptOptions.labelKey; } } var _ref4 = props, meta = _ref4.meta, options = _ref4.options, input = _ref4.input, disabled = _ref4.disabled, restProps = _objectWithoutProperties(_ref4, _excluded2); if (!options || !Array.isArray(options)) { throw new Error("Invalid options in \"".concat(input.name, "\", expects options to be Array<OptionT>.")); } var values = Array.isArray(input.value) ? input.value : []; var newOptions = uniqueConcat(options, values.map(function (option) { var _ref5; return _ref5 = {}, _defineProperty(_ref5, valueKey, option), _defineProperty(_ref5, labelKey, option), _ref5; }), valueKey); // $FlowFixMe var selectedOption = newOptions.filter(function (option) { return values.includes(option[valueKey]); }); return _objectSpread(_objectSpread({}, restProps), {}, { id: input.name, options: newOptions, disabled: disabled, multi: true, valueKey: valueKey, labelKey: labelKey, onChange: function onChange(_ref6) { var value = _ref6.value, option = _ref6.option, type = _ref6.type; if (input.onChange) { input.onChange(Array.isArray(value) ? value.map(function (option) { return option[valueKey]; }) : undefined); newOptions.concat(value); } }, onBlur: function onBlur(ev) { if (input.onBlur) { input.onBlur(ev); } }, onFocus: function onFocus(ev) { if (input.onFocus) { input.onFocus(ev); } }, value: selectedOption, error: meta.error && meta.touched }); };