@nucleux/form 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nucleux
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # @nucleux/form
2
+
3
+ Accessible form primitives — Nucleux agentic UI for React.
4
+
5
+ Part of **[Nucleux](https://github.com/intelligaia-open-labs/nucleux)** — agentic UI components for React. Install a single component, or the full kit via [`@nucleux/react`](https://www.npmjs.com/package/@nucleux/react).
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pnpm add @nucleux/form @nucleux/tokens
11
+ ```
12
+
13
+ `react` / `react-dom` are peer dependencies.
14
+
15
+ ## Usage
16
+
17
+ ```tsx
18
+ import { Form } from "@nucleux/form";
19
+ ```
20
+
21
+ Requires the theme once in your app: `import "@nucleux/tokens/styles.css";`
22
+ and the Tailwind preset from `@nucleux/tokens/preset`.
23
+
24
+ ## License
25
+
26
+ MIT © Nucleux
package/dist/index.cjs ADDED
@@ -0,0 +1,115 @@
1
+ "use client";
2
+ 'use strict';
3
+
4
+ var React = require('react');
5
+ var reactHookForm = require('react-hook-form');
6
+ var utils = require('@nucleux/utils');
7
+ var jsxRuntime = require('react/jsx-runtime');
8
+
9
+ function _interopNamespace(e) {
10
+ if (e && e.__esModule) return e;
11
+ var n = Object.create(null);
12
+ if (e) {
13
+ Object.keys(e).forEach(function (k) {
14
+ if (k !== 'default') {
15
+ var d = Object.getOwnPropertyDescriptor(e, k);
16
+ Object.defineProperty(n, k, d.get ? d : {
17
+ enumerable: true,
18
+ get: function () { return e[k]; }
19
+ });
20
+ }
21
+ });
22
+ }
23
+ n.default = e;
24
+ return Object.freeze(n);
25
+ }
26
+
27
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
28
+
29
+ // src/index.tsx
30
+ var Form = reactHookForm.FormProvider;
31
+ var FormFieldContext = React__namespace.createContext({});
32
+ var FormField = (props) => /* @__PURE__ */ jsxRuntime.jsx(FormFieldContext.Provider, { value: { name: props.name }, children: /* @__PURE__ */ jsxRuntime.jsx(reactHookForm.Controller, { ...props }) });
33
+ var FormItemContext = React__namespace.createContext({});
34
+ function useFormField() {
35
+ const fieldContext = React__namespace.useContext(FormFieldContext);
36
+ const itemContext = React__namespace.useContext(FormItemContext);
37
+ const { getFieldState, formState } = reactHookForm.useFormContext();
38
+ if (!fieldContext?.name) throw new Error("useFormField must be used within a <FormField>");
39
+ const fieldState = getFieldState(fieldContext.name, formState);
40
+ const { id } = itemContext;
41
+ return {
42
+ id,
43
+ name: fieldContext.name,
44
+ formItemId: `${id}-form-item`,
45
+ formDescriptionId: `${id}-form-item-description`,
46
+ formMessageId: `${id}-form-item-message`,
47
+ ...fieldState
48
+ };
49
+ }
50
+ var FormItem = React__namespace.forwardRef(
51
+ ({ className, ...props }, ref) => {
52
+ const id = React__namespace.useId();
53
+ return /* @__PURE__ */ jsxRuntime.jsx(FormItemContext.Provider, { value: { id }, children: /* @__PURE__ */ jsxRuntime.jsx("div", { ref, className: utils.cn("space-y-2", className), ...props }) });
54
+ }
55
+ );
56
+ FormItem.displayName = "FormItem";
57
+ var FormLabel = React__namespace.forwardRef(
58
+ ({ className, ...props }, ref) => {
59
+ const { error, formItemId } = useFormField();
60
+ return /* @__PURE__ */ jsxRuntime.jsx(
61
+ "label",
62
+ {
63
+ ref,
64
+ htmlFor: formItemId,
65
+ className: utils.cn("text-sm font-medium leading-none text-foreground", error && "text-destructive", className),
66
+ ...props
67
+ }
68
+ );
69
+ }
70
+ );
71
+ FormLabel.displayName = "FormLabel";
72
+ function FormControl({ children }) {
73
+ const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
74
+ return React__namespace.cloneElement(children, {
75
+ id: formItemId,
76
+ "aria-describedby": error ? `${formDescriptionId} ${formMessageId}` : formDescriptionId,
77
+ "aria-invalid": error ? true : void 0
78
+ });
79
+ }
80
+ var FormDescription = React__namespace.forwardRef(
81
+ ({ className, ...props }, ref) => {
82
+ const { formDescriptionId } = useFormField();
83
+ return /* @__PURE__ */ jsxRuntime.jsx("p", { ref, id: formDescriptionId, className: utils.cn("text-sm text-muted-foreground", className), ...props });
84
+ }
85
+ );
86
+ FormDescription.displayName = "FormDescription";
87
+ var FormMessage = React__namespace.forwardRef(
88
+ ({ className, children, ...props }, ref) => {
89
+ const { error, formMessageId } = useFormField();
90
+ const body = error ? String(error.message ?? "") : children;
91
+ if (!body) return null;
92
+ return /* @__PURE__ */ jsxRuntime.jsx(
93
+ "p",
94
+ {
95
+ ref,
96
+ id: formMessageId,
97
+ className: utils.cn("text-sm font-medium text-destructive", className),
98
+ ...props,
99
+ children: body
100
+ }
101
+ );
102
+ }
103
+ );
104
+ FormMessage.displayName = "FormMessage";
105
+
106
+ exports.Form = Form;
107
+ exports.FormControl = FormControl;
108
+ exports.FormDescription = FormDescription;
109
+ exports.FormField = FormField;
110
+ exports.FormItem = FormItem;
111
+ exports.FormLabel = FormLabel;
112
+ exports.FormMessage = FormMessage;
113
+ exports.useFormField = useFormField;
114
+ //# sourceMappingURL=index.cjs.map
115
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.tsx"],"names":["FormProvider","React","jsx","Controller","useFormContext","cn"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYO,IAAM,IAAA,GAAOA;AAKpB,IAAM,gBAAA,GAAyBC,gBAAA,CAAA,aAAA,CAAqC,EAA2B,CAAA;AAGxF,IAAM,YAAY,CAIvB,KAAA,qBAEAC,cAAA,CAAC,gBAAA,CAAiB,UAAjB,EAA0B,KAAA,EAAO,EAAE,IAAA,EAAM,MAAM,IAAA,EAAK,EACnD,yCAACC,wBAAA,EAAA,EAAY,GAAG,OAAO,CAAA,EACzB;AAMF,IAAM,eAAA,GAAwBF,gBAAA,CAAA,aAAA,CAAoC,EAA0B,CAAA;AAGrF,SAAS,YAAA,GAAe;AAC7B,EAAA,MAAM,YAAA,GAAqBA,4BAAW,gBAAgB,CAAA;AACtD,EAAA,MAAM,WAAA,GAAoBA,4BAAW,eAAe,CAAA;AACpD,EAAA,MAAM,EAAE,aAAA,EAAe,SAAA,EAAU,GAAIG,4BAAA,EAAe;AAEpD,EAAA,IAAI,CAAC,YAAA,EAAc,IAAA,EAAM,MAAM,IAAI,MAAM,gDAAgD,CAAA;AACzF,EAAA,MAAM,UAAA,GAAa,aAAA,CAAc,YAAA,CAAa,IAAA,EAAM,SAAS,CAAA;AAC7D,EAAA,MAAM,EAAE,IAAG,GAAI,WAAA;AAEf,EAAA,OAAO;AAAA,IACL,EAAA;AAAA,IACA,MAAM,YAAA,CAAa,IAAA;AAAA,IACnB,UAAA,EAAY,GAAG,EAAE,CAAA,UAAA,CAAA;AAAA,IACjB,iBAAA,EAAmB,GAAG,EAAE,CAAA,sBAAA,CAAA;AA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G,iBAAiB,CAAA,CAAA,EAAI,aAAa,CAAA,CAAA,GAAK,iBAAA;AAAA,IACtE,cAAA,EAAgB,QAAQ,IAAA,GAAO;AAAA,GACK,CAAA;AACxC;AAGO,IAAM,eAAA,GAAwBA,gBAAA,CAAA,UAAA;AAAA,EACnC,CAAC,EAAE,SAAA,EAAW,GAAG,KAAA,IAAS,GAAA,KAAQ;AAChC,IAAA,MAAM,EAAE,iBAAA,EAAkB,GAAI,YAAA,EAAa;AAC3C,IAAA,uBACEC,cAAA,CAAC,GAAA,EAAA,EAAE,GAAA,EAAU,EAAA,EAAI,iBAAA,EAAmB,SAAA,EAAWG,QAAA,CAAG,+BAAA,EAAiC,SAAS,CAAA,EAAI,GAAG,KAAA,EAAO,CAAA;AAAA,EAE9G;AACF;AACA,eAAA,CAAgB,WAAA,GAAc,iBAAA;AAGvB,IAAM,WAAA,GAAoBJ,gBAAA,CAAA,UAAA;AAAA,EAC/B,CAAC,EAAE,SAAA,EAAW,UAAU,GAAG,KAAA,IAAS,GAAA,KAAQ;AAC1C,IAAA,MAAM,EAAE,KAAA,EAAO,aAAA,EAAc,GAAI,YAAA,EAAa;AAC9C,IAAA,MAAM,OAAO,KAAA,GAAQ,MAAA,CAAO,KAAA,CAAM,OAAA,IAAW,EAAE,CAAA,GAAI,QAAA;AACnD,IAAA,IAAI,CAAC,MAAM,OAAO,IAAA;AAClB,IAAA,uBACEC,cAAA;AAAA,MAAC,GAAA;AAAA,MAAA;AAAA,QACC,GAAA;AAAA,QACA,EAAA,EAAI,aAAA;AAAA,QACJ,SAAA,EAAWG,QAAA,CAAG,sCAAA,EAAwC,SAAS,CAAA;AAAA,QAC9D,GAAG,KAAA;AAAA,QAEH,QAAA,EAAA;AAAA;AAAA,KACH;AAAA,EAEJ;AACF;AACA,WAAA,CAAY,WAAA,GAAc,aAAA","file":"index.cjs","sourcesContent":["import * as React from \"react\";\nimport {\n Controller,\n FormProvider,\n useFormContext,\n type ControllerProps,\n type FieldPath,\n type FieldValues,\n} from \"react-hook-form\";\nimport { cn } from \"@nucleux/utils\";\n\n/** The form root — a re-export of react-hook-form's FormProvider. */\nexport const Form = FormProvider;\n\ninterface FormFieldContextValue {\n name: string;\n}\nconst FormFieldContext = React.createContext<FormFieldContextValue>({} as FormFieldContextValue);\n\n/** Connects a single field to react-hook-form and the surrounding Form context. */\nexport const FormField = <\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n>(\n props: ControllerProps<TFieldValues, TName>,\n) => (\n <FormFieldContext.Provider value={{ name: props.name }}>\n <Controller {...props} />\n </FormFieldContext.Provider>\n);\n\ninterface FormItemContextValue {\n id: string;\n}\nconst FormItemContext = React.createContext<FormItemContextValue>({} as FormItemContextValue);\n\n/** Reads field state + generated ids for the current {@link FormField}/{@link FormItem}. */\nexport function useFormField() {\n const fieldContext = React.useContext(FormFieldContext);\n const itemContext = React.useContext(FormItemContext);\n const { getFieldState, formState } = useFormContext();\n\n if (!fieldContext?.name) throw new Error(\"useFormField must be used within a <FormField>\");\n const fieldState = getFieldState(fieldContext.name, formState);\n const { id } = itemContext;\n\n return {\n id,\n name: fieldContext.name,\n formItemId: `${id}-form-item`,\n formDescriptionId: `${id}-form-item-description`,\n formMessageId: `${id}-form-item-message`,\n ...fieldState,\n };\n}\n\n/** Groups a label, control, description, and message; supplies a shared id. */\nexport const FormItem = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(\n ({ className, ...props }, ref) => {\n const id = React.useId();\n return (\n <FormItemContext.Provider value={{ id }}>\n <div ref={ref} className={cn(\"space-y-2\", className)} {...props} />\n </FormItemContext.Provider>\n );\n },\n);\nFormItem.displayName = \"FormItem\";\n\n/** A label wired to its control; turns destructive on validation error. */\nexport const FormLabel = React.forwardRef<HTMLLabelElement, React.LabelHTMLAttributes<HTMLLabelElement>>(\n ({ className, ...props }, ref) => {\n const { error, formItemId } = useFormField();\n return (\n <label\n ref={ref}\n htmlFor={formItemId}\n className={cn(\"text-sm font-medium leading-none text-foreground\", error && \"text-destructive\", className)}\n {...props}\n />\n );\n },\n);\nFormLabel.displayName = \"FormLabel\";\n\n/** Wraps the field control, injecting id + aria wiring from the field state. */\nexport function FormControl({ children }: { children: React.ReactElement }) {\n const { error, formItemId, formDescriptionId, formMessageId } = useFormField();\n return React.cloneElement(children, {\n id: formItemId,\n \"aria-describedby\": error ? `${formDescriptionId} ${formMessageId}` : formDescriptionId,\n \"aria-invalid\": error ? true : undefined,\n } as React.HTMLAttributes<HTMLElement>);\n}\n\n/** Helper text describing the field. */\nexport const FormDescription = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HTMLParagraphElement>>(\n ({ className, ...props }, ref) => {\n const { formDescriptionId } = useFormField();\n return (\n <p ref={ref} id={formDescriptionId} className={cn(\"text-sm text-muted-foreground\", className)} {...props} />\n );\n },\n);\nFormDescription.displayName = \"FormDescription\";\n\n/** Renders the field's validation error (or explicit children). */\nexport const FormMessage = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HTMLParagraphElement>>(\n ({ className, children, ...props }, ref) => {\n const { error, formMessageId } = useFormField();\n const body = error ? String(error.message ?? \"\") : children;\n if (!body) return null;\n return (\n <p\n ref={ref}\n id={formMessageId}\n className={cn(\"text-sm font-medium text-destructive\", className)}\n {...props}\n >\n {body}\n </p>\n );\n },\n);\nFormMessage.displayName = \"FormMessage\";\n"]}
@@ -0,0 +1,35 @@
1
+ import * as react_hook_form from 'react-hook-form';
2
+ import { FieldValues, FieldPath, ControllerProps } from 'react-hook-form';
3
+ import * as React from 'react';
4
+
5
+ /** The form root — a re-export of react-hook-form's FormProvider. */
6
+ declare const Form: <TFieldValues extends FieldValues, TContext = any, TTransformedValues = TFieldValues>({ children, watch, getValues, getErrors, getFieldState, setError, clearErrors, setValue, setValues, trigger, formState, resetField, reset, resetDefaultValues, handleSubmit, unregister, control, register, setFocus, subscribe, }: react_hook_form.FormProviderProps<TFieldValues, TContext, TTransformedValues>) => React.JSX.Element;
7
+ /** Connects a single field to react-hook-form and the surrounding Form context. */
8
+ declare const FormField: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(props: ControllerProps<TFieldValues, TName>) => React.JSX.Element;
9
+ /** Reads field state + generated ids for the current {@link FormField}/{@link FormItem}. */
10
+ declare function useFormField(): {
11
+ invalid: boolean;
12
+ isDirty: boolean;
13
+ isTouched: boolean;
14
+ isValidating: boolean;
15
+ error?: react_hook_form.FieldError | undefined;
16
+ id: string;
17
+ name: string;
18
+ formItemId: string;
19
+ formDescriptionId: string;
20
+ formMessageId: string;
21
+ };
22
+ /** Groups a label, control, description, and message; supplies a shared id. */
23
+ declare const FormItem: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLDivElement> & React.RefAttributes<HTMLDivElement>>;
24
+ /** A label wired to its control; turns destructive on validation error. */
25
+ declare const FormLabel: React.ForwardRefExoticComponent<React.LabelHTMLAttributes<HTMLLabelElement> & React.RefAttributes<HTMLLabelElement>>;
26
+ /** Wraps the field control, injecting id + aria wiring from the field state. */
27
+ declare function FormControl({ children }: {
28
+ children: React.ReactElement;
29
+ }): React.ReactElement<any, string | React.JSXElementConstructor<any>>;
30
+ /** Helper text describing the field. */
31
+ declare const FormDescription: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLParagraphElement> & React.RefAttributes<HTMLParagraphElement>>;
32
+ /** Renders the field's validation error (or explicit children). */
33
+ declare const FormMessage: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLParagraphElement> & React.RefAttributes<HTMLParagraphElement>>;
34
+
35
+ export { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useFormField };
@@ -0,0 +1,35 @@
1
+ import * as react_hook_form from 'react-hook-form';
2
+ import { FieldValues, FieldPath, ControllerProps } from 'react-hook-form';
3
+ import * as React from 'react';
4
+
5
+ /** The form root — a re-export of react-hook-form's FormProvider. */
6
+ declare const Form: <TFieldValues extends FieldValues, TContext = any, TTransformedValues = TFieldValues>({ children, watch, getValues, getErrors, getFieldState, setError, clearErrors, setValue, setValues, trigger, formState, resetField, reset, resetDefaultValues, handleSubmit, unregister, control, register, setFocus, subscribe, }: react_hook_form.FormProviderProps<TFieldValues, TContext, TTransformedValues>) => React.JSX.Element;
7
+ /** Connects a single field to react-hook-form and the surrounding Form context. */
8
+ declare const FormField: <TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>(props: ControllerProps<TFieldValues, TName>) => React.JSX.Element;
9
+ /** Reads field state + generated ids for the current {@link FormField}/{@link FormItem}. */
10
+ declare function useFormField(): {
11
+ invalid: boolean;
12
+ isDirty: boolean;
13
+ isTouched: boolean;
14
+ isValidating: boolean;
15
+ error?: react_hook_form.FieldError | undefined;
16
+ id: string;
17
+ name: string;
18
+ formItemId: string;
19
+ formDescriptionId: string;
20
+ formMessageId: string;
21
+ };
22
+ /** Groups a label, control, description, and message; supplies a shared id. */
23
+ declare const FormItem: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLDivElement> & React.RefAttributes<HTMLDivElement>>;
24
+ /** A label wired to its control; turns destructive on validation error. */
25
+ declare const FormLabel: React.ForwardRefExoticComponent<React.LabelHTMLAttributes<HTMLLabelElement> & React.RefAttributes<HTMLLabelElement>>;
26
+ /** Wraps the field control, injecting id + aria wiring from the field state. */
27
+ declare function FormControl({ children }: {
28
+ children: React.ReactElement;
29
+ }): React.ReactElement<any, string | React.JSXElementConstructor<any>>;
30
+ /** Helper text describing the field. */
31
+ declare const FormDescription: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLParagraphElement> & React.RefAttributes<HTMLParagraphElement>>;
32
+ /** Renders the field's validation error (or explicit children). */
33
+ declare const FormMessage: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLParagraphElement> & React.RefAttributes<HTMLParagraphElement>>;
34
+
35
+ export { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useFormField };
package/dist/index.js ADDED
@@ -0,0 +1,86 @@
1
+ "use client";
2
+ import * as React from 'react';
3
+ import { useFormContext, FormProvider, Controller } from 'react-hook-form';
4
+ import { cn } from '@nucleux/utils';
5
+ import { jsx } from 'react/jsx-runtime';
6
+
7
+ // src/index.tsx
8
+ var Form = FormProvider;
9
+ var FormFieldContext = React.createContext({});
10
+ var FormField = (props) => /* @__PURE__ */ jsx(FormFieldContext.Provider, { value: { name: props.name }, children: /* @__PURE__ */ jsx(Controller, { ...props }) });
11
+ var FormItemContext = React.createContext({});
12
+ function useFormField() {
13
+ const fieldContext = React.useContext(FormFieldContext);
14
+ const itemContext = React.useContext(FormItemContext);
15
+ const { getFieldState, formState } = useFormContext();
16
+ if (!fieldContext?.name) throw new Error("useFormField must be used within a <FormField>");
17
+ const fieldState = getFieldState(fieldContext.name, formState);
18
+ const { id } = itemContext;
19
+ return {
20
+ id,
21
+ name: fieldContext.name,
22
+ formItemId: `${id}-form-item`,
23
+ formDescriptionId: `${id}-form-item-description`,
24
+ formMessageId: `${id}-form-item-message`,
25
+ ...fieldState
26
+ };
27
+ }
28
+ var FormItem = React.forwardRef(
29
+ ({ className, ...props }, ref) => {
30
+ const id = React.useId();
31
+ return /* @__PURE__ */ jsx(FormItemContext.Provider, { value: { id }, children: /* @__PURE__ */ jsx("div", { ref, className: cn("space-y-2", className), ...props }) });
32
+ }
33
+ );
34
+ FormItem.displayName = "FormItem";
35
+ var FormLabel = React.forwardRef(
36
+ ({ className, ...props }, ref) => {
37
+ const { error, formItemId } = useFormField();
38
+ return /* @__PURE__ */ jsx(
39
+ "label",
40
+ {
41
+ ref,
42
+ htmlFor: formItemId,
43
+ className: cn("text-sm font-medium leading-none text-foreground", error && "text-destructive", className),
44
+ ...props
45
+ }
46
+ );
47
+ }
48
+ );
49
+ FormLabel.displayName = "FormLabel";
50
+ function FormControl({ children }) {
51
+ const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
52
+ return React.cloneElement(children, {
53
+ id: formItemId,
54
+ "aria-describedby": error ? `${formDescriptionId} ${formMessageId}` : formDescriptionId,
55
+ "aria-invalid": error ? true : void 0
56
+ });
57
+ }
58
+ var FormDescription = React.forwardRef(
59
+ ({ className, ...props }, ref) => {
60
+ const { formDescriptionId } = useFormField();
61
+ return /* @__PURE__ */ jsx("p", { ref, id: formDescriptionId, className: cn("text-sm text-muted-foreground", className), ...props });
62
+ }
63
+ );
64
+ FormDescription.displayName = "FormDescription";
65
+ var FormMessage = React.forwardRef(
66
+ ({ className, children, ...props }, ref) => {
67
+ const { error, formMessageId } = useFormField();
68
+ const body = error ? String(error.message ?? "") : children;
69
+ if (!body) return null;
70
+ return /* @__PURE__ */ jsx(
71
+ "p",
72
+ {
73
+ ref,
74
+ id: formMessageId,
75
+ className: cn("text-sm font-medium text-destructive", className),
76
+ ...props,
77
+ children: body
78
+ }
79
+ );
80
+ }
81
+ );
82
+ FormMessage.displayName = "FormMessage";
83
+
84
+ export { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useFormField };
85
+ //# sourceMappingURL=index.js.map
86
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.tsx"],"names":[],"mappings":";;;;;;AAYO,IAAM,IAAA,GAAO;AAKpB,IAAM,gBAAA,GAAyB,KAAA,CAAA,aAAA,CAAqC,EAA2B,CAAA;AAGxF,IAAM,YAAY,CAIvB,KAAA,qBAEA,GAAA,CAAC,gBAAA,CAAiB,UAAjB,EAA0B,KAAA,EAAO,EAAE,IAAA,EAAM,MAAM,IAAA,EAAK,EACnD,8BAAC,UAAA,EAAA,EAAY,GAAG,OAAO,CAAA,EACzB;AAMF,IAAM,eAAA,GAAwB,KAAA,CAAA,aAAA,CAAoC,EAA0B,CAAA;AAGrF,SAAS,YAAA,GAAe;AAC7B,EAAA,MAAM,YAAA,GAAqB,iBAAW,gBAAgB,CAAA;AACtD,EAAA,MAAM,WAAA,GAAoB,iBAAW,eAAe,CAAA;AACpD,EAAA,MAAM,EAAE,aA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,CAAC,MAAM,OAAO,IAAA;AAClB,IAAA,uBACE,GAAA;AAAA,MAAC,GAAA;AAAA,MAAA;AAAA,QACC,GAAA;AAAA,QACA,EAAA,EAAI,aAAA;AAAA,QACJ,SAAA,EAAW,EAAA,CAAG,sCAAA,EAAwC,SAAS,CAAA;AAAA,QAC9D,GAAG,KAAA;AAAA,QAEH,QAAA,EAAA;AAAA;AAAA,KACH;AAAA,EAEJ;AACF;AACA,WAAA,CAAY,WAAA,GAAc,aAAA","file":"index.js","sourcesContent":["import * as React from \"react\";\nimport {\n Controller,\n FormProvider,\n useFormContext,\n type ControllerProps,\n type FieldPath,\n type FieldValues,\n} from \"react-hook-form\";\nimport { cn } from \"@nucleux/utils\";\n\n/** The form root — a re-export of react-hook-form's FormProvider. */\nexport const Form = FormProvider;\n\ninterface FormFieldContextValue {\n name: string;\n}\nconst FormFieldContext = React.createContext<FormFieldContextValue>({} as FormFieldContextValue);\n\n/** Connects a single field to react-hook-form and the surrounding Form context. */\nexport const FormField = <\n TFieldValues extends FieldValues = FieldValues,\n TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,\n>(\n props: ControllerProps<TFieldValues, TName>,\n) => (\n <FormFieldContext.Provider value={{ name: props.name }}>\n <Controller {...props} />\n </FormFieldContext.Provider>\n);\n\ninterface FormItemContextValue {\n id: string;\n}\nconst FormItemContext = React.createContext<FormItemContextValue>({} as FormItemContextValue);\n\n/** Reads field state + generated ids for the current {@link FormField}/{@link FormItem}. */\nexport function useFormField() {\n const fieldContext = React.useContext(FormFieldContext);\n const itemContext = React.useContext(FormItemContext);\n const { getFieldState, formState } = useFormContext();\n\n if (!fieldContext?.name) throw new Error(\"useFormField must be used within a <FormField>\");\n const fieldState = getFieldState(fieldContext.name, formState);\n const { id } = itemContext;\n\n return {\n id,\n name: fieldContext.name,\n formItemId: `${id}-form-item`,\n formDescriptionId: `${id}-form-item-description`,\n formMessageId: `${id}-form-item-message`,\n ...fieldState,\n };\n}\n\n/** Groups a label, control, description, and message; supplies a shared id. */\nexport const FormItem = React.forwardRef<HTMLDivElement, React.HTMLAttributes<HTMLDivElement>>(\n ({ className, ...props }, ref) => {\n const id = React.useId();\n return (\n <FormItemContext.Provider value={{ id }}>\n <div ref={ref} className={cn(\"space-y-2\", className)} {...props} />\n </FormItemContext.Provider>\n );\n },\n);\nFormItem.displayName = \"FormItem\";\n\n/** A label wired to its control; turns destructive on validation error. */\nexport const FormLabel = React.forwardRef<HTMLLabelElement, React.LabelHTMLAttributes<HTMLLabelElement>>(\n ({ className, ...props }, ref) => {\n const { error, formItemId } = useFormField();\n return (\n <label\n ref={ref}\n htmlFor={formItemId}\n className={cn(\"text-sm font-medium leading-none text-foreground\", error && \"text-destructive\", className)}\n {...props}\n />\n );\n },\n);\nFormLabel.displayName = \"FormLabel\";\n\n/** Wraps the field control, injecting id + aria wiring from the field state. */\nexport function FormControl({ children }: { children: React.ReactElement }) {\n const { error, formItemId, formDescriptionId, formMessageId } = useFormField();\n return React.cloneElement(children, {\n id: formItemId,\n \"aria-describedby\": error ? `${formDescriptionId} ${formMessageId}` : formDescriptionId,\n \"aria-invalid\": error ? true : undefined,\n } as React.HTMLAttributes<HTMLElement>);\n}\n\n/** Helper text describing the field. */\nexport const FormDescription = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HTMLParagraphElement>>(\n ({ className, ...props }, ref) => {\n const { formDescriptionId } = useFormField();\n return (\n <p ref={ref} id={formDescriptionId} className={cn(\"text-sm text-muted-foreground\", className)} {...props} />\n );\n },\n);\nFormDescription.displayName = \"FormDescription\";\n\n/** Renders the field's validation error (or explicit children). */\nexport const FormMessage = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HTMLParagraphElement>>(\n ({ className, children, ...props }, ref) => {\n const { error, formMessageId } = useFormField();\n const body = error ? String(error.message ?? \"\") : children;\n if (!body) return null;\n return (\n <p\n ref={ref}\n id={formMessageId}\n className={cn(\"text-sm font-medium text-destructive\", className)}\n {...props}\n >\n {body}\n </p>\n );\n },\n);\nFormMessage.displayName = \"FormMessage\";\n"]}
package/package.json ADDED
@@ -0,0 +1,61 @@
1
+ {
2
+ "name": "@nucleux/form",
3
+ "version": "0.2.0",
4
+ "type": "module",
5
+ "sideEffects": false,
6
+ "description": "Accessible form primitives — Nucleux agentic UI for React.",
7
+ "keywords": [
8
+ "nucleux",
9
+ "react",
10
+ "ui",
11
+ "components",
12
+ "agentic",
13
+ "ai",
14
+ "tailwind",
15
+ "form"
16
+ ],
17
+ "main": "./dist/index.cjs",
18
+ "module": "./dist/index.js",
19
+ "types": "./dist/index.d.ts",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js",
24
+ "require": "./dist/index.cjs"
25
+ }
26
+ },
27
+ "files": [
28
+ "dist",
29
+ "README.md"
30
+ ],
31
+ "peerDependencies": {
32
+ "react": ">=18",
33
+ "react-dom": ">=18"
34
+ },
35
+ "dependencies": {
36
+ "react-hook-form": "^7.54.2",
37
+ "@nucleux/utils": "0.2.0"
38
+ },
39
+ "devDependencies": {
40
+ "tsup": "^8.3.5",
41
+ "typescript": "^5.7.2",
42
+ "@types/react": "^18.3.18",
43
+ "react": "^18.3.1",
44
+ "lucide-react": "^0.469.0",
45
+ "zod": "^3.24.1",
46
+ "@hookform/resolvers": "^3.10.0"
47
+ },
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "license": "MIT",
52
+ "repository": {
53
+ "type": "git",
54
+ "url": "git+https://github.com/intelligaia-open-labs/nucleux.git",
55
+ "directory": "packages/form"
56
+ },
57
+ "homepage": "https://github.com/intelligaia-open-labs/nucleux#readme",
58
+ "scripts": {
59
+ "build": "tsup"
60
+ }
61
+ }