Skip to content

[form][field] Add actionsRef - #3395

Merged
mj12albert merged 7 commits into
mui:masterfrom
mj12albert:form/actions-ref
Jan 6, 2026
Merged

mj12albert merged 7 commits into
mui:masterfrom
mj12albert:form/actions-ref

Conversation

@mj12albert

@mj12albert mj12albert commented Dec 2, 2025 •

Copy link
Copy Markdown
Member

Adds a actionsRef.validate method to Form and Field so validation can be called imperatively (and arbitrarily).

Form:

const actionsRef = React.useRef<Form.Actions>(null);

<Form actionsRef={actionsRef} />>

// validate all fields
actionsRef.current?.validate();
// validate one field by name
actionsRef.current?.validate('username');

Field:

const actionsRef = React.useRef<Field.Root.Actions>(null);

<Field.Root actionsRef={actionsRef} />>

// validate the field
actionsRef.current?.validate();

Closes #3323

@mj12albert mj12albert added type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. component: form Changes related to the form component. labels Dec 2, 2025
@pkg-pr-new

pkg-pr-new Bot commented Dec 2, 2025 •

Copy link
Copy Markdown
  • vite-css-base-ui-example

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

commit: f8b58c9

@mui-bot

mui-bot commented Dec 2, 2025 •

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 🔺+368B(+0.09%) 🔺+114B(+0.09%)

Details of bundle changes


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

@netlify

netlify Bot commented Dec 2, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit f8b58c9
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/695cb7badc2d3a0008f3bef3
😎 Deploy Preview https://deploy-preview-3395--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

@mj12albert
mj12albert force-pushed the form/actions-ref branch 3 times, most recently from cb13f43 to 40c2427 Compare December 8, 2025 14:27
@mj12albert mj12albert added the component: field Changes related to the field component. label Dec 8, 2025
@mj12albert mj12albert changed the title [form] Add actionsRef [form][field] Add actionsRef Dec 15, 2025
@mj12albert
mj12albert marked this pull request as ready for review December 16, 2025 11:22
@LukasTy LukasTy added type: new feature Expand the scope of the product to solve a new problem. and removed type: enhancement It’s an improvement, but we can’t make up our mind whether it's a bug fix or a new feature. labels Dec 19, 2025

@LukasTy LukasTy 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.

LGTM, nice addition. 👍
I've changed the label to new feature instead of enhancement. 🙈

Comment on lines +74 to +82
const values = Array.from(formRef.current.fields.values());

if (fieldName) {
const namedField = values.find((field) => field.name === fieldName);
if (namedField) {
namedField.validate(false);
}
} else {
values.forEach((field) => {

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.

Looks slightly more efficient.

Suggested change
const values = Array.from(formRef.current.fields.values());
if (fieldName) {
const namedField = values.find((field) => field.name === fieldName);
if (namedField) {
namedField.validate(false);
}
} else {
values.forEach((field) => {
if (fieldName) {
const namedField = formRef.current.fields.get(fieldName);
if (namedField) {
namedField.validate(false);
}
} else {
Array.from(formRef.current.fields.values()).forEach((field) => {

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.

Gotta do it like this because the map key is the id and not the field name 😬

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.

Gotcha.
Well, then maybe move the L74 to be inline in L82 to avoid unnecessary calculations in case they are not needed? 🤔

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.

We need the array in the if as well to do a .find() on it for the field name ~

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.

Oh yes, silly me. Sorry for causing commotion here. 🙈
Regardless, it would be nice to have access to the field ID here to improve the performance and simplicity of the code.

@atomiks

atomiks commented Dec 28, 2025

Copy link
Copy Markdown
Contributor

const actionsRef = React.useRef<Form.Actions>(null);

With Form.Actions accepting null, we should probably also adapt other actionsRef props to accept null. Right now you need to fill it in with the object with a noop unmount() function

@mj12albert
mj12albert merged commit 683308d into mui:master Jan 6, 2026
@mj12albert
mj12albert deleted the form/actions-ref branch January 6, 2026 07:34
atomiks pushed a commit to atomiks/base-ui that referenced this pull request Jan 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: field Changes related to the field component. component: form Changes related to the form component. type: new feature Expand the scope of the product to solve a new problem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to trigger initial validation in Field.Root?

4 participants