Skip to content

Commit

Permalink
fix: OTP with controlled of '' not working (#48399)
Browse files Browse the repository at this point in the history
* test: test driven

* fix: controlled logic
  • Loading branch information
zombieJ committed Apr 11, 2024
1 parent f460b48 commit 4216003
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions components/input/OTP/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface OTPProps extends Omit<React.HTMLAttributes<HTMLDivElement>, 'on
}

function strToArr(str: string) {
return str.split('');
return (str || '').split('');
}

const OTP = React.forwardRef<OTPRef, OTPProps>((props, ref) => {
Expand Down Expand Up @@ -121,7 +121,7 @@ const OTP = React.forwardRef<OTPRef, OTPProps>((props, ref) => {
);

React.useEffect(() => {
if (value) {
if (value !== undefined) {
setValueCells(strToArr(value));
}
}, [value]);
Expand Down
9 changes: 9 additions & 0 deletions components/input/__tests__/otp.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ describe('Input.OTP', () => {

rerender(<OTP value="LITTLE" />);
expect(getText(container)).toBe('LITTLE');

rerender(<OTP value="" />);
expect(getText(container)).toBe('');

rerender(<OTP value="EXCEED-RANGE" />);
expect(getText(container)).toBe('EXCEED');

rerender(<OTP value={null!} />);
expect(getText(container)).toBe('');
});

it('focus to selection', async () => {
Expand Down

0 comments on commit 4216003

Please sign in to comment.