Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

fieldComponentHelpers.lib.tsx

30 lines797 Bytes

1import { useFieldContext } from './contexts.lib'
2import type { FunctionComponent } from 'preact/compat'
3import type { CrossVersionPreactNode } from '../preactTypes.public'
4​
5type AnyFieldComponent = (props: any) => CrossVersionPreactNode
6​
7export function wrapField(
8 Component: AnyFieldComponent,
9 fieldPropKey: string,
10): AnyFieldComponent {
11 const Wrapper: FunctionComponent<any> = function TanStackFormFieldWrapper(
12 props,
13 ) {
14 const field = useFieldContext()
15​
16 const newProps = { ...props, [fieldPropKey]: field }
17​
18 return <Component {...newProps} />
19 }
20​
21 Wrapper.displayName = 'TanStackForm.FieldComponent'
22​
23 return Wrapper as never
25​
26export function brandComponentFactory(): (
27 component: AnyFieldComponent,
28) => AnyFieldComponent {
29 return (component) => component