Skip to content

Commit ea77943

Browse files
authored
fix: enable single-field editing in OTP input (#8307)
* fix: enable single-field editing in OTP input * npm run lint
1 parent 541ba8a commit ea77943

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

components/lib/inputotp/InputOtp.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,12 @@ export const InputOtp = React.memo(
185185

186186
default: {
187187
//Prevent non-numeric characters from being entered if integerOnly is true or if the length of the input is greater than the specified length
188-
if ((props?.integerOnly && !(event.code !== 'Space' && Number(event.key) >= 0 && Number(event.key) <= 9)) || (tokens.join('').length >= props.length && event.code !== 'Delete')) {
188+
const target = event.target;
189+
const hasSelection = target.selectionStart !== target.selectionEnd;
190+
const isAtMaxLength = tokens.join('').length >= props.length;
191+
const isValidKey = props.integerOnly ? /^[0-9]$/.test(event.key) : true;
192+
193+
if (!isValidKey || (isAtMaxLength && event.code !== 'Delete' && !hasSelection)) {
189194
event.preventDefault();
190195
}
191196

0 commit comments

Comments
 (0)