iway1 / react-ts-form

https://react-ts-form.com
MIT License
2.01k stars 35 forks source link

How to access form state in a custom form? #33

Closed rvetere closed 1 year ago

rvetere commented 1 year ago

Is there any way to access the formState in a custom form implementation? We want to display some kind of loading spinner when the form is submitting.

When i'm in a field, i can easily use useTsController but this is only allowed within fields, not in forms.

iway1 commented 1 year ago

Do you mean in a custom form component? IE you're wanting to do:

function FormContainer() {
  const formState = useFormState();
  // ...
}

const MyForm = createTsForm(mapping, {FormComponent: FormContainer});

Or do you mean accessing the form state in the parent component of the form?

Right now this should be possible via passing props to the form component. Something like:

function FormContainer({isLoading}:{isLoading: boolean}) {}
//...
const MyForm = createTsForm(mapping, {FormComponent:FormContainer});

function Page() {
  return (
    <MyForm 
      formProps={{isLoading: isLoading}}
    />
  )
}
iway1 commented 1 year ago

Gonna close this, but feel free to reopen if the above isn't able to do something you're looking for