Skip to content

Commit 0bfaa19

Browse files
committed
code review feedback
1 parent 11d7d36 commit 0bfaa19

File tree

1 file changed

+53
-77
lines changed
  • components/dash-core-components/src/components

1 file changed

+53
-77
lines changed

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

Lines changed: 53 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -287,19 +287,6 @@ type HTMLInputProps = Extract<
287287
keyof InputHTMLAttributes<HTMLInputElement>
288288
>;
289289

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-
303290
/**
304291
* A basic HTML input control for entering text, numbers, or passwords.
305292
*
@@ -308,38 +295,25 @@ const defaultProps: Partial<InputProps> = {
308295
* are also supported through separate components.
309296
*/
310297
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
322310
}: 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-
};
336311
const input = useRef(document.createElement('input'));
337312
const [value, setValue] = useState<InputProps['value']>(props.value);
338313
const [pendingEvent, setPendingEvent] = useState<number>();
339314
const inputId = useState(() => uniqid('input-'))[0];
340315

341-
const valprops =
342-
props.type === HTMLInputTypes.number ? {} : {value: value ?? ''};
316+
const valprops = type === HTMLInputTypes.number ? {} : {value: value ?? ''};
343317
let {className} = props;
344318
className = 'dash-input' + (className ? ` ${className}` : '');
345319

@@ -356,49 +330,46 @@ function Input({
356330
[props.setProps]
357331
);
358332

359-
const onEvent = useCallback(() => {
333+
const onEvent = () => {
360334
const {value: inputValue} = input.current;
361-
const {setProps} = props;
362335
const valueAsNumber = convert(inputValue);
363-
if (props.type === HTMLInputTypes.number) {
336+
if (type === HTMLInputTypes.number) {
364337
setPropValue(props.value, valueAsNumber ?? value);
365338
} else {
366339
const propValue =
367340
inputValue === '' && props.value === undefined
368341
? undefined
369342
: inputValue;
370-
setProps({value: propValue});
343+
props.setProps({value: propValue});
371344
}
372345
setPendingEvent(undefined);
373-
}, [props.setProps]);
346+
};
374347

375348
const onBlur = useCallback(() => {
376-
const {debounce, n_blur, setProps} = props;
377-
setProps({
349+
props.setProps({
378350
n_blur: (n_blur ?? 0) + 1,
379351
n_blur_timestamp: Date.now(),
380352
});
381353
input.current.checkValidity();
382354
return debounce === true && onEvent();
383-
}, [props.setProps, props.n_blur, props.debounce]);
355+
}, [n_blur, debounce]);
384356

385357
const onChange = useCallback(() => {
386358
const {value} = input.current;
387359
setValue(value);
388-
}, [setValue]);
360+
}, []);
389361

390362
const onKeyPress: KeyboardEventHandler<HTMLInputElement> = useCallback(
391363
(e: KeyboardEvent) => {
392-
const {setProps} = props;
393364
if (e.key === 'Enter') {
394-
setProps({
395-
n_submit: (props.n_submit ?? 0) + 1,
365+
props.setProps({
366+
n_submit: (n_submit ?? 0) + 1,
396367
n_submit_timestamp: Date.now(),
397368
});
398369
}
399-
return props.debounce === true && e.key === 'Enter' && onEvent();
370+
return debounce === true && e.key === 'Enter' && onEvent();
400371
},
401-
[props.setProps, props.n_submit, props.debounce]
372+
[n_submit, debounce]
402373
);
403374

404375
const setInputValue = useCallback(
@@ -435,11 +406,11 @@ function Input({
435406
const handleStepperClick = useCallback(
436407
(direction: 'increment' | 'decrement') => {
437408
const currentValue = parseFloat(input.current.value) || 0;
438-
const step = parseFloat(props.step as string) || 1;
409+
const stepAsNum = parseFloat(step as string) || 1;
439410
const newValue =
440411
direction === 'increment'
441-
? currentValue + step
442-
: currentValue - step;
412+
? currentValue + stepAsNum
413+
: currentValue - stepAsNum;
443414

444415
// Apply min/max constraints
445416
let constrainedValue = newValue;
@@ -460,7 +431,7 @@ function Input({
460431
setValue(constrainedValue.toString());
461432
onEvent();
462433
},
463-
[props.step, props.min, props.max, onEvent]
434+
[step, props.min, props.max, onEvent]
464435
);
465436

466437
useEffect(() => {
@@ -470,18 +441,17 @@ function Input({
470441
}
471442
const valueAsNumber = convert(value);
472443
setInputValue(valueAsNumber ?? value, props.value);
473-
if (props.type !== HTMLInputTypes.number) {
444+
if (type !== HTMLInputTypes.number) {
474445
setValue(props.value);
475446
}
476-
}, [props.value, props.type, pendingEvent]);
447+
}, [props.value, type, pendingEvent]);
477448

478449
useEffect(() => {
479450
// Skip this effect if the value change came from props update (not user input)
480451
if (value === props.value) {
481452
return;
482453
}
483454

484-
const {debounce, type} = props;
485455
const {selectionStart: cursorPosition} = input.current;
486456
if (debounce) {
487457
if (typeof debounce === 'number' && Number.isFinite(debounce)) {
@@ -498,29 +468,35 @@ function Input({
498468
} else {
499469
onEvent();
500470
}
501-
}, [value, props.debounce, props.type]);
471+
}, [value, debounce, type]);
502472

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>;
507484

508-
const isNumberInput = props.type === HTMLInputTypes.number;
485+
const isNumberInput = type === HTMLInputTypes.number;
509486
const currentNumericValue = convert(input.current.value || '0');
510487
const minValue = convert(props.min);
511488
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;
517493

518494
return (
519495
<LoadingElement>
520496
{loadingProps => (
521497
<div
522498
className={`dash-input-container ${className}${
523-
props.type === HTMLInputTypes.hidden
499+
type === HTMLInputTypes.hidden
524500
? ' dash-input-hidden'
525501
: ''
526502
}`.trim()}
@@ -536,7 +512,7 @@ function Input({
536512
{...valprops}
537513
{...pickedInputs}
538514
{...loadingProps}
539-
disabled={disabled}
515+
disabled={disabledAsBool}
540516
/>
541517
{isNumberInput && (
542518
<button
@@ -568,9 +544,9 @@ function Input({
568544
);
569545
}
570546

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+
};
575551

576552
export default Input;

0 commit comments

Comments
 (0)