Skip to content

Commit a541fe8

Browse files
Merge pull request #1 from mattharris191/text-inputs
Switch to input type="text" and @wojtekmaj/predict-input-value packag…
2 parents 7dfa6bf + 3f0bd23 commit a541fe8

File tree

4 files changed

+17
-19
lines changed

4 files changed

+17
-19
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"license": "MIT",
4242
"dependencies": {
4343
"@wojtekmaj/date-utils": "^1.0.0",
44+
"@wojtekmaj/predict-input-value": "^1.0.0",
4445
"get-user-locale": "^1.2.0",
4546
"make-event-props": "^1.1.0",
4647
"merge-class-names": "^1.1.1",

src/TimeInput.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ function hoursAreDifferent(date1, date2) {
3131
);
3232
}
3333

34-
function isValidInput(element) {
35-
return element.tagName === 'INPUT' && element.type === 'number';
34+
function isInternalInput(element) {
35+
return element.tagName === 'INPUT' && element.getAttribute('data-input') === 'true';
3636
}
3737

3838
function findInput(element, property) {
3939
let nextElement = element;
4040
do {
4141
nextElement = nextElement[property];
42-
} while (nextElement && !isValidInput(nextElement));
42+
} while (nextElement && !isInternalInput(nextElement));
4343
return nextElement;
4444
}
4545

src/TimeInput/Input.jsx

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import mergeClassNames from 'merge-class-names';
44
import 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
];

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,6 +1196,11 @@
11961196
resolved "https://registry.yarnpkg.com/@wojtekmaj/date-utils/-/date-utils-1.0.2.tgz#31348c572606ac42aaf9b7bb22074fc0b163cc2b"
11971197
integrity sha512-sOu+uH3jzsECLg3YGH++/pLWs8S4eKiXMwMIcotE62CO9AB/HRyhZ0ISwann/30DLnfCw4skvr8h9gF3aafhPA==
11981198

1199+
"@wojtekmaj/predict-input-value@^1.0.0":
1200+
version "1.0.0"
1201+
resolved "https://registry.yarnpkg.com/@wojtekmaj/predict-input-value/-/predict-input-value-1.0.0.tgz#dab4b6fa18f7247843f2f1d7065e182efd715f6c"
1202+
integrity sha512-cAMCkp0Qv0BZdnSeTER66xW2LeCkf1H2NgVYoblb/F/TzewqbugTc/GeXsUQK79875GXIPSaAypo0sbKX0jwVQ==
1203+
11991204
abab@^2.0.3:
12001205
version "2.0.3"
12011206
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.3.tgz#623e2075e02eb2d3f2475e49f99c91846467907a"

0 commit comments

Comments
 (0)