@sybilion/uilib 1.4.16 → 1.4.17
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/dist/agent-glossary/workspace.generated.ts +1 -0
- package/dist/agent-glossary/workspace.index.md +1 -0
- package/dist/agent-glossary/workspace.md +1 -0
- package/dist/esm/components/ui/AppHeader/AppHeader.js +1 -1
- package/dist/esm/components/ui/Chip/Chip.js +1 -1
- package/dist/esm/components/ui/Form/Form.js +96 -0
- package/dist/esm/components/ui/Form/Form.styl.js +7 -0
- package/dist/esm/components/widgets/SybilionAppHeader/SybilionAppHeader.styl.js +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/types/src/components/ui/Form/Form.d.ts +48 -0
- package/dist/esm/types/src/components/ui/Form/Form.example.d.ts +1 -0
- package/dist/esm/types/src/components/ui/Form/Form.types.d.ts +14 -0
- package/dist/esm/types/src/components/ui/Form/index.d.ts +2 -0
- package/dist/esm/types/src/docs/pages/FormPage.d.ts +1 -0
- package/dist/esm/types/src/index.d.ts +1 -0
- package/dist/esm/types/tests/form.test.d.ts +1 -0
- package/package.json +9 -3
- package/src/components/ui/AppHeader/AppHeader.tsx +1 -0
- package/src/components/ui/Chip/Chip.tsx +3 -3
- package/src/components/ui/Form/AGENT.md +29 -0
- package/src/components/ui/Form/Form.example.tsx +193 -0
- package/src/components/ui/Form/Form.styl +12 -0
- package/src/components/ui/Form/Form.styl.d.ts +9 -0
- package/src/components/ui/Form/Form.tsx +231 -0
- package/src/components/ui/Form/Form.types.ts +32 -0
- package/src/components/ui/Form/index.ts +11 -0
- package/src/components/widgets/SybilionAppHeader/SybilionAppHeader.styl +7 -2
- package/src/docs/App/App.tsx +17 -18
- package/src/docs/pages/FormPage.tsx +21 -0
- package/src/docs/registry.ts +6 -0
- package/src/index.ts +1 -0
|
@@ -32,6 +32,7 @@ Entry point for \`@sybilion/uilib\`. Open linked AGENT.md before using a compone
|
|
|
32
32
|
- [FileChip](src/components/ui/FileChip/AGENT.md) — FileChip is a compact row that represents a file with a format icon, name, and hint text. It supports an onClick hand…
|
|
33
33
|
- [FlickeringGrid](src/components/ui/FlickeringGrid/AGENT.md) — FlickeringGrid is a purely decorative animated grid of squares that flicker at random. Place it behind content in a p…
|
|
34
34
|
- [Foldable](src/components/ui/Foldable/AGENT.md) — Foldable is a collapsible section with a clickable header that shows or hides its content. Pass the header and conten…
|
|
35
|
+
- [Form](src/components/ui/Form/AGENT.md) — Validated forms via root Form with zod schema and inline field errors
|
|
35
36
|
- [Gap](src/components/ui/Gap/AGENT.md) — Gap is a flex-grow spacer that absorbs remaining horizontal or vertical space inside a flex container. Render it betw…
|
|
36
37
|
- [GridLayout](src/components/ui/GridLayout/AGENT.md) — GridLayout is a responsive CSS grid that fits as many columns as the container allows, with each cell keeping at leas…
|
|
37
38
|
- [Image](src/components/ui/Image/AGENT.md) — Image is a thin wrapper around the native img element that loads a remote URL and accepts a fallback for when the req…
|
|
@@ -31,6 +31,7 @@ Entry point for `@sybilion/uilib`. Open linked AGENT.md before using a component
|
|
|
31
31
|
- [FileChip](src/components/ui/FileChip/AGENT.md) — FileChip is a compact row that represents a file with a format icon, name, and hint text. It supports an onClick hand…
|
|
32
32
|
- [FlickeringGrid](src/components/ui/FlickeringGrid/AGENT.md) — FlickeringGrid is a purely decorative animated grid of squares that flicker at random. Place it behind content in a p…
|
|
33
33
|
- [Foldable](src/components/ui/Foldable/AGENT.md) — Foldable is a collapsible section with a clickable header that shows or hides its content. Pass the header and conten…
|
|
34
|
+
- [Form](src/components/ui/Form/AGENT.md) — Validated forms via root Form with zod schema and inline field errors
|
|
34
35
|
- [Gap](src/components/ui/Gap/AGENT.md) — Gap is a flex-grow spacer that absorbs remaining horizontal or vertical space inside a flex container. Render it betw…
|
|
35
36
|
- [GridLayout](src/components/ui/GridLayout/AGENT.md) — GridLayout is a responsive CSS grid that fits as many columns as the container allows, with each cell keeping at leas…
|
|
36
37
|
- [Image](src/components/ui/Image/AGENT.md) — Image is a thin wrapper around the native img element that loads a remote URL and accepts a fallback for when the req…
|
|
@@ -31,6 +31,7 @@ Entry point for `@sybilion/uilib`. Open linked AGENT.md before using a component
|
|
|
31
31
|
- [FileChip](src/components/ui/FileChip/AGENT.md) — FileChip is a compact row that represents a file with a format icon, name, and hint text. It supports an onClick hand…
|
|
32
32
|
- [FlickeringGrid](src/components/ui/FlickeringGrid/AGENT.md) — FlickeringGrid is a purely decorative animated grid of squares that flicker at random. Place it behind content in a p…
|
|
33
33
|
- [Foldable](src/components/ui/Foldable/AGENT.md) — Foldable is a collapsible section with a clickable header that shows or hides its content. Pass the header and conten…
|
|
34
|
+
- [Form](src/components/ui/Form/AGENT.md) — Validated forms via root Form with zod schema and inline field errors
|
|
34
35
|
- [Gap](src/components/ui/Gap/AGENT.md) — Gap is a flex-grow spacer that absorbs remaining horizontal or vertical space inside a flex container. Render it betw…
|
|
35
36
|
- [GridLayout](src/components/ui/GridLayout/AGENT.md) — GridLayout is a responsive CSS grid that fits as many columns as the container allows, with each cell keeping at leas…
|
|
36
37
|
- [Image](src/components/ui/Image/AGENT.md) — Image is a thin wrapper around the native img element that loads a remote URL and accepts a fallback for when the req…
|
|
@@ -6,7 +6,7 @@ import S from './AppHeader.styl.js';
|
|
|
6
6
|
import { PAGE_HEADER_ID } from './appChromeAnchors.js';
|
|
7
7
|
|
|
8
8
|
function AppHeaderHost({ className, anchorId = PAGE_HEADER_ID, sticky, }) {
|
|
9
|
-
return (jsx("header", { className: cn(S.root, sticky && S.sticky, className), id: anchorId }));
|
|
9
|
+
return (jsx("header", { className: cn(S.root, sticky && S.sticky, className), "data-sticky": sticky ? 'true' : 'false', id: anchorId }));
|
|
10
10
|
}
|
|
11
11
|
function AppHeaderPortal({ children, pageHeaderId = PAGE_HEADER_ID, }) {
|
|
12
12
|
const [container, setContainer] = useState(null);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
2
|
+
import cn from 'classnames';
|
|
2
3
|
import { TrendingUp, TrendingDown } from 'lucide-react';
|
|
3
4
|
import S from './Chip.styl.js';
|
|
4
|
-
import cn from 'classnames';
|
|
5
5
|
|
|
6
6
|
function Chip({ className, variant = 'neutral', dot, trend, squared = false, children, ...props }) {
|
|
7
7
|
const TrendIcon = trend === 'up' ? TrendingUp : trend === 'down' ? TrendingDown : null;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import cn from 'classnames';
|
|
3
|
+
import { createContext, useId, useContext } from 'react';
|
|
4
|
+
import { useForm, FormProvider, useFormContext, Controller, useFormState } from 'react-hook-form';
|
|
5
|
+
import { Label } from '../Label/Label.js';
|
|
6
|
+
import { zodResolver } from '@hookform/resolvers/zod';
|
|
7
|
+
import { Slot } from '@radix-ui/react-slot';
|
|
8
|
+
import S from './Form.styl.js';
|
|
9
|
+
|
|
10
|
+
const FormFieldContext = createContext(null);
|
|
11
|
+
const FormItemContext = createContext(null);
|
|
12
|
+
/**
|
|
13
|
+
* Root validated form. Owns `useForm`, applies `zodResolver(schema)` when `schema` is
|
|
14
|
+
* passed, and renders a native `<form>` wired to `onSubmit`. Wrap fields in {@link FormField}.
|
|
15
|
+
*/
|
|
16
|
+
function Form({ schema, defaultValues, onSubmit, resolver, formOptions, form: externalForm, className, children, ...formHTMLProps }) {
|
|
17
|
+
const internalForm = useForm({
|
|
18
|
+
defaultValues,
|
|
19
|
+
resolver: resolver ??
|
|
20
|
+
(schema ? zodResolver(schema) : undefined),
|
|
21
|
+
...formOptions,
|
|
22
|
+
});
|
|
23
|
+
const methods = externalForm ?? internalForm;
|
|
24
|
+
return (jsx(FormProvider, { ...methods, children: jsx("form", { "data-slot": "form", className: cn(className), onSubmit: methods.handleSubmit(onSubmit), ...formHTMLProps, children: children }) }));
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Connects a field to react-hook-form via `Controller`. Reads `control` from the nearest
|
|
28
|
+
* {@link Form} context — do not pass `control` manually. Must wrap {@link FormItem} inside `render`.
|
|
29
|
+
*/
|
|
30
|
+
function FormField({ ...props }) {
|
|
31
|
+
const { control } = useFormContext();
|
|
32
|
+
return (jsx(FormFieldContext.Provider, { value: { name: props.name }, children: jsx(Controller, { ...props, control: control }) }));
|
|
33
|
+
}
|
|
34
|
+
/** Vertical field shell; generates stable ids for label, control, description, and message. */
|
|
35
|
+
function FormItem({ className, ...props }) {
|
|
36
|
+
const id = useId();
|
|
37
|
+
return (jsx(FormItemContext.Provider, { value: { id }, children: jsx("div", { "data-slot": "form-item", className: cn(S.item, className), ...props }) }));
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Field metadata for {@link FormLabel}, {@link FormControl}, {@link FormDescription}, and
|
|
41
|
+
* {@link FormMessage}. Requires {@link FormField} + {@link FormItem} ancestors.
|
|
42
|
+
*/
|
|
43
|
+
function useFormField() {
|
|
44
|
+
const fieldContext = useContext(FormFieldContext);
|
|
45
|
+
const itemContext = useContext(FormItemContext);
|
|
46
|
+
const { getFieldState, control } = useFormContext();
|
|
47
|
+
const formState = useFormState({ control, name: fieldContext?.name });
|
|
48
|
+
const fieldState = getFieldState(fieldContext?.name ?? '', formState);
|
|
49
|
+
if (!fieldContext) {
|
|
50
|
+
throw new Error('useFormField should be used within <FormField>');
|
|
51
|
+
}
|
|
52
|
+
if (!itemContext) {
|
|
53
|
+
throw new Error('useFormField should be used within <FormItem>');
|
|
54
|
+
}
|
|
55
|
+
const { id } = itemContext;
|
|
56
|
+
return {
|
|
57
|
+
id,
|
|
58
|
+
name: fieldContext.name,
|
|
59
|
+
formItemId: `${id}-form-item`,
|
|
60
|
+
formDescriptionId: `${id}-form-item-description`,
|
|
61
|
+
formMessageId: `${id}-form-item-message`,
|
|
62
|
+
...fieldState,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/** Accessible label linked to the control id from {@link FormItem}. */
|
|
66
|
+
function FormLabel({ className, ...props }) {
|
|
67
|
+
const { formItemId } = useFormField();
|
|
68
|
+
return (jsx(Label, { "data-slot": "form-label", className: cn(className), htmlFor: formItemId, ...props }));
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Slot wrapper for the actual input. Forwards `id`, `aria-invalid`, and `aria-describedby`
|
|
72
|
+
* so uilib controls show validation styling (P-07 inline errors).
|
|
73
|
+
*/
|
|
74
|
+
function FormControl({ ...props }) {
|
|
75
|
+
const { error, formItemId, formDescriptionId, formMessageId } = useFormField();
|
|
76
|
+
return (jsx(Slot, { "data-slot": "form-control", id: formItemId, "aria-describedby": error ? `${formDescriptionId} ${formMessageId}` : formDescriptionId, "aria-invalid": !!error, ...props }));
|
|
77
|
+
}
|
|
78
|
+
/** Optional helper text below the label; referenced by `aria-describedby` on {@link FormControl}. */
|
|
79
|
+
function FormDescription({ className, ...props }) {
|
|
80
|
+
const { formDescriptionId } = useFormField();
|
|
81
|
+
return (jsx("p", { "data-slot": "form-description", id: formDescriptionId, className: cn(S.description, className), ...props }));
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Inline validation message (`role="alert"`). Shows `fieldState.error.message` when present;
|
|
85
|
+
* omit for empty fields — never use toast for field errors.
|
|
86
|
+
*/
|
|
87
|
+
function FormMessage({ className, children, ...props }) {
|
|
88
|
+
const { error, formMessageId } = useFormField();
|
|
89
|
+
const body = error ? String(error?.message ?? '') : children;
|
|
90
|
+
if (!body) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
return (jsx("p", { "data-slot": "form-message", id: formMessageId, role: "alert", className: cn(S.message, className), ...props, children: body }));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useFormField };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import styleInject from 'style-inject';
|
|
2
|
+
|
|
3
|
+
var css_248z = ".Form_item__zPDBr{display:flex;flex-direction:column;gap:var(--spacing-xs)}.Form_message__2ch7w{color:var(--destructive);font-size:var(--text-sm)}.Form_description__rVj8c{color:var(--muted-foreground);font-size:var(--text-sm)}";
|
|
4
|
+
var S = {"item":"Form_item__zPDBr","message":"Form_message__2ch7w","description":"Form_description__rVj8c"};
|
|
5
|
+
styleInject(css_248z);
|
|
6
|
+
|
|
7
|
+
export { S as default };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import styleInject from 'style-inject';
|
|
2
2
|
|
|
3
|
-
var css_248z = "@media (max-width:768px){:root{--page-x-padding:var(--p-6);--page-y-padding:var(--p-6)}}.SybilionAppHeader_actionsAnchor__ress2{align-items:center;display:flex;flex-shrink:0;gap:var(--p-2)}.SybilionAppHeader_pageActionsPortal__9C5ww{align-items:center;display:flex;flex-shrink:0;gap:var(--p-1)}.SybilionAppHeader_pageActionsPortal__9C5ww:empty{display:none}.SybilionAppHeader_logoArea__3HAhG{align-items:center;display:flex;gap:var(--p-2);left:40px;position:absolute;top:22px;z-index:10}@media (max-width:768px){.SybilionAppHeader_logoArea__3HAhG{left:22px}}@media (min-width:768px){[data-slot=sidebar-wrapper][data-state=expanded]:not([data-slot=app-shell] [data-slot=sidebar-wrapper]) [data-slot=app-shell]:not([data-slot=app-shell] [data-slot=app-shell]) .SybilionAppHeader_logoArea__3HAhG:not([data-slot=app-shell] [data-slot=app-shell] *){left:calc(var(--sidebar-width)*-1 + 52px);position:absolute}[data-slot=app-shell] [data-slot=sidebar-wrapper][data-state=expanded] [data-slot=app-shell] .SybilionAppHeader_logoArea__3HAhG{left:calc(var(--sidebar-width)*-1 + 44px);position:absolute}[data-slot=app-shell] [data-slot=sidebar-wrapper][data-state=collapsed] [data-slot=app-shell] .SybilionAppHeader_logoArea__3HAhG{left:var(--p-8);position:absolute}}.SybilionAppHeader_logoLink__bH-KX{align-items:center;color:var(--color-foreground);display:flex;font-size:1.5rem;font-weight:400;gap:.5rem;text-decoration:none;white-space:nowrap;width:-moz-fit-content;width:fit-content}.SybilionAppHeader_logoLink__bH-KX svg{display:inline-flex;flex-shrink:0;height:32px;transition:transform .1s ease-in-out;width:auto}.SybilionAppHeader_logoLink__bH-KX:hover svg{transform:scale(1.05)}";
|
|
3
|
+
var css_248z = "@media (max-width:768px){:root{--page-x-padding:var(--p-6);--page-y-padding:var(--p-6)}}.SybilionAppHeader_actionsAnchor__ress2{align-items:center;display:flex;flex-shrink:0;gap:var(--p-2)}.SybilionAppHeader_pageActionsPortal__9C5ww{align-items:center;display:flex;flex-shrink:0;gap:var(--p-1)}.SybilionAppHeader_pageActionsPortal__9C5ww:empty{display:none}.SybilionAppHeader_logoArea__3HAhG{align-items:center;display:flex;gap:var(--p-2);left:40px;position:absolute;top:22px;z-index:10}@media (max-width:768px){.SybilionAppHeader_logoArea__3HAhG{left:22px}}@media (min-width:768px){[data-slot=sidebar-wrapper][data-state=expanded]:not([data-slot=app-shell] [data-slot=sidebar-wrapper]) [data-slot=app-shell]:not([data-slot=app-shell] [data-slot=app-shell]) header[data-sticky=false] .SybilionAppHeader_logoArea__3HAhG:not([data-slot=app-shell] [data-slot=app-shell] *){left:52px;position:fixed;top:22px;top:calc(22px + var(--app-banner-height, 0px))}[data-slot=sidebar-wrapper][data-state=expanded]:not([data-slot=app-shell] [data-slot=sidebar-wrapper]) [data-slot=app-shell]:not([data-slot=app-shell] [data-slot=app-shell]) header[data-sticky=true] .SybilionAppHeader_logoArea__3HAhG:not([data-slot=app-shell] [data-slot=app-shell] *){left:calc(var(--sidebar-width)*-1 + 52px);position:absolute}[data-slot=app-shell] [data-slot=sidebar-wrapper][data-state=expanded] [data-slot=app-shell] .SybilionAppHeader_logoArea__3HAhG{left:calc(var(--sidebar-width)*-1 + 44px);position:absolute}[data-slot=app-shell] [data-slot=sidebar-wrapper][data-state=collapsed] [data-slot=app-shell] .SybilionAppHeader_logoArea__3HAhG{left:var(--p-8);position:absolute}}.SybilionAppHeader_logoLink__bH-KX{align-items:center;color:var(--color-foreground);display:flex;font-size:1.5rem;font-weight:400;gap:.5rem;text-decoration:none;white-space:nowrap;width:-moz-fit-content;width:fit-content}.SybilionAppHeader_logoLink__bH-KX svg{display:inline-flex;flex-shrink:0;height:32px;transition:transform .1s ease-in-out;width:auto}.SybilionAppHeader_logoLink__bH-KX:hover svg{transform:scale(1.05)}";
|
|
4
4
|
var S = {"actionsAnchor":"SybilionAppHeader_actionsAnchor__ress2","pageActionsPortal":"SybilionAppHeader_pageActionsPortal__9C5ww","logoArea":"SybilionAppHeader_logoArea__3HAhG","logoLink":"SybilionAppHeader_logoLink__bH-KX"};
|
|
5
5
|
styleInject(css_248z);
|
|
6
6
|
|
package/dist/esm/index.js
CHANGED
|
@@ -54,6 +54,7 @@ export { DropZone } from './components/ui/DropZone/DropZone.js';
|
|
|
54
54
|
export { FileChip } from './components/ui/FileChip/FileChip.js';
|
|
55
55
|
export { FlickeringGrid } from './components/ui/FlickeringGrid/FlickeringGrid.js';
|
|
56
56
|
export { Foldable } from './components/ui/Foldable/Foldable.js';
|
|
57
|
+
export { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useFormField } from './components/ui/Form/Form.js';
|
|
57
58
|
export { Gap } from './components/ui/Gap/Gap.js';
|
|
58
59
|
export { GridLayout } from './components/ui/GridLayout/GridLayout.js';
|
|
59
60
|
export { Image } from './components/ui/Image/Image.js';
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { type ComponentProps, type HTMLAttributes } from 'react';
|
|
2
|
+
import { type FieldPath, type FieldValues } from 'react-hook-form';
|
|
3
|
+
import { Label } from '#uilib/components/ui/Label';
|
|
4
|
+
import { Slot } from '@radix-ui/react-slot';
|
|
5
|
+
import type { FormFieldProps, FormProps } from './Form.types';
|
|
6
|
+
/**
|
|
7
|
+
* Root validated form. Owns `useForm`, applies `zodResolver(schema)` when `schema` is
|
|
8
|
+
* passed, and renders a native `<form>` wired to `onSubmit`. Wrap fields in {@link FormField}.
|
|
9
|
+
*/
|
|
10
|
+
declare function Form<TFieldValues extends FieldValues>({ schema, defaultValues, onSubmit, resolver, formOptions, form: externalForm, className, children, ...formHTMLProps }: FormProps<TFieldValues>): import("react/jsx-runtime").JSX.Element;
|
|
11
|
+
/**
|
|
12
|
+
* Connects a field to react-hook-form via `Controller`. Reads `control` from the nearest
|
|
13
|
+
* {@link Form} context — do not pass `control` manually. Must wrap {@link FormItem} inside `render`.
|
|
14
|
+
*/
|
|
15
|
+
declare function FormField<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>>({ ...props }: FormFieldProps<TFieldValues, TName>): import("react/jsx-runtime").JSX.Element;
|
|
16
|
+
/** Vertical field shell; generates stable ids for label, control, description, and message. */
|
|
17
|
+
declare function FormItem({ className, ...props }: HTMLAttributes<HTMLDivElement>): import("react/jsx-runtime").JSX.Element;
|
|
18
|
+
/**
|
|
19
|
+
* Field metadata for {@link FormLabel}, {@link FormControl}, {@link FormDescription}, and
|
|
20
|
+
* {@link FormMessage}. Requires {@link FormField} + {@link FormItem} ancestors.
|
|
21
|
+
*/
|
|
22
|
+
declare function useFormField(): {
|
|
23
|
+
invalid: boolean;
|
|
24
|
+
isDirty: boolean;
|
|
25
|
+
isTouched: boolean;
|
|
26
|
+
isValidating: boolean;
|
|
27
|
+
error?: import("react-hook-form").FieldError;
|
|
28
|
+
id: string;
|
|
29
|
+
name: string;
|
|
30
|
+
formItemId: string;
|
|
31
|
+
formDescriptionId: string;
|
|
32
|
+
formMessageId: string;
|
|
33
|
+
};
|
|
34
|
+
/** Accessible label linked to the control id from {@link FormItem}. */
|
|
35
|
+
declare function FormLabel({ className, ...props }: ComponentProps<typeof Label>): import("react/jsx-runtime").JSX.Element;
|
|
36
|
+
/**
|
|
37
|
+
* Slot wrapper for the actual input. Forwards `id`, `aria-invalid`, and `aria-describedby`
|
|
38
|
+
* so uilib controls show validation styling (P-07 inline errors).
|
|
39
|
+
*/
|
|
40
|
+
declare function FormControl({ ...props }: ComponentProps<typeof Slot>): import("react/jsx-runtime").JSX.Element;
|
|
41
|
+
/** Optional helper text below the label; referenced by `aria-describedby` on {@link FormControl}. */
|
|
42
|
+
declare function FormDescription({ className, ...props }: HTMLAttributes<HTMLParagraphElement>): import("react/jsx-runtime").JSX.Element;
|
|
43
|
+
/**
|
|
44
|
+
* Inline validation message (`role="alert"`). Shows `fieldState.error.message` when present;
|
|
45
|
+
* omit for empty fields — never use toast for field errors.
|
|
46
|
+
*/
|
|
47
|
+
declare function FormMessage({ className, children, ...props }: HTMLAttributes<HTMLParagraphElement>): import("react/jsx-runtime").JSX.Element;
|
|
48
|
+
export { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, useFormField, };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function FormExample(): import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ComponentPropsWithoutRef, ReactNode } from 'react';
|
|
2
|
+
import type { ControllerProps, DefaultValues, FieldPath, FieldValues, Resolver, SubmitHandler, UseFormProps, UseFormReturn } from 'react-hook-form';
|
|
3
|
+
import type { z } from 'zod';
|
|
4
|
+
export type FormProps<TFieldValues extends FieldValues = FieldValues> = Omit<ComponentPropsWithoutRef<'form'>, 'onSubmit' | 'children'> & {
|
|
5
|
+
schema?: z.ZodType<TFieldValues>;
|
|
6
|
+
defaultValues?: DefaultValues<TFieldValues>;
|
|
7
|
+
onSubmit: SubmitHandler<TFieldValues>;
|
|
8
|
+
resolver?: Resolver<TFieldValues>;
|
|
9
|
+
formOptions?: Omit<UseFormProps<TFieldValues>, 'defaultValues' | 'resolver'>;
|
|
10
|
+
form?: UseFormReturn<TFieldValues>;
|
|
11
|
+
children?: ReactNode;
|
|
12
|
+
className?: string;
|
|
13
|
+
};
|
|
14
|
+
export type FormFieldProps<TFieldValues extends FieldValues = FieldValues, TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>> = Omit<ControllerProps<TFieldValues, TName>, 'control'>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function FormPage(): import("react/jsx-runtime").JSX.Element;
|
|
@@ -35,6 +35,7 @@ export * from './components/ui/DropZone/DropZone';
|
|
|
35
35
|
export * from './components/ui/FileChip';
|
|
36
36
|
export * from './components/ui/FlickeringGrid';
|
|
37
37
|
export * from './components/ui/Foldable';
|
|
38
|
+
export * from './components/ui/Form';
|
|
38
39
|
export * from './components/ui/Gap/Gap';
|
|
39
40
|
export * from './components/ui/GridLayout/GridLayout';
|
|
40
41
|
export * from './components/ui/Image';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import '@testing-library/jest-dom';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sybilion/uilib",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.17",
|
|
4
4
|
"description": "Sybilion Design System — React UI components (Webpack + Stylus)",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public",
|
|
@@ -125,11 +125,14 @@
|
|
|
125
125
|
},
|
|
126
126
|
"peerDependencies": {
|
|
127
127
|
"@auth0/auth0-react": "^2.3.1",
|
|
128
|
+
"@hookform/resolvers": "^5.0.0",
|
|
128
129
|
"@sybilion/platform-sdk": ">=0.0.1",
|
|
129
130
|
"react": ">=18.0.0",
|
|
130
131
|
"react-dom": ">=18.0.0",
|
|
132
|
+
"react-hook-form": "^7.55.0",
|
|
131
133
|
"react-router-dom": ">=6.0.0",
|
|
132
|
-
"vite": "^6.0.0"
|
|
134
|
+
"vite": "^6.0.0",
|
|
135
|
+
"zod": "^4.0.0"
|
|
133
136
|
},
|
|
134
137
|
"peerDependenciesMeta": {
|
|
135
138
|
"@auth0/auth0-react": {
|
|
@@ -147,6 +150,7 @@
|
|
|
147
150
|
"@babel/core": "^7.20.12",
|
|
148
151
|
"@babel/preset-typescript": "^7.21.0",
|
|
149
152
|
"@homecode/ui": "^4.30.6",
|
|
153
|
+
"@hookform/resolvers": "^5.0.0",
|
|
150
154
|
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.10",
|
|
151
155
|
"@rollup/plugin-alias": "^6.0.0",
|
|
152
156
|
"@rollup/plugin-commonjs": "^24.0.1",
|
|
@@ -187,6 +191,7 @@
|
|
|
187
191
|
"prettier": "^3.6.2",
|
|
188
192
|
"react": "^19.0.0",
|
|
189
193
|
"react-dom": "^19.1.1",
|
|
194
|
+
"react-hook-form": "^7.55.0",
|
|
190
195
|
"react-markdown": "^10.1.0",
|
|
191
196
|
"react-refresh": "^0.17.0",
|
|
192
197
|
"react-router-dom": "^6.28.0",
|
|
@@ -206,7 +211,8 @@
|
|
|
206
211
|
"vite": "^6.0.0",
|
|
207
212
|
"webpack": "^5.75.0",
|
|
208
213
|
"webpack-cli": "^5.0.1",
|
|
209
|
-
"webpack-dev-server": "^5.2.3"
|
|
214
|
+
"webpack-dev-server": "^5.2.3",
|
|
215
|
+
"zod": "^4.0.0"
|
|
210
216
|
},
|
|
211
217
|
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
|
|
212
218
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import cn from 'classnames';
|
|
2
|
+
|
|
1
3
|
import { TrendingDown, TrendingUp } from 'lucide-react';
|
|
2
4
|
|
|
3
|
-
import { ChipProps } from './Chip.types';
|
|
4
5
|
import S from './Chip.styl';
|
|
5
|
-
import
|
|
6
|
+
import { ChipProps } from './Chip.types';
|
|
6
7
|
|
|
7
8
|
function Chip({
|
|
8
9
|
className,
|
|
@@ -15,7 +16,6 @@ function Chip({
|
|
|
15
16
|
}: ChipProps) {
|
|
16
17
|
const TrendIcon =
|
|
17
18
|
trend === 'up' ? TrendingUp : trend === 'down' ? TrendingDown : null;
|
|
18
|
-
|
|
19
19
|
return (
|
|
20
20
|
<span
|
|
21
21
|
data-slot="chip"
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
---
|
|
2
|
+
summary: Validated forms via root Form with zod schema and inline field errors
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
Root `<Form>` owns schema validation, default values, and submit handling — it wraps react-hook-form and zodResolver internally and renders the native `<form>` element. Pair field primitives with uilib controls for inline validation errors.
|
|
6
|
+
|
|
7
|
+
## When to use
|
|
8
|
+
|
|
9
|
+
- Demo forms validated with a zod schema.
|
|
10
|
+
- Controls that need `Controller` wiring (Select, Checkbox, custom inputs).
|
|
11
|
+
- Inline field errors after submit or blur.
|
|
12
|
+
|
|
13
|
+
## When not to use
|
|
14
|
+
|
|
15
|
+
- Simple label-plus-input rows without validation — use `LabeledInput`.
|
|
16
|
+
- Page-level or session errors — use `Banner`.
|
|
17
|
+
- Success or info feedback — use Sonner toasts, not field errors.
|
|
18
|
+
|
|
19
|
+
## Do
|
|
20
|
+
|
|
21
|
+
- Pass `schema`, `defaultValues`, and `onSubmit` to the root `Form`.
|
|
22
|
+
- Wrap each control in `FormControl` inside `FormItem`.
|
|
23
|
+
- Show validation messages with `FormMessage`.
|
|
24
|
+
|
|
25
|
+
## Don't
|
|
26
|
+
|
|
27
|
+
- Call `useForm` or `zodResolver` in page code — the root `Form` handles that.
|
|
28
|
+
- Use `toast.error` for field validation failures.
|
|
29
|
+
- Render manual error `<p>` elements outside `FormMessage`.
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import { Button } from '#uilib/components/ui/Button';
|
|
4
|
+
import { Checkbox } from '#uilib/components/ui/Checkbox';
|
|
5
|
+
import { Dialog } from '#uilib/components/ui/Dialog';
|
|
6
|
+
import { Input } from '#uilib/components/ui/Input';
|
|
7
|
+
import { PageContentSection } from '#uilib/components/ui/Page';
|
|
8
|
+
import {
|
|
9
|
+
Select,
|
|
10
|
+
SelectContent,
|
|
11
|
+
SelectItem,
|
|
12
|
+
SelectTrigger,
|
|
13
|
+
SelectValue,
|
|
14
|
+
} from '#uilib/components/ui/Select';
|
|
15
|
+
import { Switch } from '#uilib/components/ui/Switch';
|
|
16
|
+
import { z } from 'zod';
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
Form,
|
|
20
|
+
FormControl,
|
|
21
|
+
FormField,
|
|
22
|
+
FormItem,
|
|
23
|
+
FormLabel,
|
|
24
|
+
FormMessage,
|
|
25
|
+
} from './Form';
|
|
26
|
+
|
|
27
|
+
const REGION_OPTIONS = [
|
|
28
|
+
{ value: 'eu-west', label: 'EU West' },
|
|
29
|
+
{ value: 'us-east', label: 'US East' },
|
|
30
|
+
{ value: 'apac', label: 'APAC' },
|
|
31
|
+
] as const;
|
|
32
|
+
|
|
33
|
+
const schema = z.object({
|
|
34
|
+
name: z.string().min(1, 'Enter a name.'),
|
|
35
|
+
notes: z.string().optional(),
|
|
36
|
+
region: z.string().min(1, 'Pick a region.'),
|
|
37
|
+
notifications: z.boolean(),
|
|
38
|
+
termsAccepted: z.boolean().refine(value => value, {
|
|
39
|
+
message: 'Accept the terms to continue.',
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
type FormValues = z.infer<typeof schema>;
|
|
44
|
+
|
|
45
|
+
const defaultValues: FormValues = {
|
|
46
|
+
name: '',
|
|
47
|
+
notes: '',
|
|
48
|
+
region: '',
|
|
49
|
+
notifications: false,
|
|
50
|
+
termsAccepted: false,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export function FormExample() {
|
|
54
|
+
const [dialogOpen, setDialogOpen] = useState(false);
|
|
55
|
+
const [submittedValues, setSubmittedValues] = useState<FormValues | null>(
|
|
56
|
+
null,
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
const handleSubmit = (values: FormValues) => {
|
|
60
|
+
setSubmittedValues(values);
|
|
61
|
+
setDialogOpen(true);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
return (
|
|
65
|
+
<PageContentSection>
|
|
66
|
+
<Form
|
|
67
|
+
schema={schema}
|
|
68
|
+
defaultValues={defaultValues}
|
|
69
|
+
onSubmit={handleSubmit}
|
|
70
|
+
style={{
|
|
71
|
+
display: 'flex',
|
|
72
|
+
flexDirection: 'column',
|
|
73
|
+
gap: '1.25rem',
|
|
74
|
+
width: '300px',
|
|
75
|
+
}}
|
|
76
|
+
>
|
|
77
|
+
<FormField
|
|
78
|
+
name="name"
|
|
79
|
+
render={({ field }) => (
|
|
80
|
+
<FormItem>
|
|
81
|
+
<FormLabel>Name</FormLabel>
|
|
82
|
+
<FormControl>
|
|
83
|
+
<Input placeholder="Your name" {...field} />
|
|
84
|
+
</FormControl>
|
|
85
|
+
<FormMessage />
|
|
86
|
+
</FormItem>
|
|
87
|
+
)}
|
|
88
|
+
/>
|
|
89
|
+
|
|
90
|
+
<FormField
|
|
91
|
+
name="notes"
|
|
92
|
+
render={({ field }) => (
|
|
93
|
+
<FormItem>
|
|
94
|
+
<FormLabel>Notes</FormLabel>
|
|
95
|
+
<FormControl>
|
|
96
|
+
<Input
|
|
97
|
+
type="textarea"
|
|
98
|
+
placeholder="Optional notes"
|
|
99
|
+
{...field}
|
|
100
|
+
/>
|
|
101
|
+
</FormControl>
|
|
102
|
+
<FormMessage />
|
|
103
|
+
</FormItem>
|
|
104
|
+
)}
|
|
105
|
+
/>
|
|
106
|
+
|
|
107
|
+
<FormField
|
|
108
|
+
name="region"
|
|
109
|
+
render={({ field }) => (
|
|
110
|
+
<FormItem>
|
|
111
|
+
<FormLabel>Region</FormLabel>
|
|
112
|
+
<Select
|
|
113
|
+
onValueChange={field.onChange}
|
|
114
|
+
value={field.value || undefined}
|
|
115
|
+
>
|
|
116
|
+
<FormControl>
|
|
117
|
+
<SelectTrigger style={{ width: '100%' }}>
|
|
118
|
+
<SelectValue placeholder="Pick a region" />
|
|
119
|
+
</SelectTrigger>
|
|
120
|
+
</FormControl>
|
|
121
|
+
<SelectContent>
|
|
122
|
+
{REGION_OPTIONS.map(option => (
|
|
123
|
+
<SelectItem key={option.value} value={option.value}>
|
|
124
|
+
{option.label}
|
|
125
|
+
</SelectItem>
|
|
126
|
+
))}
|
|
127
|
+
</SelectContent>
|
|
128
|
+
</Select>
|
|
129
|
+
<FormMessage />
|
|
130
|
+
</FormItem>
|
|
131
|
+
)}
|
|
132
|
+
/>
|
|
133
|
+
|
|
134
|
+
<FormField
|
|
135
|
+
name="notifications"
|
|
136
|
+
render={({ field }) => (
|
|
137
|
+
<FormItem>
|
|
138
|
+
<FormLabel>Email notifications</FormLabel>
|
|
139
|
+
<FormControl>
|
|
140
|
+
<Switch
|
|
141
|
+
checked={field.value}
|
|
142
|
+
onCheckedChange={field.onChange}
|
|
143
|
+
/>
|
|
144
|
+
</FormControl>
|
|
145
|
+
<FormMessage />
|
|
146
|
+
</FormItem>
|
|
147
|
+
)}
|
|
148
|
+
/>
|
|
149
|
+
|
|
150
|
+
<FormField
|
|
151
|
+
name="termsAccepted"
|
|
152
|
+
render={({ field }) => (
|
|
153
|
+
<FormItem>
|
|
154
|
+
<FormLabel>Terms</FormLabel>
|
|
155
|
+
<FormControl>
|
|
156
|
+
<Checkbox
|
|
157
|
+
checked={field.value}
|
|
158
|
+
onCheckedChange={field.onChange}
|
|
159
|
+
/>
|
|
160
|
+
</FormControl>
|
|
161
|
+
<FormMessage />
|
|
162
|
+
</FormItem>
|
|
163
|
+
)}
|
|
164
|
+
/>
|
|
165
|
+
|
|
166
|
+
<Button type="submit">Submit</Button>
|
|
167
|
+
</Form>
|
|
168
|
+
|
|
169
|
+
<Dialog
|
|
170
|
+
open={dialogOpen}
|
|
171
|
+
onOpenChange={setDialogOpen}
|
|
172
|
+
title="Submitted values"
|
|
173
|
+
content={
|
|
174
|
+
<pre
|
|
175
|
+
style={{
|
|
176
|
+
margin: 0,
|
|
177
|
+
whiteSpace: 'pre-wrap',
|
|
178
|
+
fontFamily: 'var(--font-mono, monospace)',
|
|
179
|
+
fontSize: 'var(--text-sm)',
|
|
180
|
+
}}
|
|
181
|
+
>
|
|
182
|
+
{submittedValues ? JSON.stringify(submittedValues, null, 2) : ''}
|
|
183
|
+
</pre>
|
|
184
|
+
}
|
|
185
|
+
footer={
|
|
186
|
+
<Button type="button" onClick={() => setDialogOpen(false)}>
|
|
187
|
+
Close
|
|
188
|
+
</Button>
|
|
189
|
+
}
|
|
190
|
+
/>
|
|
191
|
+
</PageContentSection>
|
|
192
|
+
);
|
|
193
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import cn from 'classnames';
|
|
2
|
+
import {
|
|
3
|
+
type ComponentProps,
|
|
4
|
+
type HTMLAttributes,
|
|
5
|
+
createContext,
|
|
6
|
+
useContext,
|
|
7
|
+
useId,
|
|
8
|
+
} from 'react';
|
|
9
|
+
import {
|
|
10
|
+
Controller,
|
|
11
|
+
type FieldPath,
|
|
12
|
+
type FieldValues,
|
|
13
|
+
FormProvider,
|
|
14
|
+
type Resolver,
|
|
15
|
+
type UseFormReturn,
|
|
16
|
+
useForm,
|
|
17
|
+
useFormContext,
|
|
18
|
+
useFormState,
|
|
19
|
+
} from 'react-hook-form';
|
|
20
|
+
|
|
21
|
+
import { Label } from '#uilib/components/ui/Label';
|
|
22
|
+
import { zodResolver } from '@hookform/resolvers/zod';
|
|
23
|
+
import { Slot } from '@radix-ui/react-slot';
|
|
24
|
+
|
|
25
|
+
import S from './Form.styl';
|
|
26
|
+
import type { FormFieldProps, FormProps } from './Form.types';
|
|
27
|
+
|
|
28
|
+
type FormFieldContextValue<
|
|
29
|
+
TFieldValues extends FieldValues = FieldValues,
|
|
30
|
+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
31
|
+
> = {
|
|
32
|
+
name: TName;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const FormFieldContext = createContext<FormFieldContextValue | null>(null);
|
|
36
|
+
|
|
37
|
+
type FormItemContextValue = {
|
|
38
|
+
id: string;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const FormItemContext = createContext<FormItemContextValue | null>(null);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Root validated form. Owns `useForm`, applies `zodResolver(schema)` when `schema` is
|
|
45
|
+
* passed, and renders a native `<form>` wired to `onSubmit`. Wrap fields in {@link FormField}.
|
|
46
|
+
*/
|
|
47
|
+
function Form<TFieldValues extends FieldValues>({
|
|
48
|
+
schema,
|
|
49
|
+
defaultValues,
|
|
50
|
+
onSubmit,
|
|
51
|
+
resolver,
|
|
52
|
+
formOptions,
|
|
53
|
+
form: externalForm,
|
|
54
|
+
className,
|
|
55
|
+
children,
|
|
56
|
+
...formHTMLProps
|
|
57
|
+
}: FormProps<TFieldValues>) {
|
|
58
|
+
const internalForm = useForm<TFieldValues>({
|
|
59
|
+
defaultValues,
|
|
60
|
+
resolver:
|
|
61
|
+
resolver ??
|
|
62
|
+
(schema ? (zodResolver(schema) as Resolver<TFieldValues>) : undefined),
|
|
63
|
+
...formOptions,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const methods: UseFormReturn<TFieldValues> = externalForm ?? internalForm;
|
|
67
|
+
|
|
68
|
+
return (
|
|
69
|
+
<FormProvider {...methods}>
|
|
70
|
+
<form
|
|
71
|
+
data-slot="form"
|
|
72
|
+
className={cn(className)}
|
|
73
|
+
onSubmit={methods.handleSubmit(onSubmit)}
|
|
74
|
+
{...formHTMLProps}
|
|
75
|
+
>
|
|
76
|
+
{children}
|
|
77
|
+
</form>
|
|
78
|
+
</FormProvider>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Connects a field to react-hook-form via `Controller`. Reads `control` from the nearest
|
|
84
|
+
* {@link Form} context — do not pass `control` manually. Must wrap {@link FormItem} inside `render`.
|
|
85
|
+
*/
|
|
86
|
+
function FormField<
|
|
87
|
+
TFieldValues extends FieldValues = FieldValues,
|
|
88
|
+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
89
|
+
>({ ...props }: FormFieldProps<TFieldValues, TName>) {
|
|
90
|
+
const { control } = useFormContext<TFieldValues>();
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<FormFieldContext.Provider value={{ name: props.name }}>
|
|
94
|
+
<Controller {...props} control={control} />
|
|
95
|
+
</FormFieldContext.Provider>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** Vertical field shell; generates stable ids for label, control, description, and message. */
|
|
100
|
+
function FormItem({ className, ...props }: HTMLAttributes<HTMLDivElement>) {
|
|
101
|
+
const id = useId();
|
|
102
|
+
|
|
103
|
+
return (
|
|
104
|
+
<FormItemContext.Provider value={{ id }}>
|
|
105
|
+
<div data-slot="form-item" className={cn(S.item, className)} {...props} />
|
|
106
|
+
</FormItemContext.Provider>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Field metadata for {@link FormLabel}, {@link FormControl}, {@link FormDescription}, and
|
|
112
|
+
* {@link FormMessage}. Requires {@link FormField} + {@link FormItem} ancestors.
|
|
113
|
+
*/
|
|
114
|
+
function useFormField() {
|
|
115
|
+
const fieldContext = useContext(FormFieldContext);
|
|
116
|
+
const itemContext = useContext(FormItemContext);
|
|
117
|
+
const { getFieldState, control } = useFormContext();
|
|
118
|
+
const formState = useFormState({ control, name: fieldContext?.name });
|
|
119
|
+
const fieldState = getFieldState(fieldContext?.name ?? '', formState);
|
|
120
|
+
|
|
121
|
+
if (!fieldContext) {
|
|
122
|
+
throw new Error('useFormField should be used within <FormField>');
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (!itemContext) {
|
|
126
|
+
throw new Error('useFormField should be used within <FormItem>');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const { id } = itemContext;
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
id,
|
|
133
|
+
name: fieldContext.name,
|
|
134
|
+
formItemId: `${id}-form-item`,
|
|
135
|
+
formDescriptionId: `${id}-form-item-description`,
|
|
136
|
+
formMessageId: `${id}-form-item-message`,
|
|
137
|
+
...fieldState,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Accessible label linked to the control id from {@link FormItem}. */
|
|
142
|
+
function FormLabel({ className, ...props }: ComponentProps<typeof Label>) {
|
|
143
|
+
const { formItemId } = useFormField();
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<Label
|
|
147
|
+
data-slot="form-label"
|
|
148
|
+
className={cn(className)}
|
|
149
|
+
htmlFor={formItemId}
|
|
150
|
+
{...props}
|
|
151
|
+
/>
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Slot wrapper for the actual input. Forwards `id`, `aria-invalid`, and `aria-describedby`
|
|
157
|
+
* so uilib controls show validation styling (P-07 inline errors).
|
|
158
|
+
*/
|
|
159
|
+
function FormControl({ ...props }: ComponentProps<typeof Slot>) {
|
|
160
|
+
const { error, formItemId, formDescriptionId, formMessageId } =
|
|
161
|
+
useFormField();
|
|
162
|
+
|
|
163
|
+
return (
|
|
164
|
+
<Slot
|
|
165
|
+
data-slot="form-control"
|
|
166
|
+
id={formItemId}
|
|
167
|
+
aria-describedby={
|
|
168
|
+
error ? `${formDescriptionId} ${formMessageId}` : formDescriptionId
|
|
169
|
+
}
|
|
170
|
+
aria-invalid={!!error}
|
|
171
|
+
{...props}
|
|
172
|
+
/>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** Optional helper text below the label; referenced by `aria-describedby` on {@link FormControl}. */
|
|
177
|
+
function FormDescription({
|
|
178
|
+
className,
|
|
179
|
+
...props
|
|
180
|
+
}: HTMLAttributes<HTMLParagraphElement>) {
|
|
181
|
+
const { formDescriptionId } = useFormField();
|
|
182
|
+
|
|
183
|
+
return (
|
|
184
|
+
<p
|
|
185
|
+
data-slot="form-description"
|
|
186
|
+
id={formDescriptionId}
|
|
187
|
+
className={cn(S.description, className)}
|
|
188
|
+
{...props}
|
|
189
|
+
/>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Inline validation message (`role="alert"`). Shows `fieldState.error.message` when present;
|
|
195
|
+
* omit for empty fields — never use toast for field errors.
|
|
196
|
+
*/
|
|
197
|
+
function FormMessage({
|
|
198
|
+
className,
|
|
199
|
+
children,
|
|
200
|
+
...props
|
|
201
|
+
}: HTMLAttributes<HTMLParagraphElement>) {
|
|
202
|
+
const { error, formMessageId } = useFormField();
|
|
203
|
+
const body = error ? String(error?.message ?? '') : children;
|
|
204
|
+
|
|
205
|
+
if (!body) {
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return (
|
|
210
|
+
<p
|
|
211
|
+
data-slot="form-message"
|
|
212
|
+
id={formMessageId}
|
|
213
|
+
role="alert"
|
|
214
|
+
className={cn(S.message, className)}
|
|
215
|
+
{...props}
|
|
216
|
+
>
|
|
217
|
+
{body}
|
|
218
|
+
</p>
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export {
|
|
223
|
+
Form,
|
|
224
|
+
FormControl,
|
|
225
|
+
FormDescription,
|
|
226
|
+
FormField,
|
|
227
|
+
FormItem,
|
|
228
|
+
FormLabel,
|
|
229
|
+
FormMessage,
|
|
230
|
+
useFormField,
|
|
231
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ComponentPropsWithoutRef, ReactNode } from 'react';
|
|
2
|
+
import type {
|
|
3
|
+
ControllerProps,
|
|
4
|
+
DefaultValues,
|
|
5
|
+
FieldPath,
|
|
6
|
+
FieldValues,
|
|
7
|
+
Resolver,
|
|
8
|
+
SubmitHandler,
|
|
9
|
+
UseFormProps,
|
|
10
|
+
UseFormReturn,
|
|
11
|
+
} from 'react-hook-form';
|
|
12
|
+
|
|
13
|
+
import type { z } from 'zod';
|
|
14
|
+
|
|
15
|
+
export type FormProps<TFieldValues extends FieldValues = FieldValues> = Omit<
|
|
16
|
+
ComponentPropsWithoutRef<'form'>,
|
|
17
|
+
'onSubmit' | 'children'
|
|
18
|
+
> & {
|
|
19
|
+
schema?: z.ZodType<TFieldValues>;
|
|
20
|
+
defaultValues?: DefaultValues<TFieldValues>;
|
|
21
|
+
onSubmit: SubmitHandler<TFieldValues>;
|
|
22
|
+
resolver?: Resolver<TFieldValues>;
|
|
23
|
+
formOptions?: Omit<UseFormProps<TFieldValues>, 'defaultValues' | 'resolver'>;
|
|
24
|
+
form?: UseFormReturn<TFieldValues>;
|
|
25
|
+
children?: ReactNode;
|
|
26
|
+
className?: string;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type FormFieldProps<
|
|
30
|
+
TFieldValues extends FieldValues = FieldValues,
|
|
31
|
+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
|
|
32
|
+
> = Omit<ControllerProps<TFieldValues, TName>, 'control'>;
|
|
@@ -28,9 +28,14 @@
|
|
|
28
28
|
@media (max-width MOBILE)
|
|
29
29
|
left 22px
|
|
30
30
|
|
|
31
|
-
// Top-level shell
|
|
31
|
+
// Top-level shell + expanded sidebar: fixed logo when header scrolls; absolute when header is sticky.
|
|
32
32
|
@media (min-width MOBILE)
|
|
33
|
-
:global([data-slot='sidebar-wrapper'][data-state='expanded']:not([data-slot='app-shell'] [data-slot='sidebar-wrapper']) [data-slot='app-shell']:not([data-slot='app-shell'] [data-slot='app-shell'])) &:global(:not([data-slot='app-shell'] [data-slot='app-shell'] *))
|
|
33
|
+
:global([data-slot='sidebar-wrapper'][data-state='expanded']:not([data-slot='app-shell'] [data-slot='sidebar-wrapper']) [data-slot='app-shell']:not([data-slot='app-shell'] [data-slot='app-shell'])) :global(header[data-sticky='false']) &:global(:not([data-slot='app-shell'] [data-slot='app-shell'] *))
|
|
34
|
+
position fixed
|
|
35
|
+
left 52px
|
|
36
|
+
top calc(22px + var(--app-banner-height, 0px))
|
|
37
|
+
|
|
38
|
+
:global([data-slot='sidebar-wrapper'][data-state='expanded']:not([data-slot='app-shell'] [data-slot='sidebar-wrapper']) [data-slot='app-shell']:not([data-slot='app-shell'] [data-slot='app-shell'])) :global(header[data-sticky='true']) &:global(:not([data-slot='app-shell'] [data-slot='app-shell'] *))
|
|
34
39
|
position absolute
|
|
35
40
|
left calc(-1 * var(--sidebar-width) + 52px)
|
|
36
41
|
|
package/src/docs/App/App.tsx
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
AppBanner,
|
|
3
|
+
AppShell,
|
|
4
|
+
AppShellMainContent,
|
|
5
|
+
} from '#uilib/components/ui/Page';
|
|
6
|
+
import {
|
|
7
|
+
DOCS_CHAT_USER_KEY,
|
|
8
|
+
DOCS_SIDEBAR_WIDTH_LS_KEY,
|
|
9
|
+
} from '../docsConstants';
|
|
3
10
|
import {
|
|
4
11
|
Link,
|
|
5
12
|
Navigate,
|
|
@@ -12,28 +19,20 @@ import {
|
|
|
12
19
|
|
|
13
20
|
import { AppHeaderHost } from '#uilib/components/ui/AppHeader';
|
|
14
21
|
import { Button } from '#uilib/components/ui/Button';
|
|
22
|
+
import { ChatProvider } from '#uilib/contexts/chat-context';
|
|
23
|
+
import type { ChatResponse } from '#uilib/types/chat-api.types';
|
|
24
|
+
import { DOC_DEFAULT_SLUG } from '../registry';
|
|
25
|
+
import { DocPage } from '../DocPage';
|
|
26
|
+
import { DocsSidebar } from '#uilib/docs/components/DocsSidebar/DocsSidebar';
|
|
15
27
|
import { Logo } from '#uilib/components/ui/Logo';
|
|
16
|
-
import {
|
|
17
|
-
AppBanner,
|
|
18
|
-
AppShell,
|
|
19
|
-
AppShellMainContent,
|
|
20
|
-
} from '#uilib/components/ui/Page';
|
|
21
28
|
import { PageFooter } from '#uilib/components/ui/Page/PageFooter/PageFooter';
|
|
22
29
|
import { PageScroll } from '#uilib/components/ui/Page/PageScroll/PageScroll';
|
|
30
|
+
import S from '../DocsShell.styl';
|
|
23
31
|
import { SidebarProvider } from '#uilib/components/ui/Sidebar/Sidebar';
|
|
24
32
|
import { SybilionAppHeader } from '#uilib/components/widgets/SybilionAppHeader';
|
|
25
|
-
import { ChatProvider } from '#uilib/contexts/chat-context';
|
|
26
|
-
import { DocsSidebar } from '#uilib/docs/components/DocsSidebar/DocsSidebar';
|
|
27
|
-
import type { ChatResponse } from '#uilib/types/chat-api.types';
|
|
28
|
-
|
|
29
|
-
import { DocPage } from '../DocPage';
|
|
30
|
-
import S from '../DocsShell.styl';
|
|
31
|
-
import {
|
|
32
|
-
DOCS_CHAT_USER_KEY,
|
|
33
|
-
DOCS_SIDEBAR_WIDTH_LS_KEY,
|
|
34
|
-
} from '../docsConstants';
|
|
35
|
-
import { DOC_DEFAULT_SLUG } from '../registry';
|
|
36
33
|
import { ThemeToggle } from './ThemeToggle';
|
|
34
|
+
import cn from 'classnames';
|
|
35
|
+
import { useState } from 'react';
|
|
37
36
|
|
|
38
37
|
async function mockDocsSendChatMessage(
|
|
39
38
|
message: string,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import FormGuidance from '#uilib/components/ui/Form/AGENT.md';
|
|
2
|
+
import { FormExample } from '#uilib/components/ui/Form/Form.example';
|
|
3
|
+
|
|
4
|
+
import { AppPageHeader } from '../components/AppPageHeader/AppPageHeader';
|
|
5
|
+
import { DocsGuidance } from '../components/DocsGuidance/DocsGuidance';
|
|
6
|
+
import { DocsHeaderActions } from '../docsHeaderActions';
|
|
7
|
+
|
|
8
|
+
export default function FormPage() {
|
|
9
|
+
return (
|
|
10
|
+
<>
|
|
11
|
+
<AppPageHeader
|
|
12
|
+
breadcrumbs={[{ label: 'Form' }]}
|
|
13
|
+
title="Form"
|
|
14
|
+
subheader="Validated forms with react-hook-form and zod."
|
|
15
|
+
actions={<DocsHeaderActions />}
|
|
16
|
+
/>
|
|
17
|
+
<DocsGuidance source={FormGuidance} />
|
|
18
|
+
<FormExample />
|
|
19
|
+
</>
|
|
20
|
+
);
|
|
21
|
+
}
|
package/src/docs/registry.ts
CHANGED
|
@@ -278,6 +278,12 @@ export const DOC_REGISTRY: DocEntry[] = [
|
|
|
278
278
|
section: 'Layout',
|
|
279
279
|
load: () => import('./pages/FoldablePage'),
|
|
280
280
|
},
|
|
281
|
+
{
|
|
282
|
+
slug: 'form',
|
|
283
|
+
title: 'Form',
|
|
284
|
+
section: 'Forms',
|
|
285
|
+
load: () => import('./pages/FormPage'),
|
|
286
|
+
},
|
|
281
287
|
{
|
|
282
288
|
slug: 'grid-layout',
|
|
283
289
|
title: 'GridLayout',
|
package/src/index.ts
CHANGED
|
@@ -40,6 +40,7 @@ export * from './components/ui/DropZone/DropZone';
|
|
|
40
40
|
export * from './components/ui/FileChip';
|
|
41
41
|
export * from './components/ui/FlickeringGrid';
|
|
42
42
|
export * from './components/ui/Foldable';
|
|
43
|
+
export * from './components/ui/Form';
|
|
43
44
|
export * from './components/ui/Gap/Gap';
|
|
44
45
|
export * from './components/ui/GridLayout/GridLayout';
|
|
45
46
|
export * from './components/ui/Image';
|