UNPKG

@heroui/form

Version:

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

120 lines (118 loc) 4.21 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/utils.ts var utils_exports = {}; __export(utils_exports, { DEFAULT_SLOT: () => DEFAULT_SLOT, useContextProps: () => useContextProps, useObjectRef: () => useObjectRef, useSlottedContext: () => useSlottedContext }); module.exports = __toCommonJS(utils_exports); 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]; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DEFAULT_SLOT, useContextProps, useObjectRef, useSlottedContext });