UNPKG

@heroui/form

Version:

A form is a group of inputs that allows users submit data to a server and supports field validation errors.

142 lines (136 loc) 5.68 kB
"use client"; "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/form.tsx var form_exports = {}; __export(form_exports, { Form: () => Form3 }); module.exports = __toCommonJS(form_exports); var import_system = require("@heroui/system"); var import_react3 = require("react"); // src/base-form.tsx var import_form = require("@react-stately/form"); var import_react2 = require("react"); var import_theme = require("@heroui/theme"); // src/utils.ts var import_react = require("react"); var import_shared_utils = require("@heroui/shared-utils"); var DEFAULT_SLOT = Symbol("default"); function useObjectRef(ref) { const objRef = (0, import_react.useRef)(null); const cleanupRef = (0, import_react.useRef)(void 0); const refEffect = (0, import_react.useCallback)( (instance) => { if (typeof ref === "function") { const refCallback = ref; const refCleanup = refCallback(instance); return () => { if (typeof refCleanup === "function") { refCleanup(); } else { refCallback(null); } }; } else if (ref) { ref.current = instance; return () => { ref.current = null; }; } }, [ref] ); return (0, import_react.useMemo)( () => ({ get current() { return objRef.current; }, set current(value) { objRef.current = value; if (cleanupRef.current) { cleanupRef.current(); cleanupRef.current = void 0; } if (value != null) { cleanupRef.current = refEffect(value); } } }), [refEffect] ); } function useSlottedContext(context, slot) { let ctx = (0, import_react.useContext)(context); if (slot === null) { return null; } if (ctx && typeof ctx === "object" && "slots" in ctx && ctx.slots) { let availableSlots = new Intl.ListFormat().format(Object.keys(ctx.slots).map((p) => `"${p}"`)); if (!slot && !ctx.slots[DEFAULT_SLOT]) { throw new Error(`A slot prop is required. Valid slot names are ${availableSlots}.`); } let slotKey = slot || DEFAULT_SLOT; if (!ctx.slots[slotKey]) { throw new Error(`Invalid slot "${slot}". Valid slot names are ${availableSlots}.`); } return ctx.slots[slotKey]; } return ctx; } function useContextProps(props, ref, context) { let ctx = useSlottedContext(context, props.slot) || {}; let { ref: contextRef, ...contextProps } = ctx; let mergedRef = useObjectRef((0, import_react.useMemo)(() => (0, import_shared_utils.mergeRefs)(ref, contextRef), [ref, contextRef])); let mergedProps = (0, import_shared_utils.mergeProps)(contextProps, props); if ("style" in contextProps && contextProps.style && "style" in props && props.style) { if (typeof contextProps.style === "function" || typeof props.style === "function") { mergedProps.style = (renderProps) => { let contextStyle = typeof contextProps.style === "function" ? contextProps.style(renderProps) : contextProps.style; let defaultStyle = { ...renderProps.defaultStyle, ...contextStyle }; let style = typeof props.style === "function" ? props.style({ ...renderProps, defaultStyle }) : props.style; return { ...defaultStyle, ...style }; }; } else { mergedProps.style = { ...contextProps.style, ...props.style }; } } return [mergedProps, mergedRef]; } // src/base-form.tsx var import_jsx_runtime = require("react/jsx-runtime"); var FormContext = (0, import_react2.createContext)(null); var Form = (0, import_react2.forwardRef)(function Form2(props, ref) { [props, ref] = useContextProps(props, ref, FormContext); let { validationErrors, validationBehavior = "native", children, className, ...domProps } = props; const styles = (0, import_react2.useMemo)(() => (0, import_theme.form)({ className }), [className]); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("form", { noValidate: validationBehavior !== "native", ...domProps, ref, className: styles, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(FormContext.Provider, { value: { ...props, validationBehavior }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_form.FormValidationContext.Provider, { value: validationErrors != null ? validationErrors : {}, children }) }) }); }); // src/form.tsx var import_jsx_runtime2 = require("react/jsx-runtime"); var Form3 = (0, import_react3.forwardRef)(function Form4(props, ref) { var _a, _b; const globalContext = (0, import_system.useProviderContext)(); const validationBehavior = (_b = (_a = props.validationBehavior) != null ? _a : globalContext == null ? void 0 : globalContext.validationBehavior) != null ? _b : "native"; return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(Form, { ...props, ref, validationBehavior }); }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Form });