iway1 / react-ts-form

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

how to handle dependent field props? #21

Open christopherGdynia opened 1 year ago

christopherGdynia commented 1 year ago

In some forms, field props are dependen on values from other fields.

How are we going to solve this?

this could be quiet tricky


A solution for now is creating a custom component, which is dependent and loading the formState from the context (when implemented).

This should be documented. :D

iway1 commented 1 year ago

Are you talking about situations where you need to compare things like a password and confirm password field (IE dependent validation steps)? If so, I agree it should be documented. You can use the zod schemas .refine method to do this.

If you're trying to do things like "Hide certain inputs when a certain drop down value is selected", I believe this is possible with an object schema at the moment but might not be a super smooth developer experience? Might be worth adding a more elegant way to deal with these situations...

Let me know some more details on your use case

christopherGdynia commented 1 year ago

I am talking about passing props like min or max to fields which will have the value ofher fields

We have a use case where the user needs to pick three dates, the value of the first date is passed as the min value to the second date field

iway1 commented 1 year ago

@christopherGdynia Ah okay thanks for the info - I would recommend using an object schema for this right now I think, or just writing plain old JSX for these more complex forms.

For the future, I think there's an API that could simplify this though that could be added. We could add a feature where props can be calculated from form state by passing a function instead of an object:

return (
  <Form
    props={(fieldValues)=>({
      endDateSelect: {
        minDate: fieldValues.startDate
      }
    })}
  />
)

Would this suffice, I think it'd cover this use case pretty well?

christopherGdynia commented 1 year ago

@iway1, this would be sufficient,

maybe pass as second parameter the form state, with error state.

iway1 commented 1 year ago

@christopherGdynia good idea, we'll pass the error state as well.