Skip to content

[input] Update ref type to HTMLElement#3866

Merged
mj12albert merged 2 commits into
mui:masterfrom
mj12albert:field-control-ref-type
Jan 28, 2026
Merged

[input] Update ref type to HTMLElement#3866
mj12albert merged 2 commits into
mui:masterfrom
mj12albert:field-control-ref-type

Conversation

@mj12albert

@mj12albert mj12albert commented Jan 26, 2026

Copy link
Copy Markdown
Member

This allows <Field.Control render={<textarea />} /> without type errors when passing a ref to a HTMLTextAreaElement

@mj12albert mj12albert added component: input Changes related to the input component. typescript labels Jan 26, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jan 26, 2026

Copy link
Copy Markdown

commit: 050410d

@mui-bot

mui-bot commented Jan 26, 2026

Copy link
Copy Markdown

Bundle size report

Bundle Parsed size Gzip size
@base-ui/react 0B(0.00%) 0B(0.00%)

Details of bundle changes


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

@netlify

netlify Bot commented Jan 26, 2026

Copy link
Copy Markdown

Deploy Preview for base-ui ready!

Name Link
🔨 Latest commit 050410d
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/6979c9fc032ccd0009ae6443
😎 Deploy Preview https://deploy-preview-3866--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.

@greptile-apps

greptile-apps Bot commented Jan 26, 2026

Copy link
Copy Markdown
Contributor

Greptile Overview

Greptile Summary

Updated ref types from HTMLInputElement to HTMLElement in FieldControl and Input components to support <textarea /> elements via the render prop.

Key Changes

  • FieldControl and Input now accept React.ForwardedRef<HTMLElement> instead of React.ForwardedRef<HTMLInputElement>
  • Added spec files demonstrating type-checking for textarea and contentEditable elements

Issues Found

  • The contentEditable example in FieldControl.spec.tsx:9-11 will cause runtime errors because the validation system expects elements with .validity, .validationMessage, and .setCustomValidity() methods, which plain HTMLElement doesn't have
  • Internal type mismatch: validation.inputRef is still typed as React.RefObject<HTMLInputElement | null> in useFieldValidation.ts:59, creating a type inconsistency with the broadened ref type

Confidence Score: 3/5

  • This PR introduces a type mismatch that allows invalid element types, though the primary use case (textarea) works correctly
  • While the change successfully enables <textarea /> support (the stated goal), the broader HTMLElement type allows contentEditable divs that will crash at runtime when validation occurs. The internal validation system still expects HTMLInputElement properties.
  • Pay close attention to FieldControl.spec.tsx - the contentEditable example needs removal or the validation system needs updates to support it

Important Files Changed

Filename Overview
packages/react/src/field/control/FieldControl.tsx Updated ref type from HTMLInputElement to HTMLElement, enabling textarea support but introducing type mismatch with internal validation system
packages/react/src/input/Input.tsx Updated ref type from HTMLInputElement to HTMLElement to match FieldControl changes
packages/react/src/field/control/FieldControl.spec.tsx Added type-checking tests for textarea and contentEditable elements, but contentEditable will fail at runtime
packages/react/src/input/Input.spec.tsx Added type-checking test for textarea element (valid use case)

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment thread packages/react/src/field/control/FieldControl.spec.tsx Outdated
@mj12albert mj12albert force-pushed the field-control-ref-type branch from f2022a3 to 2b6592b Compare January 26, 2026 14:37
export const FieldControl = React.forwardRef(function FieldControl(
componentProps: FieldControl.Props,
forwardedRef: React.ForwardedRef<HTMLInputElement>,
forwardedRef: React.ForwardedRef<HTMLElement>,

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.

Wouldn't this be more correct?

Suggested change
forwardedRef: React.ForwardedRef<HTMLElement>,
forwardedRef: React.ForwardedRef<HTMLInputElement | HTMLTextAreaElement>,

Could also update specs with testing both versions.

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 think it could actually support <div contentEditable>

The contentEditable example in FieldControl.spec.tsx:9-11 will cause runtime errors because the validation system expects elements with .validity

Regarding this comment from the bot, I don't think it really matters because TS would reject validation props passed to a div anyway?

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.

Yeah, good point, we can keep the added flexibility.
In any case, the user needs to define the actual type that they are passing to the component.

One minor gripe is that the default ref type (without any explicit render) is going to become more generic, even hough we know that it renders an <input>

export const FieldControl = React.forwardRef(function FieldControl(
componentProps: FieldControl.Props,
forwardedRef: React.ForwardedRef<HTMLInputElement>,
forwardedRef: React.ForwardedRef<HTMLElement>,

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.

Yeah, good point, we can keep the added flexibility.
In any case, the user needs to define the actual type that they are passing to the component.

One minor gripe is that the default ref type (without any explicit render) is going to become more generic, even hough we know that it renders an <input>

@mj12albert mj12albert merged commit 7ca58b9 into mui:master Jan 28, 2026
23 checks passed
@mj12albert mj12albert deleted the field-control-ref-type branch January 28, 2026 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component: input Changes related to the input component. typescript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants