Skip to content

Commit 5f388fc

Browse files
fix: virtual keyboard on mobile should be based on the allowed keys (#4411)
* fix: virtual keyboard on mobile should be based on the allowed keys * chore: applying junior's suggestions * chore: add inputmode prop, update changeset --------- Co-authored-by: Junior Garcia <[email protected]>
1 parent de0e277 commit 5f388fc

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

.changeset/cyan-donkeys-swim.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@nextui-org/input-otp": patch
3+
"@nextui-org/shared-utils": patch
4+
---
5+
6+
Fix virtual keyboard to display the keys based on allowedKeys(#4408)

packages/components/input-otp/src/use-input-otp.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from "@nextui-org/system";
1414
import {inputOtp} from "@nextui-org/theme";
1515
import {filterDOMProps, ReactRef, useDOMRef} from "@nextui-org/react-utils";
16-
import {clsx, dataAttr, objectToDeps} from "@nextui-org/shared-utils";
16+
import {clsx, dataAttr, objectToDeps, isPatternNumeric} from "@nextui-org/shared-utils";
1717
import {useCallback, useMemo} from "react";
1818
import {chain, mergeProps, useFormReset} from "@react-aria/utils";
1919
import {AriaTextFieldProps} from "@react-types/textfield";
@@ -120,6 +120,7 @@ export function useInputOtp(originalProps: UseInputOtpProps) {
120120
containerClassName,
121121
noScriptCSSFallback,
122122
onChange,
123+
inputMode,
123124
...otherProps
124125
} = props;
125126

@@ -237,22 +238,24 @@ export function useInputOtp(originalProps: UseInputOtpProps) {
237238
minLength: minLength ?? length,
238239
textAlign,
239240
ref: inputRef,
240-
name: name,
241-
value: value,
241+
name,
242+
value,
242243
autoFocus,
243244
onChange: setValue,
244245
onBlur: chain(focusProps.onBlur, props?.onBlur),
245-
onComplete: onComplete,
246+
onComplete,
246247
pushPasswordManagerStrategy,
247248
pasteTransformer,
248249
noScriptCSSFallback,
250+
inputMode: inputMode ?? (isPatternNumeric(allowedKeys) ? "numeric" : "text"),
249251
containerClassName: slots.wrapper?.({class: clsx(classNames?.wrapper, containerClassName)}),
250252
...props,
251253
};
252254

253255
return otpProps;
254256
},
255257
[
258+
inputMode,
256259
isRequired,
257260
isDisabled,
258261
isReadOnly,

packages/utilities/shared-utils/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from "./numbers";
88
export * from "./console";
99
export * from "./types";
1010
export * from "./dates";
11+
export * from "./regex";
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const isPatternNumeric = (pattern: string) => {
2+
const numericPattern = /(^|\W)[0-9](\W|$)/;
3+
4+
return numericPattern.test(pattern) && !/[^\d\^$\[\]\(\)\*\+\-\.\|]/.test(pattern);
5+
};

0 commit comments

Comments
 (0)