Support the HTML form attribute to enable complex forms #4794
slimshreydy
started this conversation in
Feature requests
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
TL;DR HeroUI is not compatible with uncontrolled nested forms right now, but support is very very easy to add.
Background
For people using HeroUI to create uncontrolled forms, they typically rely on the hidden inputs inside the various form input components to ensure all the data gets submitted as part of the form action.
For example, if you want to make an uncontrolled form that uses Nextjs server actions to process the submission, you can use HeroUI to set it up like this:
The DateRangePicker component implementation uses hidden
input
elements to store the values of theDateRangePicker
, and we can usestartName
andendName
to tell HeroUI what name keys those values should be stored under in the submitted form data. That means on submit, our serversubmitAction
sees something like this (technically its a FormData object and not a JSON, but that's an irrelevant detail here):Problem
However, one challenge is that forms can't be nested in HTML. Submitting a form nested in another form leads to undefined behavior. That means if you're making a complex form (or perhaps a form with subforms), rendered HTML like this is invalid:
Fortunately, modern browsers (everything released 2017 and on) support a
form
attribute, so you can associate inputs with different forms like so:This allows us to present form inputs in a way that end users can logically process as a form and a subform, while staying in compliance with HTML's "no nested forms" rule.
Sadly, HeroUI's form input components do not all support the
form
attribute, making this type of thing impossible to do with HeroUI at the moment. Only naive/thinly-wrapped inputs likeInput
andCheckbox
support this, mostly because HeroUI just inherits the default list of native element props from their corresponding HTML elements. For more complex inputs likeDateRangePicker
that use hidden inputs, there's no way to propagate aform
attribute to these hidden inputs.Proposed Solution
The proposal is simple and (presumably) very easy: just support a
form
attribute on any HeroUI component that exposes aname
field (or some variant of it). For example, if we could initialize aDateRangeInput
like so, it would fix the issue:HeroUI has already done the work of adding support for uncontrolled forms by adding hidden inputs and exposing
name
props on all complex form inputs -- to support nested uncontrolled forms, it would just need to expose a newform
prop and pass that prop to the hidden input.Beta Was this translation helpful? Give feedback.
All reactions