Repository navigation
[checkbox] Submit the associated form on Enter key press - #4713
Conversation
commit: |
Bundle size
PerformanceTotal duration: 1,357.00 ms 🔺+231.70 ms(+20.6%) | Renders: 53 (+0) | Paint: 2,077.30 ms 🔺+367.14 ms(+21.5%)
…and 7 more — details Check out the code infra dashboard for more information about this PR. |
✅ Deploy Preview for base-ui ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: bda47152dd
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 168c965f00
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (tagName === 'INPUT') { | ||
| const input = candidate as HTMLInputElement; | ||
|
|
||
| if (input.type === 'submit') { |
There was a problem hiding this comment.
Include image submit inputs in default submitter lookup
getDefaultFormSubmitter only recognizes input[type="submit"], but HTML also treats input[type="image"] as a submit button. In forms where the first submitter is an image input (or where it is the only submitter), pressing Enter on Checkbox.Root will pick the wrong submitter or none at all, so implicit submission behavior diverges from native and SubmitEvent.submitter can be incorrect.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
I chose to ignore input[type=image] as Chromium omits it from form.elements and it would have to be handled separately. Considering that it's quite exotic, I don't want to inflate the bundle because of it.
flaviendelangle
left a comment
There was a problem hiding this comment.
PR Review: [checkbox] Submit the associated form on Enter key press
PR: mui/base-ui#4713
Scope: 4 files, +366/-14 — checkbox Enter behavior + new utility
Critical Issues
1. Missing input type="image" as a submit button
getDefaultFormSubmitter only checks for type === 'submit', but per the HTML Living Standard, both submit and image are considered submit buttons:
The following input types are considered submit buttons:
submit,image
Real-world example:
<form>
<input type="image" src="go.png" alt="Go">
</form>Native browsers will use <input type="image"> as the default submitter when there's no other submit button. The PR's utility would return null here, diverging from native behavior.
Suggested fix in getDefaultFormSubmitter.ts:
if (tagName === 'BUTTON') {
const button = candidate as HTMLButtonElement;
if (button.type === 'submit') return button;
} else if (tagName === 'INPUT') {
const input = candidate as HTMLInputElement;
if (input.type === 'submit' || input.type === 'image') return input;
}A test for <input type="image"> should also be added.
Important Issues
2. event.preventDefault() is called even when there is no form
In CheckboxRoot.tsx:
event.preventDefault();
getDefaultFormSubmitter(inputRef.current?.form ?? null)?.click();When the checkbox is rendered with nativeButton outside any form, preventDefault() will swallow the Enter keypress without performing any action. That mirrors the goal ("Enter must not toggle the checkbox") so the resulting behavior is fine, but the intent of the unconditional preventDefault() could be clearer. Consider hoisting the rationale into a comment, or only calling preventDefault() when there's actually a form to submit. (Behavior-equivalent today, but easier to reason about.)
3. Inconsistent assertions across new tests
Most new Enter-submit tests assert expect(checkbox).toHaveAttribute('aria-checked', 'false') afterwards to confirm the checkbox didn't toggle. But the new submits to an external form with Enter when 'form' is provided test (packages/react/src/checkbox/root/CheckboxRoot.test.tsx, around L670–698) is missing that assertion. Add it for consistency and to defend against a regression where Enter both submits and toggles.
Suggestions
4. Iteration could be slightly clearer
The current pattern works, but the local tagName + cast pattern is a touch awkward. A narrower check via DOM type guards would mix concerns. Either keep the current shape, or compress to:
for (const candidate of form.elements) {
if (candidate instanceof HTMLButtonElement && candidate.type === 'submit') {
return candidate;
}
if (candidate instanceof HTMLInputElement &&
(candidate.type === 'submit' || candidate.type === 'image')) {
return candidate;
}
}Note: if you adopt instanceof, form.ownerDocument.defaultView's constructors should be used for cross-realm safety (matching base-ui-plus convention; check whether base-ui has the same rule).
5. JSDoc nit
The doc says:
Disabled submitters can be returned because the default button is determined before disabled state is considered; clicking a disabled submitter is a no-op.
This is accurate, and the test does not submit the form with Enter when the default button is disabled is the right behavioral assertion. Consider adding a one-line note pointing readers to that test (or vice versa) so future maintainers understand the "no-op click" is intentional, not a bug.
Strengths
- Comprehensive test matrix. Covers: focus + keyboard, consumer
preventDefault,readOnly,nativeButton(with the important "submits once" check), no submit button, disabled default submitter, and externalform=association. This is the right shape for a behavior change like this. preventBaseUIHandler+defaultPreventedorder is correct. CallingpreventBaseUIHandlerfirst defensively stops Base UI's button-style Enter handling regardless of what the consumer does, then thedefaultPreventedcheck honors the consumer's opt-out before attempting submission. Good escape hatch design.- Using
.click()on the default submitter (rather thanform.requestSubmit()) correctly preserves browser-native semantics: the submitter's own click handlers run,SubmitEvent.submitteris set, and submitter-specific attributes (formaction,formmethod, etc.) are honored. This is the subtle correct choice. - Refactor of the previous
['Enter', 'Space'].forEachparametric test into two separate tests is clean and clearly states the new contract (Space activates, Enter does not). - The bottom-of-file integration test was updated correctly to flip the Enter/Space/click expectations rather than being deleted.
- Comment on the
onKeyDownhandler explains why (consumer opt-out + defensive Base UI handler suppression) rather than what — matches the comment-quality bar.
Recommended Action
- Add
input type="image"support ingetDefaultFormSubmitterand a corresponding test (Critical). - Add the
aria-checked='false'assertion to the external-form Enter test (Important). - Optional: minor doc/code cleanup per suggestions 2, 4, 5.
The core design is sound — the only behavioral correctness gap is the missing type="image" case.
This was a deliberate decision: #4713 (comment)
This would make TS slighly happier, but I think there's no real value in it (and we'd introduce more code to make sure the right |
I'd just add a comment on the method then to avoid having Claude Code or Codex propose to change that in the future 👍 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 17a37b3465
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Matches the native
<input type="checkbox">behavior regarding form submission. Enter no longer toggles the checkbox but submits the associated form. It supports both parent forms and the ones linked with theformattribute.Added a
getDefaultFormSubmitterutility to@base-ui/utils, so it can be used in other repos (cc @flaviendelangle)Fixes #4677.