Skip to content

Commit 6cf03ea

Browse files
feat: OTPInput field value clear behaviour changes (#2524)
* field value clear behaviour changes for OTPInput * Create eighty-doors-yell.md --------- Co-authored-by: Anurag Hazra <anurag.hazra@razorpay.com>
1 parent 142afcd commit 6cf03ea

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

.changeset/eighty-doors-yell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@razorpay/blade": patch
3+
---
4+
5+
feat: Improve OTPInput field value clear behaviour

packages/blade/src/components/Input/OTPInput/OTPInput.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,15 @@ const _OTPInput: React.ForwardRefRenderFunction<HTMLInputElement[], OTPInputProp
294294
}: FormInputOnKeyDownEvent & { currentOtpIndex: number }): void => {
295295
if (key === 'Backspace' || code === 'Backspace' || code === 'Delete' || key === 'Delete') {
296296
event.preventDefault?.();
297-
handleOnChange({ value: '', currentOtpIndex });
298-
focusOnOtpByIndex(--currentOtpIndex);
297+
if (otpValue[currentOtpIndex]) {
298+
// Clear the value at the current index if value exists
299+
handleOnChange({ value: '', currentOtpIndex });
300+
} else {
301+
// Move focus to the previous input if the current input is empty
302+
// and clear the value at the new active (previous) index
303+
focusOnOtpByIndex(--currentOtpIndex);
304+
handleOnChange({ value: '', currentOtpIndex });
305+
}
299306
} else if (key === 'ArrowLeft' || code === 'ArrowLeft') {
300307
event.preventDefault?.();
301308
focusOnOtpByIndex(--currentOtpIndex);

packages/blade/src/components/Input/OTPInput/__tests__/OTPInput.web.test.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ describe('<OTPInput />', () => {
247247
expect(allInputs[5]).toHaveFocus();
248248
await user.keyboard('{Backspace}');
249249
expect(allInputs[5]).toHaveValue('');
250-
expect(allInputs[4]).toHaveFocus();
250+
expect(allInputs[5]).toHaveFocus();
251251
await user.keyboard('{Delete}');
252252
expect(allInputs[4]).toHaveValue('');
253-
expect(allInputs[3]).toHaveFocus();
253+
expect(allInputs[4]).toHaveFocus();
254254
});
255255

256256
it('should pass a11y', async () => {

0 commit comments

Comments
 (0)