react-proforma
Version:
React Proforma helps you build simple to complex web forms with ease in React. -- Simplicity where you want it. Flexibility where you need it.
86 lines (85 loc) • 5.5 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var ProformaContext_1 = require("./ProformaContext");
function memoCompare(prevProps, nextProps) {
return prevProps.value === nextProps.value;
}
// ==== STANDARD CHECKBOX ====
var _Checkbox = function (props) {
var name = props.name, value = props.value, handleChange = props.handleChange, handleFocus = props.handleFocus, handleBlur = props.handleBlur, otherProps = __rest(props, ["name", "value", "handleChange", "handleFocus", "handleBlur"]);
return (react_1.default.createElement("input", __assign({ type: "checkbox", name: name, checked: value, onChange: handleChange, onFocus: handleFocus, onBlur: handleBlur }, otherProps)));
};
var MemoCheckbox = react_1.default.memo(_Checkbox, memoCompare);
// ==== COMPONENT CHECKBOX ====
var _ComponentCheckbox = function (props) {
var name = props.name, value = props.value, Component = props.component, handleChange = props.handleChange, handleFocus = props.handleFocus, handleBlur = props.handleBlur, children = props.children, otherProps = __rest(props, ["name", "value", "component", "handleChange", "handleFocus", "handleBlur", "children"]);
if (Component) {
if (!children) {
return (react_1.default.createElement(Component, __assign({ name: name, type: "checkbox", checked: value, onChange: handleChange, onFocus: handleFocus, onBlur: handleBlur }, otherProps)));
}
else {
return (react_1.default.createElement(Component, __assign({ name: name, type: "checkbox", checked: value, onChange: handleChange, onFocus: handleFocus, onBlur: handleBlur }, otherProps), children));
}
}
else {
return null;
}
};
var MemoComponentCheckbox = react_1.default.memo(_ComponentCheckbox, memoCompare);
/**
* Component to produce a checkbox form element, either with
* a standard input tag, or with your own custom component.
*
* @param {string} name - name of the form element, which MUST be the same string value as the corresponding value in your "initialValues" object.
* @param {React.ComponentType=} [component] - Optional custom component to be used instead of a standard input tag.
* @returns {JSX.Element} JSX.Element
*/
exports.Checkbox = function (props) {
var _a = react_1.default.useContext(ProformaContext_1.ProformaContext), values = _a.values, _rPHandleChange = _a.handleChange, _rPHandleFocus = _a.handleFocus, _rPHandleBlur = _a.handleBlur;
var name = props.name, Component = props.component, children = props.children,
// strip away and discard the following props if present
onChange = props.onChange, onFocus = props.onFocus, onBlur = props.onBlur, handleChange = props.handleChange, handleFocus = props.handleFocus, handleBlur = props.handleBlur, value = props.value, checked = props.checked,
// ****************************************************
otherProps = __rest(props, ["name", "component", "children", "onChange", "onFocus", "onBlur", "handleChange", "handleFocus", "handleBlur", "value", "checked"]);
if (values[name] === undefined || values[name] === null)
throw new Error('The "name" prop you passed in does not exist on the values object initialized by the "initialState" prop on the config object passed to the Proforma component.');
if (!name)
throw new Error('This component will not function without a "name" prop passed to it. Please provide a "name" prop that corresponds with one of the properties on your "initialValues" object.');
if (!Component) {
return (react_1.default.createElement(MemoCheckbox, __assign({ name: name, value: values[name], handleChange: _rPHandleChange, handleFocus: _rPHandleFocus, handleBlur: _rPHandleBlur }, otherProps)));
}
else {
if (!children) {
return (react_1.default.createElement(MemoComponentCheckbox, __assign({ component: Component, name: name, value: values[name], handleChange: _rPHandleChange, handleFocus: _rPHandleFocus, handleBlur: _rPHandleBlur }, otherProps)));
}
else {
return (react_1.default.createElement(MemoComponentCheckbox, __assign({ component: Component, name: name, value: values[name], handleChange: _rPHandleChange, handleFocus: _rPHandleFocus, handleBlur: _rPHandleBlur }, otherProps), children));
}
}
};