UNPKG

react-jsonschema-form-semantic

Version:

A simple React component capable of building HTML forms out of a JSON schema. Uses React Semantic UI (css not bundled)

104 lines (82 loc) 3.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _extends2 = require("babel-runtime/helpers/extends"); var _extends3 = _interopRequireDefault(_extends2); var _objectWithoutProperties2 = require("babel-runtime/helpers/objectWithoutProperties"); var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2); var _stringify = require("babel-runtime/core-js/json/stringify"); var _stringify2 = _interopRequireDefault(_stringify); var _react = require("react"); var _react2 = _interopRequireDefault(_react); var _propTypes = require("prop-types"); var _propTypes2 = _interopRequireDefault(_propTypes); var _semanticUiReact = require("semantic-ui-react"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function BaseInput(props) { // Note: since React 15.2.0 we can't forward unknown element attributes, so we // exclude the "options" and "schema" ones here. if (!props.id) { console.log("No id for", props); throw new Error("no id for props " + (0, _stringify2.default)(props)); } var value = props.value, readonly = props.readonly, disabled = props.disabled, autofocus = props.autofocus, onBlur = props.onBlur, onFocus = props.onFocus, options = props.options, schema = props.schema, formContext = props.formContext, registry = props.registry, rawErrors = props.rawErrors, label = props.label, inputProps = (0, _objectWithoutProperties3.default)(props, ["value", "readonly", "disabled", "autofocus", "onBlur", "onFocus", "options", "schema", "formContext", "registry", "rawErrors", "label"]); inputProps.type = options.inputType || inputProps.type || "text"; var _onChange = function _onChange(_ref) { var value = _ref.target.value; return props.onChange(value); }; var stringTypes = ["text", "textarea", "email", "url"]; var isStringType = stringTypes.includes(inputProps.type); var parsedValue = isStringType ? String(value || "") : value; return _react2.default.createElement(_semanticUiReact.Input, (0, _extends3.default)({ readOnly: readonly, disabled: disabled, autoFocus: autofocus, value: parsedValue, label: !!label && label }, inputProps, { onChange: _onChange, onBlur: onBlur && function (event) { return onBlur(inputProps.id, event.target.value); }, onFocus: onFocus && function (event) { return onFocus(inputProps.id, event.target.value); } })); } BaseInput.defaultProps = { type: "text", required: false, disabled: false, readonly: false, autofocus: false }; if (process.env.NODE_ENV !== "production") { BaseInput.propTypes = { id: _propTypes2.default.string.isRequired, placeholder: _propTypes2.default.string, value: _propTypes2.default.any, required: _propTypes2.default.bool, disabled: _propTypes2.default.bool, readonly: _propTypes2.default.bool, autofocus: _propTypes2.default.bool, onChange: _propTypes2.default.func, onBlur: _propTypes2.default.func, onFocus: _propTypes2.default.func }; } exports.default = BaseInput;