-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ui: Improve form handling #1963
Open
Etsija
wants to merge
5
commits into
main
Choose a base branch
from
improve-form-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+95
−14
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
03ec527
to
ae88545
Compare
#1940 introduced a component for inputting "password" fiels, so use it also for creating a new user. Signed-off-by: Jyrki Keisala <jyrki.keisala@doubleopen.org>
When using controlled forms, default values must be provided to non-optional form fields [1], to prevent the React warning (seen in the dev. console of the browser): "Warning: A component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen." [1]: https://stackoverflow.com/a/77202565 Signed-off-by: Jyrki Keisala <jyrki.keisala@doubleopen.org>
Define a function which makes it possible to add refining schema validations to optional parameters [1]. [1]: shadcn-ui/ui#690 (comment) Signed-off-by: Jyrki Keisala <jyrki.keisala@doubleopen.org>
Optional inputs in controlled forms are problematic, because (1) Specifying default values for them in the `useForm` hook leads to unnecessary payload sent from the form, so rather keep them as `undefined`, which means the corresponding parameter is not included in the payload. (2) Leaving them without default values causes a React warning about a component transferring from uncontrolled to controlled state. Alleviate these problems with a component specifically used for optional inputs [1]. [1]: shadcn-ui/ui#410 (comment)
As an example of using `asOptionalField` utility function and `OptionalInput` component when handling optional inputs in forms, use them in the Admin section's user creation form. This form has quite many optional input fields, which, when left empty, are not included in the form payload. When they are non-empty, they are validated with Zod rules (for example, "email" field). Signed-off-by: Jyrki Keisala <jyrki.keisala@doubleopen.org>
ae88545
to
26dfd21
Compare
mnonnenmacher
approved these changes
Feb 10, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, but I would appreciate if @lamppu could also have a look as my React knowledge is limited.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR gets rid of most React warnings about form components transferring from
uncontrolled
tocontrolled
state by:Zod
validation for optional fields, whenever user inputs something to them.As an example of handling optional fields, the user creation form is modified accordingly. If this approach is acceptable, other forms would be fixed in a follow-up PR.
This is a more proper alternative to #1946.
Please see the commits for details.