vaadin / hilla

Build better business applications, faster. No more juggling REST endpoints or deciphering GraphQL queries. Hilla seamlessly connects Spring Boot and React to accelerate application development.
https://hilla.dev
Apache License 2.0
904 stars 57 forks source link

Allow getting form state inside fieldOptions > renderer for AutoForm #2754

Open pratikpanchal4472 opened 1 week ago

pratikpanchal4472 commented 1 week ago

Describe your motivation

I have to render a conditional field based on value of other field. right now with auto form it seems to be not supported. I am using fieldOptions > renderer property to customize both the fields but the dependent field cannot get the value of other field.

I want to perform something similar to what mentioned over here but with AutoForm: https://vaadin.com/docs/latest/hilla/guides/forms/form-status-tracking#example-populate-a-field-based-on-another

Describe the solution you'd like

Allow for a propety to share state of whole form from the caller component and update that state when the form values changes. AutoForm has this item propery, if we accept the setter for that then updating the same state can help handle fieldOptions rendering.

const [formState, setFormState] = useState<any>({country: '' , city: ''});

<AutoForm service={AddressService}
                            model={AddressModel}
                            item={formState}
                            onChange={(value) => setFormState(value)}

observe the onChange method.

Describe alternatives you've considered

I tried using useForm hook with AutoForm but it seems not to update the value. Happy to understand if there are any other ways possible and available currently.