-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathField.tsx
More file actions
48 lines (46 loc) · 1.42 KB
/
Copy pathField.tsx
File metadata and controls
48 lines (46 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import {
type FormSchema,
type RequiredPath,
type ValidPath,
} from '@formisch/core/honox';
import type { JSX } from 'hono/jsx/jsx-runtime';
import type * as v from 'valibot';
import { useField } from '../../hooks/index.ts';
import type { FieldStore, FormStore } from '../../types/index.ts';
/**
* Field component props interface.
*/
export interface FieldProps<
TSchema extends FormSchema = FormSchema,
TFieldPath extends RequiredPath = RequiredPath,
> {
/**
* The form store to which the field belongs.
*/
readonly of: FormStore<TSchema>;
/**
* The path to the field within the form schema.
*/
readonly path: ValidPath<v.InferInput<TSchema>, TFieldPath>;
/**
* The render function that receives the field store and returns JSX.
*/
readonly children: (store: FieldStore<TSchema, TFieldPath>) => JSX.Element;
}
/**
* Headless form field component that provides reactive properties and state.
* The field component takes a form store, path to field, and a render function
* that receives a field store to display field state and handle user interactions.
*
* @param props The field component props.
*
* @returns The UI of the field to be rendered.
*/
// @__NO_SIDE_EFFECTS__
export function Field<
TSchema extends FormSchema,
TFieldPath extends RequiredPath,
>({ of, path, children }: FieldProps<TSchema, TFieldPath>): JSX.Element {
const field = useField(of, { path });
return children(field);
}