From 83bfaa51313a32ef11c2420b61e7c6dd2cf8fc38 Mon Sep 17 00:00:00 2001 From: Andrey Morozov Date: Mon, 10 Jun 2024 21:16:16 +0300 Subject: [PATCH] refactor: move validate function outside render --- src/components/PinInput/PinInput.tsx | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/components/PinInput/PinInput.tsx b/src/components/PinInput/PinInput.tsx index 48da358aba..8c8169e75b 100644 --- a/src/components/PinInput/PinInput.tsx +++ b/src/components/PinInput/PinInput.tsx @@ -41,6 +41,14 @@ const b = block('pin-input'); const NUMERIC_REGEXP = /[0-9]+/; const ALPHANUMERIC_REGEXP = /[0-9a-z]+/i; +const validate = (type: PinInputType, newValue: string) => { + if (type === 'numeric') { + return NUMERIC_REGEXP.test(newValue); + } else { + return ALPHANUMERIC_REGEXP.test(newValue); + } +}; + export const PinInput = React.forwardRef((props, ref) => { const { value, @@ -117,14 +125,6 @@ export const PinInput = React.forwardRef((props, } }; - const validate = (newValue: string) => { - if (type === 'numeric') { - return NUMERIC_REGEXP.test(newValue); - } else { - return ALPHANUMERIC_REGEXP.test(newValue); - } - }; - const setValuesAtIndex = (index: number, nextValue: string) => { // Normalize array size to length prop const newValues = Array.from({length}, (__, i) => values[i] ?? ''); @@ -159,7 +159,7 @@ export const PinInput = React.forwardRef((props, } } - if (!validate(nextValue)) { + if (!validate(type, nextValue)) { return; }