Skip to content

[checkbox] Submit the associated form on Enter key press - #4713

Merged
michaldudak merged 9 commits into
mui:masterfrom
michaldudak:checkbox-submit
May 5, 2026
Merged

michaldudak merged 9 commits into
mui:masterfrom
michaldudak:checkbox-submit

Conversation

@michaldudak

@michaldudak michaldudak commented Apr 30, 2026 •

Copy link
Copy Markdown
Member

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 the form attribute.

Added a getDefaultFormSubmitter utility to @base-ui/utils, so it can be used in other repos (cc @flaviendelangle)

Fixes #4677.

@pkg-pr-new

pkg-pr-new Bot commented Apr 30, 2026 •

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@4713
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4713
    

commit: 1320a23

@michaldudak michaldudak added type: bug It doesn't behave as expected. component: checkbox Changes related to the checkbox component. labels Apr 30, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Apr 30, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 🔺+522B(+0.11%) 🔺+187B(+0.13%)

Details of bundle changes

Performance

Total duration: 1,357.00 ms 🔺+231.70 ms(+20.6%) | Renders: 53 (+0) | Paint: 2,077.30 ms 🔺+367.14 ms(+21.5%)

Test Duration Renders
Tabs mount (200 instances) 252.56 ms 🔺+48.07 ms(+23.5%) 4 (+0)
Menu mount (300 instances) 167.77 ms 🔺+41.41 ms(+32.8%) 2 (+0)
Popover mount (300 instances) 110.99 ms 🔺+24.20 ms(+27.9%) 2 (+0)
Scroll Area mount (300 instances) 96.94 ms 🔺+21.92 ms(+29.2%) 3 (+0)
Select mount (200 instances) 151.52 ms 🔺+20.63 ms(+15.8%) 3 (+0)

…and 7 more — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented Apr 30, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 1320a23
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69f858590438af00089d8420
😎 Deploy Preview https://deploy-preview-4713--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@michaldudak
michaldudak marked this pull request as ready for review April 30, 2026 12:15

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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 flaviendelangle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 external form= association. This is the right shape for a behavior change like this.
  • preventBaseUIHandler + defaultPrevented order is correct. Calling preventBaseUIHandler first defensively stops Base UI's button-style Enter handling regardless of what the consumer does, then the defaultPrevented check honors the consumer's opt-out before attempting submission. Good escape hatch design.
  • Using .click() on the default submitter (rather than form.requestSubmit()) correctly preserves browser-native semantics: the submitter's own click handlers run, SubmitEvent.submitter is set, and submitter-specific attributes (formaction, formmethod, etc.) are honored. This is the subtle correct choice.
  • Refactor of the previous ['Enter', 'Space'].forEach parametric 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 onKeyDown handler explains why (consumer opt-out + defensive Base UI handler suppression) rather than what — matches the comment-quality bar.

Recommended Action

  1. Add input type="image" support in getDefaultFormSubmitter and a corresponding test (Critical).
  2. Add the aria-checked='false' assertion to the external-form Enter test (Important).
  3. 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.

@michaldudak

Copy link
Copy Markdown
Member Author

Missing input type="image" as a submit button

This was a deliberate decision: #4713 (comment)

Iteration could be slightly clearer

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 instanceof is called). Checking the tagName should be enough.

@flaviendelangle

Copy link
Copy Markdown
Member

This was a deliberate decision: #4713 (comment)

I'd just add a comment on the method then to avoid having Claude Code or Codex propose to change that in the future 👍

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

@michaldudak
michaldudak merged commit b4c85ac into mui:master May 5, 2026
6 checks passed
@michaldudak
michaldudak deleted the checkbox-submit branch May 5, 2026 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: checkbox Changes related to the checkbox component. type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[checkbox] Pressing Enter does not submit the form

4 participants