@@ -287,19 +287,6 @@ type HTMLInputProps = Extract<
287
287
keyof InputHTMLAttributes < HTMLInputElement >
288
288
> ;
289
289
290
- const defaultProps : Partial < InputProps > = {
291
- type : HTMLInputTypes . text ,
292
- inputMode : 'verbatim' ,
293
- n_blur : 0 ,
294
- n_blur_timestamp : - 1 ,
295
- n_submit : 0 ,
296
- n_submit_timestamp : - 1 ,
297
- debounce : false ,
298
- step : 'any' ,
299
- persisted_props : [ PersistedProps . value ] ,
300
- persistence_type : PersistenceTypes . local ,
301
- } ;
302
-
303
290
/**
304
291
* A basic HTML input control for entering text, numbers, or passwords.
305
292
*
@@ -308,38 +295,25 @@ const defaultProps: Partial<InputProps> = {
308
295
* are also supported through separate components.
309
296
*/
310
297
function Input ( {
311
- type = defaultProps . type ,
312
- inputMode = defaultProps . inputMode ,
313
- n_blur = defaultProps . n_blur ,
314
- n_blur_timestamp = defaultProps . n_blur_timestamp ,
315
- n_submit = defaultProps . n_submit ,
316
- n_submit_timestamp = defaultProps . n_submit_timestamp ,
317
- debounce = defaultProps . debounce ,
318
- step = defaultProps . step ,
319
- persisted_props = defaultProps . persisted_props ,
320
- persistence_type = defaultProps . persistence_type ,
321
- ...rest
298
+ type = HTMLInputTypes . text ,
299
+ inputMode = 'verbatim' ,
300
+ n_blur = 0 ,
301
+ n_blur_timestamp = - 1 ,
302
+ n_submit = 0 ,
303
+ n_submit_timestamp = - 1 ,
304
+ debounce = false ,
305
+ step = 'any' ,
306
+ persisted_props = [ PersistedProps . value ] ,
307
+ persistence_type = PersistenceTypes . local ,
308
+ disabled,
309
+ ...props
322
310
} : InputProps ) {
323
- const props = {
324
- type,
325
- inputMode,
326
- n_blur,
327
- n_blur_timestamp,
328
- n_submit,
329
- n_submit_timestamp,
330
- debounce,
331
- step,
332
- persisted_props,
333
- persistence_type,
334
- ...rest ,
335
- } ;
336
311
const input = useRef ( document . createElement ( 'input' ) ) ;
337
312
const [ value , setValue ] = useState < InputProps [ 'value' ] > ( props . value ) ;
338
313
const [ pendingEvent , setPendingEvent ] = useState < number > ( ) ;
339
314
const inputId = useState ( ( ) => uniqid ( 'input-' ) ) [ 0 ] ;
340
315
341
- const valprops =
342
- props . type === HTMLInputTypes . number ? { } : { value : value ?? '' } ;
316
+ const valprops = type === HTMLInputTypes . number ? { } : { value : value ?? '' } ;
343
317
let { className} = props ;
344
318
className = 'dash-input' + ( className ? ` ${ className } ` : '' ) ;
345
319
@@ -356,49 +330,46 @@ function Input({
356
330
[ props . setProps ]
357
331
) ;
358
332
359
- const onEvent = useCallback ( ( ) => {
333
+ const onEvent = ( ) => {
360
334
const { value : inputValue } = input . current ;
361
- const { setProps} = props ;
362
335
const valueAsNumber = convert ( inputValue ) ;
363
- if ( props . type === HTMLInputTypes . number ) {
336
+ if ( type === HTMLInputTypes . number ) {
364
337
setPropValue ( props . value , valueAsNumber ?? value ) ;
365
338
} else {
366
339
const propValue =
367
340
inputValue === '' && props . value === undefined
368
341
? undefined
369
342
: inputValue ;
370
- setProps ( { value : propValue } ) ;
343
+ props . setProps ( { value : propValue } ) ;
371
344
}
372
345
setPendingEvent ( undefined ) ;
373
- } , [ props . setProps ] ) ;
346
+ } ;
374
347
375
348
const onBlur = useCallback ( ( ) => {
376
- const { debounce, n_blur, setProps} = props ;
377
- setProps ( {
349
+ props . setProps ( {
378
350
n_blur : ( n_blur ?? 0 ) + 1 ,
379
351
n_blur_timestamp : Date . now ( ) ,
380
352
} ) ;
381
353
input . current . checkValidity ( ) ;
382
354
return debounce === true && onEvent ( ) ;
383
- } , [ props . setProps , props . n_blur , props . debounce ] ) ;
355
+ } , [ n_blur , debounce ] ) ;
384
356
385
357
const onChange = useCallback ( ( ) => {
386
358
const { value} = input . current ;
387
359
setValue ( value ) ;
388
- } , [ setValue ] ) ;
360
+ } , [ ] ) ;
389
361
390
362
const onKeyPress : KeyboardEventHandler < HTMLInputElement > = useCallback (
391
363
( e : KeyboardEvent ) => {
392
- const { setProps} = props ;
393
364
if ( e . key === 'Enter' ) {
394
- setProps ( {
395
- n_submit : ( props . n_submit ?? 0 ) + 1 ,
365
+ props . setProps ( {
366
+ n_submit : ( n_submit ?? 0 ) + 1 ,
396
367
n_submit_timestamp : Date . now ( ) ,
397
368
} ) ;
398
369
}
399
- return props . debounce === true && e . key === 'Enter' && onEvent ( ) ;
370
+ return debounce === true && e . key === 'Enter' && onEvent ( ) ;
400
371
} ,
401
- [ props . setProps , props . n_submit , props . debounce ]
372
+ [ n_submit , debounce ]
402
373
) ;
403
374
404
375
const setInputValue = useCallback (
@@ -435,11 +406,11 @@ function Input({
435
406
const handleStepperClick = useCallback (
436
407
( direction : 'increment' | 'decrement' ) => {
437
408
const currentValue = parseFloat ( input . current . value ) || 0 ;
438
- const step = parseFloat ( props . step as string ) || 1 ;
409
+ const stepAsNum = parseFloat ( step as string ) || 1 ;
439
410
const newValue =
440
411
direction === 'increment'
441
- ? currentValue + step
442
- : currentValue - step ;
412
+ ? currentValue + stepAsNum
413
+ : currentValue - stepAsNum ;
443
414
444
415
// Apply min/max constraints
445
416
let constrainedValue = newValue ;
@@ -460,7 +431,7 @@ function Input({
460
431
setValue ( constrainedValue . toString ( ) ) ;
461
432
onEvent ( ) ;
462
433
} ,
463
- [ props . step , props . min , props . max , onEvent ]
434
+ [ step , props . min , props . max , onEvent ]
464
435
) ;
465
436
466
437
useEffect ( ( ) => {
@@ -470,18 +441,17 @@ function Input({
470
441
}
471
442
const valueAsNumber = convert ( value ) ;
472
443
setInputValue ( valueAsNumber ?? value , props . value ) ;
473
- if ( props . type !== HTMLInputTypes . number ) {
444
+ if ( type !== HTMLInputTypes . number ) {
474
445
setValue ( props . value ) ;
475
446
}
476
- } , [ props . value , props . type , pendingEvent ] ) ;
447
+ } , [ props . value , type , pendingEvent ] ) ;
477
448
478
449
useEffect ( ( ) => {
479
450
// Skip this effect if the value change came from props update (not user input)
480
451
if ( value === props . value ) {
481
452
return ;
482
453
}
483
454
484
- const { debounce, type} = props ;
485
455
const { selectionStart : cursorPosition } = input . current ;
486
456
if ( debounce ) {
487
457
if ( typeof debounce === 'number' && Number . isFinite ( debounce ) ) {
@@ -498,29 +468,35 @@ function Input({
498
468
} else {
499
469
onEvent ( ) ;
500
470
}
501
- } , [ value , props . debounce , props . type ] ) ;
471
+ } , [ value , debounce , type ] ) ;
502
472
503
- const pickedInputs = pick ( inputProps , props ) as Pick <
504
- InputHTMLAttributes < HTMLInputElement > ,
505
- HTMLInputProps
506
- > ;
473
+ const disabledAsBool = [ true , 'disabled' , 'DISABLED' ] . includes (
474
+ disabled ?? false
475
+ ) ;
476
+
477
+ const pickedInputs = pick ( inputProps , {
478
+ ...props ,
479
+ type,
480
+ inputMode,
481
+ step,
482
+ disabled : disabledAsBool ,
483
+ } ) as Pick < InputHTMLAttributes < HTMLInputElement > , HTMLInputProps > ;
507
484
508
- const isNumberInput = props . type === HTMLInputTypes . number ;
485
+ const isNumberInput = type === HTMLInputTypes . number ;
509
486
const currentNumericValue = convert ( input . current . value || '0' ) ;
510
487
const minValue = convert ( props . min ) ;
511
488
const maxValue = convert ( props . max ) ;
512
- const disabled = [ true , 'disabled' , 'DISABLED' ] . includes (
513
- props . disabled ?? false
514
- ) ;
515
- const isDecrementDisabled = disabled || currentNumericValue <= minValue ;
516
- const isIncrementDisabled = disabled || currentNumericValue >= maxValue ;
489
+ const isDecrementDisabled =
490
+ disabledAsBool || currentNumericValue <= minValue ;
491
+ const isIncrementDisabled =
492
+ disabledAsBool || currentNumericValue >= maxValue ;
517
493
518
494
return (
519
495
< LoadingElement >
520
496
{ loadingProps => (
521
497
< div
522
498
className = { `dash-input-container ${ className } ${
523
- props . type === HTMLInputTypes . hidden
499
+ type === HTMLInputTypes . hidden
524
500
? ' dash-input-hidden'
525
501
: ''
526
502
} `. trim ( ) }
@@ -536,7 +512,7 @@ function Input({
536
512
{ ...valprops }
537
513
{ ...pickedInputs }
538
514
{ ...loadingProps }
539
- disabled = { disabled }
515
+ disabled = { disabledAsBool }
540
516
/>
541
517
{ isNumberInput && (
542
518
< button
@@ -568,9 +544,9 @@ function Input({
568
544
) ;
569
545
}
570
546
571
- Input . dashPersistence = pick (
572
- [ ' persisted_props' , 'persistence_type' ] ,
573
- defaultProps
574
- ) ;
547
+ Input . dashPersistence = {
548
+ persisted_props : [ PersistedProps . value ] ,
549
+ persistence_type : PersistenceTypes . local ,
550
+ } ;
575
551
576
552
export default Input ;
0 commit comments