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.
54 lines (53 loc) • 2.78 kB
JavaScript
;
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");
/**
* Attaches the internal "handleSubmit" method from the Proforma component and adds it to a
* form element's (default or your own custom form's) onSubmit prop, returning
* the form. Place all form elements inside the Form component as children.
*
* @param {React.ComponentType=} [component] - Custom component (Note: must still be a form element)
* @param {string=} [action] - Action to be passed to form element.
* @param {string=} [method] - Method to be passed to form element
* @returns {JSX.Element | null} - JSX.Element or null (null if no children are provided)
*/
exports.Form = function (props) {
var handleSubmit = react_1.default.useContext(ProformaContext_1.ProformaContext).handleSubmit;
var children = props.children, Component = props.component,
// strip away and discard the following props if present
onSubmit = props.onSubmit, onChange = props.onChange, onInput = props.onInput, onReset = props.onReset,
// ****************************************************
otherProps = __rest(props, ["children", "component", "onSubmit", "onChange", "onInput", "onReset"]);
if (!children)
throw new Error('The Form component must contain child form elements to be of any use. Please add form elements to your form.');
if (!Component) {
return (react_1.default.createElement("form", __assign({ onSubmit: handleSubmit }, otherProps), children));
}
return (react_1.default.createElement(Component, __assign({ onSubmit: handleSubmit }, otherProps), children));
};