function _extends() { _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; }; return _extends.apply(this, arguments); } 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; } /** * * Inputs * */ import React, { useMemo, useRef } from 'react'; import PropTypes from 'prop-types'; import { get, isEmpty, isFunction, isUndefined } from 'lodash'; import { DatePicker, Checkbox, Enumeration, Error, InputNumber, InputText, Label, Select, Textarea, TimePicker, Toggle, UnknownInput } from '@buffetjs/core'; import { Description, ErrorMessage } from '@buffetjs/styles'; import DateTime from '../DateTime'; import Wrapper from './Wrapper'; /* eslint-disable react/forbid-prop-types */ var inputs = { bool: Toggle, checkbox: Checkbox, date: DatePicker, datetime: DateTime, "enum": Enumeration, number: InputNumber, text: InputText, textarea: Textarea, time: TimePicker, select: Select, email: InputText, password: InputText, search: InputText }; function Inputs(_ref) { var customInputs = _ref.customInputs, description = _ref.description, inputError = _ref.error, id = _ref.id, label = _ref.label, name = _ref.name, handleBlur = _ref.onBlur, _onChange = _ref.onChange, translatedErrors = _ref.translatedErrors, type = _ref.type, validations = _ref.validations, value = _ref.value, rest = _objectWithoutProperties(_ref, ["customInputs", "description", "error", "id", "label", "name", "onBlur", "onChange", "translatedErrors", "type", "validations", "value"]); var inputValue = useMemo(function () { var ret; switch (type) { case 'checkbox': ret = value || false; break; case 'bool': ret = value; break; case 'number': ret = isUndefined(value) ? '' : value; break; default: ret = value || ''; } return ret; }, [type, value]); var allInputs = useRef(Object.assign(inputs, customInputs)); var InputComponent = allInputs.current[type] || UnknownInput; var inputId = useMemo(function () { return id || name; }, [id, name]); var descriptionId = "description-".concat(inputId); var errorId = "error-".concat(inputId); if (get(customInputs, type, null) !== null) { return /*#__PURE__*/React.createElement(InputComponent, _extends({ description: description, error: inputError, label: label, name: name, onBlur: handleBlur, onChange: _onChange, type: type, validations: validations, value: value }, rest)); } return /*#__PURE__*/React.createElement(Error, { inputError: inputError, name: name, translatedErrors: translatedErrors, type: type, validations: validations }, function (_ref2) { var canCheck = _ref2.canCheck, onBlur = _ref2.onBlur, error = _ref2.error, dispatch = _ref2.dispatch; return /*#__PURE__*/React.createElement(Wrapper, { error: error }, type !== 'checkbox' && /*#__PURE__*/React.createElement(Label, { htmlFor: inputId }, label, isEmpty(label) && /*#__PURE__*/React.createElement(React.Fragment, null, "\xA0")), /*#__PURE__*/React.createElement(InputComponent, _extends({}, rest, { message: label // Only for the checkbox , name: name, id: inputId, "aria-describedby": "".concat(!error && description ? descriptionId : '', " ").concat(error ? errorId : ''), "aria-invalid": error ? 'true' : 'false', onBlur: isFunction(handleBlur) ? handleBlur : onBlur, onChange: function onChange(e) { if (!canCheck) { dispatch({ type: 'SET_CHECK' }); } dispatch({ type: 'SET_ERROR', error: null }); _onChange(e); }, type: type, value: inputValue })), !error && description && /*#__PURE__*/React.createElement(Description, { id: descriptionId, title: description }, description), error && /*#__PURE__*/React.createElement(ErrorMessage, { id: errorId }, error)); }); } Inputs.defaultProps = { customInputs: null, description: null, id: null, error: null, label: null, onBlur: null, onChange: function onChange() {}, translatedErrors: { date: 'This is not a date', email: 'This is not an email', string: 'This is not a string', number: 'This is not a number', json: 'This is not a JSON', max: 'This is too high', maxLength: 'This is too long', min: 'This is too small', minLength: 'This is too short', required: 'This value is required', regex: 'This does not match the format', uppercase: 'This must be a upper case string' }, validations: {}, value: null }; Inputs.propTypes = { customInputs: PropTypes.object, description: PropTypes.string, error: PropTypes.string, id: PropTypes.string, label: PropTypes.string, name: PropTypes.string.isRequired, onBlur: PropTypes.func, onChange: function onChange() {}, translatedErrors: PropTypes.objectOf(PropTypes.string), type: PropTypes.string.isRequired, validations: PropTypes.object, value: PropTypes.any }; export default Inputs;