Skip to content

Commit 172586a

Browse files
authored
fix: when specifying the type of One-Time Password Field, the input is displayed as pw. (#3510)
1 parent 4140bd6 commit 172586a

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

.changeset/fluffy-days-kneel.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@radix-ui/react-one-time-password-field': patch
3+
---
4+
5+
6+
7+
- As stated in the [documentation](https://www.radix-ui.com/primitives/docs/components/one-time-password-field#root), change default to text.
8+
- Connect a type to input to display the input value in a different way when the type changes.
9+
- Add test for this feature
10+

packages/react/one-time-password-field/src/one-time-password-field.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ describe('given a default OneTimePasswordField', () => {
3232
expect(await axe(rendered.container)).toHaveNoViolations();
3333
});
3434

35+
it('should mask input value when type is password', async () => {
36+
rendered.rerender(
37+
<OneTimePasswordField.Root type="password">
38+
<OneTimePasswordField.Input />
39+
<OneTimePasswordField.HiddenInput name="code" />
40+
</OneTimePasswordField.Root>
41+
);
42+
43+
const input = rendered.container.querySelector(
44+
'input:not([type="hidden"])'
45+
) as HTMLInputElement;
46+
47+
await userEvent.type(input, '1');
48+
expect(input.type).toBe('password');
49+
50+
const hiddenInput = rendered.container.querySelector(
51+
'input[type="hidden"]'
52+
) as HTMLInputElement;
53+
expect(hiddenInput.value).toBe('1');
54+
});
55+
3556
// TODO: userEvent paste not behaving as expected. Debug and unskip.
3657
// Replicated in storybook for now.
3758
it.todo('pastes the code into the input', async () => {

packages/react/one-time-password-field/src/one-time-password-field.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ const OneTimePasswordField = React.forwardRef<HTMLDivElement, OneTimePasswordFie
211211
form,
212212
name,
213213
placeholder,
214-
type = 'password',
214+
type = 'text',
215215
// TODO: Change default to vertical when inputs use vertical writing mode
216216
orientation = 'horizontal',
217217
dir,
@@ -638,7 +638,7 @@ const OneTimePasswordFieldInput = React.forwardRef<
638638
return (
639639
<Primitive.Root.input
640640
ref={composedInputRef}
641-
type="text"
641+
type={context.type}
642642
aria-label={`Character ${index + 1} of ${collection.size}`}
643643
autoComplete={supportsAutoComplete ? context.autoComplete : 'off'}
644644
data-1p-ignore={supportsAutoComplete ? undefined : 'true'}

0 commit comments

Comments
 (0)