1
1
import { pick } from 'ramda' ;
2
2
import React , {
3
+ InputHTMLAttributes ,
3
4
KeyboardEvent ,
4
5
KeyboardEventHandler ,
5
6
useCallback ,
6
7
useEffect ,
7
8
useRef ,
8
9
useState ,
9
- useId ,
10
10
} from 'react' ;
11
+ import uniqid from 'uniqid' ;
11
12
import fastIsNumeric from 'fast-isnumeric' ;
12
13
import LoadingElement from '../utils/_LoadingElement' ;
13
14
import './css/input.css' ;
@@ -260,7 +261,7 @@ type InputProps = {
260
261
type ?: HTMLInputTypes ;
261
262
} ;
262
263
263
- const inputProps : ( keyof InputProps ) [ ] = [
264
+ const inputProps = [
264
265
'type' ,
265
266
'placeholder' ,
266
267
'inputMode' ,
@@ -279,7 +280,12 @@ const inputProps: (keyof InputProps)[] = [
279
280
'maxLength' ,
280
281
'pattern' ,
281
282
'size' ,
282
- ] ;
283
+ ] as const ;
284
+
285
+ type HTMLInputProps = Extract <
286
+ ( typeof inputProps ) [ number ] ,
287
+ keyof InputHTMLAttributes < HTMLInputElement >
288
+ > ;
283
289
284
290
const defaultProps : Partial < InputProps > = {
285
291
type : HTMLInputTypes . text ,
@@ -330,7 +336,7 @@ function Input({
330
336
const input = useRef ( document . createElement ( 'input' ) ) ;
331
337
const [ value , setValue ] = useState < InputProps [ 'value' ] > ( props . value ) ;
332
338
const [ pendingEvent , setPendingEvent ] = useState < number > ( ) ;
333
- const inputId = useId ( ) ;
339
+ const inputId = useState ( ( ) => uniqid ( 'input-' ) ) [ 0 ] ;
334
340
335
341
const valprops =
336
342
props . type === HTMLInputTypes . number ? { } : { value : value ?? '' } ;
@@ -401,7 +407,11 @@ function Input({
401
407
value = convert ( value ) ;
402
408
403
409
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
+ }
405
415
}
406
416
} ,
407
417
[ ]
@@ -490,7 +500,10 @@ function Input({
490
500
}
491
501
} , [ value , props . debounce , props . type ] ) ;
492
502
493
- const pickedInputs = pick ( inputProps , props ) ;
503
+ const pickedInputs = pick ( inputProps , props ) as Pick <
504
+ InputHTMLAttributes < HTMLInputElement > ,
505
+ HTMLInputProps
506
+ > ;
494
507
495
508
const isNumberInput = props . type === HTMLInputTypes . number ;
496
509
const currentNumericValue = convert ( input . current . value || '0' ) ;
0 commit comments