UNPKG

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.

97 lines (96 loc) 6.24 kB
"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.isSubmitting === nextProps.isSubmitting; } function _handleClick(event, handleSubmit) { event.preventDefault(); handleSubmit(event); } // ==== STANDARD BUTTON ==== var _SubmitButton = function (props) { var textNotSubmitting = props.textNotSubmitting, textSubmitting = props.textSubmitting, isSubmitting = props.isSubmitting, children = props.children, handleSubmit = props.handleSubmit, otherProps = __rest(props, ["textNotSubmitting", "textSubmitting", "isSubmitting", "children", "handleSubmit"]); if (!children) { return (react_1.default.createElement("button", __assign({ type: "submit", onClick: function (event) { return _handleClick(event, handleSubmit); } }, otherProps), !isSubmitting ? textNotSubmitting : textSubmitting)); } return (react_1.default.createElement("button", __assign({ type: "submit", onClick: function (event) { return _handleClick(event, handleSubmit); } }, otherProps), !isSubmitting ? textNotSubmitting : textSubmitting, children)); }; var MemoSubmitButton = react_1.default.memo(_SubmitButton, memoCompare); // ==== COMPONENT BUTTON ==== var _ComponentSubmitButton = function (props) { var textNotSubmitting = props.textNotSubmitting, textSubmitting = props.textSubmitting, isSubmitting = props.isSubmitting, children = props.children, handleSubmit = props.handleSubmit, Component = props.component, otherProps = __rest(props, ["textNotSubmitting", "textSubmitting", "isSubmitting", "children", "handleSubmit", "component"]); if (Component) { if (!children) { return (react_1.default.createElement(Component, __assign({ type: "submit", role: "button", onClick: function (event) { return _handleClick(event, handleSubmit); } }, otherProps), !isSubmitting ? textNotSubmitting : textSubmitting)); } return (react_1.default.createElement(Component, __assign({ type: "submit", role: "button", onClick: function (event) { return _handleClick(event, handleSubmit); } }, otherProps), !isSubmitting ? textNotSubmitting : textSubmitting, children)); } else { return null; } }; var MemoComponentSubmitButton = react_1.default.memo(_ComponentSubmitButton, memoCompare); /** * When placed inside a form element (or React Proforma's Form component), * will submit the form data. Its display text will change from "Submit" to * "Submitting..." by default when the form is submitted, but these are configurable. * * @param {React.ComponentType=} [component] - Custom component to be used instead of a standard button. * @param {string=} [textNotSubmitting] - Text to be displayed when the form is not in a submitting state. * @param {string=} [textSubmitting] - Text to be displayed when the form is in a submitting state. * @returns {JSX.Element} JSX.Element */ exports.Submit = function (props) { // console.log('Button component re-rendering.'); var _a = react_1.default.useContext(ProformaContext_1.ProformaContext), _rPIsSubmitting = _a.isSubmitting, _rPHandleSubmit = _a.handleSubmit; var children = props.children, Component = props.component, textNotSubmitting = props.textNotSubmitting, textSubmitting = props.textSubmitting, // strip away and discard the following props if present role = props.role, type = props.type, isSubmitting = props.isSubmitting, onClick = props.onClick, handleSubmit = props.handleSubmit, // **************************************************** otherProps = __rest(props, ["children", "component", "textNotSubmitting", "textSubmitting", "role", "type", "isSubmitting", "onClick", "handleSubmit"]); if (!Component) { if (!children) { return (react_1.default.createElement(MemoSubmitButton, __assign({ textNotSubmitting: textNotSubmitting || 'Submit', textSubmitting: textSubmitting || 'Submitting...', isSubmitting: _rPIsSubmitting, handleSubmit: _rPHandleSubmit }, otherProps))); } return (react_1.default.createElement(MemoSubmitButton, __assign({ textNotSubmitting: textNotSubmitting || 'Submit', textSubmitting: textSubmitting || 'Submitting...', isSubmitting: _rPIsSubmitting, handleSubmit: _rPHandleSubmit }, otherProps), children)); } if (!children) { return (react_1.default.createElement(MemoComponentSubmitButton, __assign({ component: Component, textNotSubmitting: textNotSubmitting || 'Submit', textSubmitting: textSubmitting || 'Submitting...', isSubmitting: _rPIsSubmitting, handleSubmit: _rPHandleSubmit }, otherProps))); } return (react_1.default.createElement(MemoComponentSubmitButton, __assign({ component: Component, textNotSubmitting: textNotSubmitting || 'Submit', textSubmitting: textSubmitting || 'Submitting...', isSubmitting: _rPIsSubmitting, handleSubmit: _rPHandleSubmit }, otherProps), children)); };