Skip to content

fix: persist Select dropdown value and reset form after contribution submit - #6

Open
lb1192176991-lab wants to merge 1 commit into
MAY55A:devfrom
lb1192176991-lab:main
Open

fix: persist Select dropdown value and reset form after contribution submit#6
lb1192176991-lab wants to merge 1 commit into
MAY55A:devfrom
lb1192176991-lab:main

Conversation

@lb1192176991-lab

Copy link
Copy Markdown

What

Two bugs in the contribution form:

  1. The shadcn Select dropdown component did not persist selected values — the value prop received undefined initially, causing an uncontrolled/controlled mismatch that broke Radix UI state tracking.
  2. After successful submission, the form did not reset to its initial empty state.

Why

Bug 1: The Select value prop used optional chaining which returned undefined on first render. Radix UI Select cannot switch from uncontrolled to controlled.

Bug 2: formRef.current.reset() only affects native DOM form elements. React-controlled components like shadcn Select retain their internal state after reset().

Testing

  • Verified Select value prop always receives a string (empty string fallback) so Radix stays controlled
  • Form now uses a formKey counter that increments on success to force-clean all component state
  • TypeScript compiles cleanly
  • Existing functionality (text inputs, textareas, radio groups, file uploads) unchanged

…ntribution submission

Bug 1 — Select dropdown value not persisted:
The shadcn Select component's value prop received undefined initially,
causing uncontrolled/controlled mismatch. The onChange synthetic event
wrapper also introduced unnecessary indirection. Fixed by:
- Passing value={formData[...]?.value ?? ''} so the Select is always controlled
- Calling setFormData directly in onValueChange instead of routing through
  the generic handleChange helper with a synthetic event

Bug 2 — Form not resetting after submit:
Native HTMLFormElement.reset() does not clear controlled components like
shadcn Select. Fixed by:
- Replacing formRef.reset() with a formKey counter that increments on
  successful submission, forcing the FormGenerator to remount with clean
  state
- Removing the 3-second setTimeout delay before reset
@vercel

vercel Bot commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

@lb1192176991-lab is attempting to deploy a commit to the MAY55A's projects Team on Vercel.

A member of the Team first needs to authorize it.

@MAY55A

MAY55A commented Jul 29, 2026

Copy link
Copy Markdown
Owner

@lb1192176991-lab, thanks for your contribution! I really appreciate your work.

I've recently introduced a dev branch, and I'd like all future contributions to target dev instead of main.

Could you please update this pull request so that its base branch is dev? If GitHub doesn't allow changing it, you can close this PR and open a new one targeting dev. Thanks!

@lb1192176991-lab
lb1192176991-lab changed the base branch from main to dev July 29, 2026 18:50
@lb1192176991-lab

Copy link
Copy Markdown
Author

@MAY55A Done! Changed the base branch from main to dev successfully. Thanks for the guidance!

@MAY55A MAY55A left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lb1192176991-lab, thank you for your effort !

These are the remarks I made while reviewing your PR:

  • The changes in the "form-generator.tsx" file should be reverted, because they allow empty values for required Select fields.

  • Changes in "task-fields.tsx", however, fixed the issue partially (the submit button needs to be reset as well after every submission, it currently is enabled even with the check box not checked).

  • Apparently, fixing the reset issue also fixes the undefined select value as well, so no need to modify any code in "form-generator.tsx".

Would really appreciate trying to fix the remaining problem, to close this issue once and for all !

} as React.ChangeEvent<HTMLInputElement>)}
value={formData[field.label]?.value}
setFormData((prev: { [key: string]: any }) => ({ ...prev, [field.label]: { value: selectedOption } }))}
value={formData[field.label]?.value ?? ""}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows empty values for required Select fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Selected dropdown value is not saved in contribution data, and form does not reset after submit

2 participants