Skip to content

[otp field] Create component from Base UI equivalent - #49875

Open
silviuaavram wants to merge 17 commits into
mui:masterfrom
silviuaavram:feat/otp-field
Open

silviuaavram wants to merge 17 commits into
mui:masterfrom
silviuaavram:feat/otp-field

Conversation

@silviuaavram

@silviuaavram silviuaavram commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Adds OTPField to @mui/material for entering one-time passwords and verification codes. It uses Base UI’s OTP Field primitives and renders six numeric inputs by default. The value is a single string, so a PIN such as "0012" keeps its leading zeros in React state and form submissions.

<OTPField label="Verification code" name="code" />

Documentation preview

Features

  • Supports controlled and uncontrolled values, pasting a full code, and one-time-code autofill in supported browsers. Use length, validationType, and normalizeValue for formats such as alphanumeric recovery codes.
  • Exposes onValueChange, onValueInvalid, and onValueComplete. Set autoSubmit to submit the associated form when the code is complete.
  • Supports labels, helper text, error, disabled and read-only states, required fields, and character masking.
  • Provides small and medium sizes, theme colors for the focus outline, and fullWidth. The default field shrinks to fit narrow containers. With fullWidth, the inputs share the available space and separators keep their natural width.

Base UI component mapping

Base UI part Material UI API Responsibility
OTPField.Root group slot Hosts the character row and manages the value, keyboard navigation, and form submission.
OTPField.Input OTPFieldInput Renders one styled character input. The field creates these by default.
OTPField.Separator OTPFieldSeparator Adds a decorative separator when composing inputs into groups.

The Material-only slots are root (FormControl), label (FormLabel), and formHelperText (FormHelperText). The forwarded ref targets the outer container, and inputRef targets the first character input. Typed slots, render, sx, utility classes, and MuiOTPField theme overrides provide customization.

Accessibility and coverage

The field is one tab stop. The group and first input share the field label, and subsequent inputs announce their position using the theme locale, such as “Character 2 of 6.” Helper text describes the group and each input. Arrow-key navigation follows the field direction.

Adds API pages for all three components and 11 demos, including a grouped recovery code and a PIN submitted through a form. Tests cover composition, refs, label and helper text associations, RTL navigation, form values, theme overrides, and TypeScript augmentation.

@silviuaavram silviuaavram added type: new feature Expand the scope of the product to solve a new problem. scope: otp field component: otp field labels Nov 23, 2026
@code-infra-dashboard

code-infra-dashboard Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Deploy preview

Bundle size

