From 8dbc110a19cc7379ced54c919d93070b4d009883 Mon Sep 17 00:00:00 2001 From: Guilherme Rodz Date: Thu, 22 Feb 2024 12:48:07 -0300 Subject: [PATCH] fix(input): always call onChange --- packages/input-otp/src/input.tsx | 10 +++++++++- packages/input-otp/src/types.ts | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/input-otp/src/input.tsx b/packages/input-otp/src/input.tsx index 398518f..0962ff8 100644 --- a/packages/input-otp/src/input.tsx +++ b/packages/input-otp/src/input.tsx @@ -30,7 +30,15 @@ export const OTPInput = React.forwardRef( // Workarounds const value = uncheckedValue ?? internalValue const previousValue = usePrevious(value) - const onChange = uncheckedOnChange ?? setInternalValue + const onChange = (newValue: string) => { + // Check if input is controlled + if (uncheckedValue !== undefined) { + uncheckedOnChange?.(newValue) + } else { + setInternalValue(newValue) + uncheckedOnChange?.(newValue) + } + } const regexp = pattern ? typeof pattern === 'string' ? new RegExp(pattern) diff --git a/packages/input-otp/src/types.ts b/packages/input-otp/src/types.ts index 8958257..5db65a4 100644 --- a/packages/input-otp/src/types.ts +++ b/packages/input-otp/src/types.ts @@ -8,7 +8,7 @@ export type OTPInputProps = OverrideProps< React.InputHTMLAttributes, { value?: string - onChange?: (...args: any[]) => unknown + onChange?: (newValue: string) => unknown maxLength: number