@@ -2,6 +2,7 @@ import React from 'react';
22import PropTypes from 'prop-types' ;
33import mergeClassNames from 'merge-class-names' ;
44import updateInputWidth , { getFontShorthand } from 'update-input-width' ;
5+ import predictInputValue from '@wojtekmaj/predict-input-value' ;
56
67/* eslint-disable jsx-a11y/no-autofocus */
78
@@ -35,23 +36,14 @@ function updateInputWidthOnFontLoad(element) {
3536 document . fonts . addEventListener ( 'loadingdone' , onLoadingDone ) ;
3637}
3738
38- function getSelectionString ( ) {
39- if ( typeof window === 'undefined' ) {
40- return null ;
41- }
42-
43- return window . getSelection ( ) . toString ( ) ;
44- }
45-
46- function makeOnKeyPress ( maxLength ) {
39+ function makeOnKeyPress ( max ) {
4740 return function onKeyPress ( event ) {
48- const { key, target : input } = event ;
49- const { value } = input ;
41+ const { key } = event ;
5042
5143 const isNumberKey = ! isNaN ( parseInt ( key , 10 ) ) ;
52- const selection = getSelectionString ( ) ;
44+ const nextValue = predictInputValue ( event ) ;
5345
54- if ( isNumberKey && ( selection || value . length < maxLength ) ) {
46+ if ( isNumberKey && ( nextValue <= max ) ) {
5547 return ;
5648 }
5749
@@ -79,7 +71,6 @@ export default function Input({
7971 value,
8072} ) {
8173 const hasLeadingZero = showLeadingZeros && value !== null && value < 10 ;
82- const maxLength = max . toString ( ) . length ;
8374
8475 return [
8576 ( hasLeadingZero && < span key = "leadingZero" className = { `${ className } __leadingZero` } > 0</ span > ) ,
@@ -95,13 +86,14 @@ export default function Input({
9586 ) }
9687 data-input = "true"
9788 disabled = { disabled }
89+ inputMode = "numeric"
9890 max = { max }
9991 min = { min }
10092 name = { name }
10193 onChange = { onChange }
10294 onFocus = { onFocus }
10395 onKeyDown = { onKeyDown }
104- onKeyPress = { makeOnKeyPress ( maxLength ) }
96+ onKeyPress = { makeOnKeyPress ( max ) }
10597 onKeyUp = { ( event ) => {
10698 updateInputWidth ( event . target ) ;
10799
@@ -122,7 +114,7 @@ export default function Input({
122114 } }
123115 required = { required }
124116 step = { step }
125- type = "number "
117+ type = "text "
126118 value = { value !== null ? value : '' }
127119 /> ,
128120 ] ;
0 commit comments