Bundle Parsed size Gzip size
@mui/material 🔺+28.1KB(+4.91%) 🔺+10.1KB(+6.18%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes

Performance

No regressions in 32 benchmarks · precise to ±7.8% (±23.7% at worst) · ran 3m 52s · details


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

@silviuaavram
silviuaavram marked this pull request as ready for review November 26, 2026 09:33
@silviuaavram
silviuaavram requested review from LukasTy, aarongarciah, mnajdova and siriwatknp and a balanced review from Copilot November 26, 2026 09:33

Copilot AI 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.

🟢 Approval recommended

The review identifies no active issues requiring changes to the OTPField addition.

No open issues 🎉

1 file not reviewed
  • pnpm-lock.yaml: Generated file
What changed in this PR

Adds OTPField to Material UI, using Base UI for verification-code editing and form behavior while providing Material styling, theming, and accessible labels. Codes remain strings, preserving leading zeroes.

Changes:

  • Adds composable field, input, and separator components with customizable slots, refs, and localized character labels.
  • Integrates package exports, dependencies, theme types, and runtime and TypeScript coverage.
  • Adds API documentation and demos covering validation, forms, composition, and styling.
File Description
test/​moduleAugmentation/​material/​otpFieldCustomSize.tsconfig.json Configures the augmentation fixture.
test/​moduleAugmentation/​material/​otpFieldCustomSize.spec.tsx Tests custom colors, sizes, and theme overrides.
scripts/​generateProptypes.ts Configures inherited OTP prop documentation.
pnpm-lock.yaml Records Material UI’s Base UI dependency.
packages/​mui-material/​src/​styles/​props.ts Registers OTPField theme prop types.
packages/​mui-material/​src/​styles/​overrides.ts Registers OTPField style override keys.
packages/​mui-material/​src/​styles/​components.ts Adds OTPField theme configuration.
packages/​mui-material/​src/​OTPFieldSeparator/​OTPFieldSeparator.tsx Implements the decorative separator.
packages/​mui-material/​src/​OTPFieldSeparator/​OTPFieldSeparator.test.tsx Tests separator content, classes, and semantics.
packages/​mui-material/​src/​OTPFieldSeparator/​index.ts Exports the separator and its props.
packages/​mui-material/​src/​OTPFieldInput/​OTPFieldInputElement.tsx Styles inputs and composes rendering refs.
packages/​mui-material/​src/​OTPFieldInput/​OTPFieldInput.tsx Integrates input behavior and accessible attributes.
packages/​mui-material/​src/​OTPFieldInput/​OTPFieldInput.test.tsx Tests refs, composition, and form values.
packages/​mui-material/​src/​OTPFieldInput/​index.ts Exports the input and its props.
packages/​mui-material/​src/​OTPField/​OTPFieldLayout.tsx Connects layout slots, labels, descriptions, and direction.
packages/​mui-material/​src/​OTPField/​OTPFieldContext.ts Shares field state with composed children.
packages/​mui-material/​src/​OTPField/​otpFieldClasses.ts Defines OTPField utility classes.
packages/​mui-material/​src/​OTPField/​OTPField.types.ts Defines public props, slots, and owner state.
packages/​mui-material/​src/​OTPField/​OTPField.tsx Implements the Material adapter and defaults.
packages/​mui-material/​src/​OTPField/​OTPField.test.tsx Tests structure, accessibility, customization, and styling.
packages/​mui-material/​src/​OTPField/​OTPField.spec.tsx Tests public API types and invalid prop combinations.
packages/​mui-material/​src/​OTPField/​index.ts Exports components, classes, and Base UI types.
packages/​mui-material/​src/​locale/​zhTW.ts Adds Traditional Chinese character-position labels.
packages/​mui-material/​src/​locale/​zhHK.ts Adds Hong Kong Chinese character-position labels.
packages/​mui-material/​src/​locale/​zhCN.ts Adds Simplified Chinese character-position labels.
packages/​mui-material/​src/​locale/​viVN.ts Adds Vietnamese character-position labels.
packages/​mui-material/​src/​locale/​utils/​LocaleTextApi.ts Types localized OTPField labels.
packages/​mui-material/​src/​locale/​urPK.ts Adds Urdu character-position labels.
packages/​mui-material/​src/​locale/​ukUA.ts Adds Ukrainian character-position labels.
packages/​mui-material/​src/​locale/​trTR.ts Adds Turkish character-position labels.
packages/​mui-material/​src/​locale/​tlTL.ts Adds Tagalog character-position labels.
packages/​mui-material/​src/​locale/​thTH.ts Adds Thai character-position labels.
packages/​mui-material/​src/​locale/​svSE.ts Adds Swedish character-position labels.
packages/​mui-material/​src/​locale/​srRS.ts Adds Serbian character-position labels.
packages/​mui-material/​src/​locale/​skSK.ts Adds Slovak character-position labels.
packages/​mui-material/​src/​locale/​siLK.ts Adds Sinhala character-position labels.
packages/​mui-material/​src/​locale/​ruRU.ts Adds Russian character-position labels.
packages/​mui-material/​src/​locale/​roRO.ts Adds Romanian character-position labels.
packages/​mui-material/​src/​locale/​ptPT.ts Adds European Portuguese character-position labels.
packages/​mui-material/​src/​locale/​ptBR.ts Adds Brazilian Portuguese character-position labels.
packages/​mui-material/​src/​locale/​psAF.ts Adds Pashto character-position labels.
packages/​mui-material/​src/​locale/​plPL.ts Adds Polish character-position labels.
packages/​mui-material/​src/​locale/​nnNO.ts Adds Norwegian Nynorsk character-position labels.
packages/​mui-material/​src/​locale/​nlNL.ts Adds Dutch character-position labels.
packages/​mui-material/​src/​locale/​neNP.ts Adds Nepali character-position labels.
packages/​mui-material/​src/​locale/​nbNO.ts Adds Norwegian Bokmål character-position labels.
packages/​mui-material/​src/​locale/​myMY.ts Adds Burmese character-position labels.
packages/​mui-material/​src/​locale/​msMS.ts Adds Malay character-position labels.
packages/​mui-material/​src/​locale/​mkMK.ts Adds Macedonian character-position labels.
packages/​mui-material/​src/​locale/​locale.test.tsx Tests localized OTP input names.
packages/​mui-material/​src/​locale/​kuLatn.ts Adds Kurdish Latin character-position labels.
packages/​mui-material/​src/​locale/​kuCKB.ts Adds Central Kurdish character-position labels.
packages/​mui-material/​src/​locale/​koKR.ts Adds Korean character-position labels.
packages/​mui-material/​src/​locale/​kmKH.ts Adds Khmer character-position labels.
packages/​mui-material/​src/​locale/​kkKZ.ts Adds Kazakh character-position labels.
packages/​mui-material/​src/​locale/​jaJP.ts Adds Japanese character-position labels.
packages/​mui-material/​src/​locale/​itIT.ts Adds Italian character-position labels.
packages/​mui-material/​src/​locale/​isIS.ts Adds Icelandic character-position labels.
packages/​mui-material/​src/​locale/​idID.ts Adds Indonesian labels and number formatting.
packages/​mui-material/​src/​locale/​hyAM.ts Adds Armenian labels and number formatting.
packages/​mui-material/​src/​locale/​huHU.ts Adds Hungarian labels and number formatting.
packages/​mui-material/​src/​locale/​hrHR.ts Adds Croatian character-position labels.
packages/​mui-material/​src/​locale/​hiIN.ts Adds Hindi character-position labels.
packages/​mui-material/​src/​locale/​heIL.ts Adds Hebrew character-position labels.
packages/​mui-material/​src/​locale/​frFR.ts Adds French character-position labels.
packages/​mui-material/​src/​locale/​fiFI.ts Adds Finnish character-position labels.
packages/​mui-material/​src/​locale/​faIR.ts Adds Persian character-position labels.
packages/​mui-material/​src/​locale/​etEE.ts Adds Estonian character-position labels.
packages/​mui-material/​src/​locale/​esES.ts Adds Spanish character-position labels.
packages/​mui-material/​src/​locale/​enUS.ts Documents default English character-position labels.
packages/​mui-material/​src/​locale/​elGR.ts Adds Greek character-position labels.
packages/​mui-material/​src/​locale/​deDE.ts Adds German character-position labels.
packages/​mui-material/​src/​locale/​daDK.ts Adds Danish character-position labels.
packages/​mui-material/​src/​locale/​csCZ.ts Adds Czech character-position labels.
packages/​mui-material/​src/​locale/​caES.ts Adds Catalan character-position labels.
packages/​mui-material/​src/​locale/​bnBD.ts Adds Bengali character-position labels.
packages/​mui-material/​src/​locale/​bgBG.ts Adds Bulgarian labels and number formatting.
packages/​mui-material/​src/​locale/​beBY.ts Adds Belarusian character-position labels.
packages/​mui-material/​src/​locale/​azAZ.ts Adds Azerbaijani labels and number formatting.
packages/​mui-material/​src/​locale/​arSD.ts Adds Sudanese Arabic character-position labels.
packages/​mui-material/​src/​locale/​arSA.ts Adds Saudi Arabic character-position labels.
packages/​mui-material/​src/​locale/​arEG.ts Adds Egyptian Arabic character-position labels.
packages/​mui-material/​src/​locale/​amET.ts Adds Amharic character-position labels.
packages/​mui-material/​src/​index.js Adds public OTP exports.
packages/​mui-material/​src/​index.d.ts Adds public OTP type exports.
packages/​mui-material/​src/​FormLabel/​FormLabel.d.ts Adds an OTP documentation link in JSDoc.
packages/​mui-material/​src/​FormHelperText/​FormHelperText.d.ts Adds an OTP documentation link in JSDoc.
packages/​mui-material/​src/​FormControl/​FormControl.d.ts Adds an OTP documentation link in JSDoc.
packages/​mui-material/​package.json Declares Base UI and OTP subpath exports.
docs/​translations/​api-docs/​otp-field/​otp-field.json Adds field API descriptions.
docs/​translations/​api-docs/​otp-field-separator/​otp-field-separator.json Adds separator API descriptions.
docs/​translations/​api-docs/​otp-field-input/​otp-field-input.json Adds input API descriptions.
docs/​pages/​material-ui/​react-otp-field.js Adds the component documentation route.
docs/​pages/​material-ui/​api/​otp-field.json Defines field API page metadata.
docs/​pages/​material-ui/​api/​otp-field.js Renders the field API page.
docs/​pages/​material-ui/​api/​otp-field-separator.json Defines separator API page metadata.
docs/​pages/​material-ui/​api/​otp-field-separator.js Renders the separator API page.
docs/​pages/​material-ui/​api/​otp-field-input.json Defines input API page metadata.
docs/​pages/​material-ui/​api/​otp-field-input.js Renders the input API page.
docs/​pages/​material-ui/​api/​form-label.json Links FormLabel documentation to OTP demos.
docs/​pages/​material-ui/​api/​form-helper-text.json Links FormHelperText documentation to OTP demos.
docs/​pages/​material-ui/​api/​form-control.json Links FormControl documentation to OTP demos.
docs/​data/​material/​pagesApi.js Registers the three OTP API pages.
docs/​data/​material/​pages.ts Adds OTPField to component navigation.
docs/​data/​material/​components/​otp-field/​otp-field.md Documents usage, customization, forms, and accessibility.
docs/​data/​material/​components/​otp-field/​demos/​validation/​ValidationOTPField.tsx Demonstrates invalid-character feedback.
docs/​data/​material/​components/​otp-field/​demos/​validation/​index.ts Registers the validation demo.
docs/​data/​material/​components/​otp-field/​demos/​validation/​client.ts Defines the validation demo’s client boundary.
docs/​data/​material/​components/​otp-field/​demos/​theme/​OTPFieldTheme.tsx Demonstrates theme defaults and input overrides.
docs/​data/​material/​components/​otp-field/​demos/​theme/​index.ts Registers the theme demo.
docs/​data/​material/​components/​otp-field/​demos/​theme/​client.ts Defines the theme demo’s client boundary.
docs/​data/​material/​components/​otp-field/​demos/​state/​OTPFieldState.tsx Demonstrates controlled and uncontrolled values.
docs/​data/​material/​components/​otp-field/​demos/​state/​index.ts Registers the state demo.
docs/​data/​material/​components/​otp-field/​demos/​state/​client.ts Defines the state demo’s client boundary.
docs/​data/​material/​components/​otp-field/​demos/​sizes/​OTPFieldSizes.tsx Demonstrates small and medium inputs.
docs/​data/​material/​components/​otp-field/​demos/​sizes/​index.ts Registers the sizes demo.
docs/​data/​material/​components/​otp-field/​demos/​sizes/​client.ts Defines the sizes demo’s client boundary.
docs/​data/​material/​components/​otp-field/​demos/​render/​OTPFieldRender.tsx Demonstrates custom rendering and completion announcements.
docs/​data/​material/​components/​otp-field/​demos/​render/​index.ts Registers the render demo.
docs/​data/​material/​components/​otp-field/​demos/​render/​client.ts Defines the render demo’s client boundary.
docs/​data/​material/​components/​otp-field/​demos/​grouped/​index.ts Registers the grouped-code demo.
docs/​data/​material/​components/​otp-field/​demos/​grouped/​GroupedOTPField.tsx Demonstrates separators and uppercase normalization.
docs/​data/​material/​components/​otp-field/​demos/​grouped/​client.ts Defines the grouped-code demo’s client boundary.
docs/​data/​material/​components/​otp-field/​demos/​full-width/​index.ts Registers the full-width demo.
docs/​data/​material/​components/​otp-field/​demos/​full-width/​FullWidthOTPField.tsx Demonstrates equal-width expanding inputs.
docs/​data/​material/​components/​otp-field/​demos/​full-width/​client.ts Defines the full-width demo’s client boundary.
docs/​data/​material/​components/​otp-field/​demos/​form/​OTPFieldForm.tsx Demonstrates PIN submission, feedback, and refocusing.
docs/​data/​material/​components/​otp-field/​demos/​form/​index.ts Registers the form demo.
docs/​data/​material/​components/​otp-field/​demos/​form/​client.ts Defines the form demo’s client boundary.
docs/​data/​material/​components/​otp-field/​demos/​form-props/​index.ts Registers the form-props demo.
docs/​data/​material/​components/​otp-field/​demos/​form-props/​FormPropsOTPFields.tsx Demonstrates required, disabled, read-only, and masked fields.
docs/​data/​material/​components/​otp-field/​demos/​form-props/​client.ts Defines the form-props demo’s client boundary.
docs/​data/​material/​components/​otp-field/​demos/​color/​OTPFieldColors.tsx Demonstrates focus-outline colors.
docs/​data/​material/​components/​otp-field/​demos/​color/​index.ts Registers the color demo.
docs/​data/​material/​components/​otp-field/​demos/​color/​client.ts Defines the color demo’s client boundary.
docs/​data/​material/​components/​otp-field/​demos/​basic/​index.ts Registers the basic demo.
docs/​data/​material/​components/​otp-field/​demos/​basic/​client.ts Defines the basic demo’s client boundary.
docs/​data/​material/​components/​otp-field/​demos/​basic/​BasicOTPField.tsx Demonstrates the default six-digit field.

Review effort: Balanced

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

@silviuaavram I went through the API at a high level (up to 6c49a9b) before doing a line-by-line review. The overall shape is good: the inputs and separator are owned, only the field chrome is reused (FormControl, FormLabel, FormHelperText), render gets the Base UI state, slotProps callbacks get ownerState, and the label is static.

Main points

  1. Theme identity of the parts. OTPFieldInput and OTPFieldSeparator are public components, but they are styled under MuiOTPField (styleOverrides.input, MuiOTPField-input) and have no theme key, default props, or classes of their own. Menu2 gives each part its own key (MuiMenu2Item, etc.). We should follow one rule for exported parts.
  2. Visual match with the outlined TextField. The character boxes use a text.secondary border, have no hover state, and show a 2px outline on focus. OutlinedInput uses the 0.23 alpha border, text.primary on hover, and a 2px border on focus. An OTP field usually sits in a form next to TextFields, so I'd match those values, keeping the input owned. Related: there is no variant. Is outlined-only intended?
  3. MuiTextField theme defaults. Like NumberField, defaults such as size or margin set on MuiTextField don't reach OTPField. Whatever we decide there should apply here.

Smaller points

  1. slotProps.group as a callback receives ownerState, but the className and style callbacks of that same slot receive the Base UI state. I'd keep ownerState for slotProps and leave the Base UI state to render and data attributes.
  2. render is supported on the root and on the label, group, and formHelperText slots, on top of slots. OTPFieldLayout needs a fair amount of code to read id, aria-*, and dir back from render elements. label and formHelperText are Material-only slots, could we drop render there?
  3. Docs: same feedback as NumberField, the page shouldn't reference Base UI ("Data attributes" section and link, Anatomy).
  4. Nit: getInputAriaLabel. Slider uses getAriaLabel.

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

Follow-up in #49936, stacked on this PR's commits because they are still unmerged:

  1. Theme identity of the parts — no change. Picking one rule for exported parts (per-part MuiOTPFieldInput/MuiOTPFieldSeparator keys versus owned parts under MuiOTPField) needs a maintainer decision.
  2. Visual match with the outlined TextField — no change. Border/hover/focus parity and whether variant is in scope need a maintainer decision.
  3. MuiTextField theme defaults — no change. OTPField should follow whatever is decided for NumberField.
  4. slotProps.group callback state — done. className and style callbacks now receive Material ownerState; Base UI state remains with render and data attributes. Types, tests, and docs updated.
  5. render on label/formHelperText — no change. Dropping it is an API break and needs a maintainer decision.
  6. Docs — done. Removed the Base UI data-attributes section and link, Base UI references in Anatomy, and Base UI wording in generated slot descriptions.
  7. getInputAriaLabel — done. Renamed to getAriaLabel across props, locale defaults, API docs, and tests, matching Slider.

Narrow unit tests, ESLint, Prettier, pnpm typescript, pnpm proptypes, and pnpm docs:api pass on #49936.

This branch has not been deployed

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

Labels

component: otp field scope: otp field 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.

4 participants