Skip to content

Commit 4a81cbe

Browse files
committed
Fix for useId not available in React 16
1 parent 0c175f5 commit 4a81cbe

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

components/dash-core-components/package-lock.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/dash-core-components/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"@types/ramda": "^0.31.0",
7777
"@types/react": "^16.14.8",
7878
"@types/react-dom": "^16.9.13",
79+
"@types/uniqid": "^5.3.4",
7980
"@typescript-eslint/eslint-plugin": "^5.59.7",
8081
"@typescript-eslint/parser": "^5.59.7",
8182
"babel-loader": "^9.2.1",

components/dash-core-components/src/components/Input.tsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import {pick} from 'ramda';
22
import React, {
3+
InputHTMLAttributes,
34
KeyboardEvent,
45
KeyboardEventHandler,
56
useCallback,
67
useEffect,
78
useRef,
89
useState,
9-
useId,
1010
} from 'react';
11+
import uniqid from 'uniqid';
1112
import fastIsNumeric from 'fast-isnumeric';
1213
import LoadingElement from '../utils/_LoadingElement';
1314
import './css/input.css';
@@ -260,7 +261,7 @@ type InputProps = {
260261
type?: HTMLInputTypes;
261262
};
262263

263-
const inputProps: (keyof InputProps)[] = [
264+
const inputProps = [
264265
'type',
265266
'placeholder',
266267
'inputMode',
@@ -279,7 +280,12 @@ const inputProps: (keyof InputProps)[] = [
279280
'maxLength',
280281
'pattern',
281282
'size',
282-
];
283+
] as const;
284+
285+
type HTMLInputProps = Extract<
286+
(typeof inputProps)[number],
287+
keyof InputHTMLAttributes<HTMLInputElement>
288+
>;
283289

284290
const defaultProps: Partial<InputProps> = {
285291
type: HTMLInputTypes.text,
@@ -330,7 +336,7 @@ function Input({
330336
const input = useRef(document.createElement('input'));
331337
const [value, setValue] = useState<InputProps['value']>(props.value);
332338
const [pendingEvent, setPendingEvent] = useState<number>();
333-
const inputId = useId();
339+
const inputId = useState(() => uniqid('input-'))[0];
334340

335341
const valprops =
336342
props.type === HTMLInputTypes.number ? {} : {value: value ?? ''};
@@ -401,7 +407,11 @@ function Input({
401407
value = convert(value);
402408

403409
if (!isEquivalent(base, value)) {
404-
input.current.value = `${value}` ?? '';
410+
if (typeof value === 'undefined') {
411+
input.current.value = '';
412+
} else {
413+
input.current.value = `${value}`;
414+
}
405415
}
406416
},
407417
[]
@@ -490,7 +500,10 @@ function Input({
490500
}
491501
}, [value, props.debounce, props.type]);
492502

493-
const pickedInputs = pick(inputProps, props);
503+
const pickedInputs = pick(inputProps, props) as Pick<
504+
InputHTMLAttributes<HTMLInputElement>,
505+
HTMLInputProps
506+
>;
494507

495508
const isNumberInput = props.type === HTMLInputTypes.number;
496509
const currentNumericValue = convert(input.current.value || '0');

0 commit comments

Comments
 (0)