Skip to content

Commit dd7476b

Browse files
committed
fix(input-otp): extract pattern to a function
1 parent cbf963d commit dd7476b

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

core/src/components/input-otp/input-otp.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,19 @@ export class InputOTP implements ComponentInterface {
304304
* Otherwise, use the default regex pattern based on type
305305
*/
306306
private get validKeys(): RegExp {
307-
const { pattern, type } = this;
307+
return new RegExp(`^${this.getPattern()}$`);
308+
}
308309

310+
/**
311+
* Gets the string pattern to pass to the input element
312+
* and use in the regex for allowed characters.
313+
*/
314+
private getPattern(): string {
315+
const { pattern, type } = this;
309316
if (pattern) {
310-
return new RegExp(`^${pattern}$`);
317+
return pattern;
311318
}
312-
return type === 'number' ? /^[0-9]$/ : /^[a-zA-Z0-9]$/;
319+
return type === 'number' ? '[0-9]' : '[a-zA-Z0-9]';
313320
}
314321

315322
/**
@@ -717,15 +724,14 @@ export class InputOTP implements ComponentInterface {
717724
inputRefs,
718725
inputValues,
719726
length,
720-
pattern,
721727
readonly,
722728
shape,
723729
size,
724-
type,
725730
} = this;
726731
const mode = getIonMode(this);
727732
const inputmode = this.getInputmode();
728733
const tabbableIndex = this.getTabbableIndex();
734+
const pattern = this.getPattern();
729735

730736
return (
731737
<Host
@@ -751,7 +757,7 @@ export class InputOTP implements ComponentInterface {
751757
autoCapitalize={autocapitalize}
752758
inputmode={inputmode}
753759
maxLength={1}
754-
pattern={pattern || (type === 'number' ? '[0-9]' : '[a-zA-Z0-9]')}
760+
pattern={pattern}
755761
disabled={disabled}
756762
readOnly={readonly}
757763
tabIndex={index === tabbableIndex ? 0 : -1}

0 commit comments

Comments
 (0)