Skip to content

Commit

Permalink
fix: virtual keyboard on mobile should be based on the allowed keys (#…
Browse files Browse the repository at this point in the history
…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 <jrgarciadev@gmail.com>
  • Loading branch information
macci001 and jrgarciadev authored Dec 22, 2024
1 parent de0e277 commit 5f388fc
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/cyan-donkeys-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nextui-org/input-otp": patch
"@nextui-org/shared-utils": patch
---

Fix virtual keyboard to display the keys based on allowedKeys(#4408)
11 changes: 7 additions & 4 deletions packages/components/input-otp/src/use-input-otp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "@nextui-org/system";
import {inputOtp} from "@nextui-org/theme";
import {filterDOMProps, ReactRef, useDOMRef} from "@nextui-org/react-utils";
import {clsx, dataAttr, objectToDeps} from "@nextui-org/shared-utils";
import {clsx, dataAttr, objectToDeps, isPatternNumeric} from "@nextui-org/shared-utils";
import {useCallback, useMemo} from "react";
import {chain, mergeProps, useFormReset} from "@react-aria/utils";
import {AriaTextFieldProps} from "@react-types/textfield";
Expand Down Expand Up @@ -120,6 +120,7 @@ export function useInputOtp(originalProps: UseInputOtpProps) {
containerClassName,
noScriptCSSFallback,
onChange,
inputMode,
...otherProps
} = props;

Expand Down Expand Up @@ -237,22 +238,24 @@ export function useInputOtp(originalProps: UseInputOtpProps) {
minLength: minLength ?? length,
textAlign,
ref: inputRef,
name: name,
value: value,
name,
value,
autoFocus,
onChange: setValue,
onBlur: chain(focusProps.onBlur, props?.onBlur),
onComplete: onComplete,
onComplete,
pushPasswordManagerStrategy,
pasteTransformer,
noScriptCSSFallback,
inputMode: inputMode ?? (isPatternNumeric(allowedKeys) ? "numeric" : "text"),
containerClassName: slots.wrapper?.({class: clsx(classNames?.wrapper, containerClassName)}),
...props,
};

return otpProps;
},
[
inputMode,
isRequired,
isDisabled,
isReadOnly,
Expand Down
1 change: 1 addition & 0 deletions packages/utilities/shared-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from "./numbers";
export * from "./console";
export * from "./types";
export * from "./dates";
export * from "./regex";
5 changes: 5 additions & 0 deletions packages/utilities/shared-utils/src/regex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const isPatternNumeric = (pattern: string) => {
const numericPattern = /(^|\W)[0-9](\W|$)/;

return numericPattern.test(pattern) && !/[^\d\^$\[\]\(\)\*\+\-\.\|]/.test(pattern);
};

0 comments on commit 5f388fc

Please sign in to comment.