mirror of
https://github.com/LukeHagar/form.git
synced 2025-12-10 12:27:45 +00:00
fix: form.Provider
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import ReactDOM from "react-dom/client";
|
import ReactDOM from "react-dom/client";
|
||||||
import { FieldApi, createFormFactory, useField } from "@tanstack/react-form";
|
import { FieldApi, createFormFactory } from "@tanstack/react-form";
|
||||||
|
|
||||||
type Person = {
|
type Person = {
|
||||||
firstName: string;
|
firstName: string;
|
||||||
@@ -41,15 +41,20 @@ export default function App() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const [count, setCount] = React.useState(0);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
<button onClick={() => setCount((prev) => prev + 1)}>{count}</button>
|
||||||
<h1>Simple Form Example</h1>
|
<h1>Simple Form Example</h1>
|
||||||
{/* A pre-bound form component */}
|
{/* A pre-bound form component */}
|
||||||
|
<form.Provider>
|
||||||
<form {...form.getFormProps()}>
|
<form {...form.getFormProps()}>
|
||||||
<div>
|
<div>
|
||||||
{/* A type-safe and pre-bound field component*/}
|
{/* A type-safe and pre-bound field component*/}
|
||||||
<form.Field
|
<form.Field
|
||||||
name="firstName"
|
name="firstName"
|
||||||
|
validateOn="change"
|
||||||
validate={(value) => !value && "A first name is required"}
|
validate={(value) => !value && "A first name is required"}
|
||||||
validateAsyncOn="change"
|
validateAsyncOn="change"
|
||||||
validateAsyncDebounceMs={500}
|
validateAsyncDebounceMs={500}
|
||||||
@@ -59,13 +64,16 @@ export default function App() {
|
|||||||
value.includes("error") && 'No "error" allowed in first name'
|
value.includes("error") && 'No "error" allowed in first name'
|
||||||
);
|
);
|
||||||
}}
|
}}
|
||||||
children={(field) => (
|
children={(field) => {
|
||||||
// Avoid hasty abstractions. Render props are great!
|
// Avoid hasty abstractions. Render props are great!
|
||||||
|
return (
|
||||||
<>
|
<>
|
||||||
|
<input placeholder="uncontrolled" />
|
||||||
<input {...field.getInputProps()} />
|
<input {...field.getInputProps()} />
|
||||||
<FieldInfo field={field} />
|
<FieldInfo field={field} />
|
||||||
</>
|
</>
|
||||||
)}
|
);
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
@@ -117,7 +125,9 @@ export default function App() {
|
|||||||
/>
|
/>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => hobbiesField.removeValue(i)}
|
onClick={() =>
|
||||||
|
hobbiesField.removeValue(i)
|
||||||
|
}
|
||||||
>
|
>
|
||||||
X
|
X
|
||||||
</button>
|
</button>
|
||||||
@@ -171,7 +181,8 @@ export default function App() {
|
|||||||
</button>
|
</button>
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
</form.Form>
|
</form>
|
||||||
|
</form.Provider>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -129,6 +129,7 @@ export function Field<TData, TFormData>({
|
|||||||
children: (fieldApi: FieldApi<TData, TFormData>) => any
|
children: (fieldApi: FieldApi<TData, TFormData>) => any
|
||||||
} & UseFieldOptions<TData, TFormData>) {
|
} & UseFieldOptions<TData, TFormData>) {
|
||||||
const fieldApi = useField(fieldOptions as any)
|
const fieldApi = useField(fieldOptions as any)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<formContext.Provider
|
<formContext.Provider
|
||||||
value={{ formApi: fieldApi.form, parentFieldName: fieldApi.name }}
|
value={{ formApi: fieldApi.form, parentFieldName: fieldApi.name }}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ declare module '@tanstack/form-core' {
|
|||||||
|
|
||||||
// eslint-disable-next-line no-shadow
|
// eslint-disable-next-line no-shadow
|
||||||
interface FormApi<TFormData> {
|
interface FormApi<TFormData> {
|
||||||
|
Provider: (props: { children: any }) => any
|
||||||
getFormProps: () => FormProps
|
getFormProps: () => FormProps
|
||||||
Field: FieldComponent<TFormData, TFormData>
|
Field: FieldComponent<TFormData, TFormData>
|
||||||
useField: UseField<TFormData>
|
useField: UseField<TFormData>
|
||||||
@@ -40,6 +41,9 @@ export function useForm<TData>(opts?: FormOptions<TData>): FormApi<TData> {
|
|||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const api = new FormApi<TData>(opts)
|
const api = new FormApi<TData>(opts)
|
||||||
|
|
||||||
|
api.Provider = (props) => (
|
||||||
|
<formContext.Provider {...props} value={{ formApi: api }} />
|
||||||
|
)
|
||||||
api.getFormProps = () => {
|
api.getFormProps = () => {
|
||||||
return {
|
return {
|
||||||
onSubmit: formApi.handleSubmit,
|
onSubmit: formApi.handleSubmit,
|
||||||
|
|||||||
Reference in New Issue
Block a user