Skip to content

Commit

Permalink
fix: adds optimizations with callbacks & debouncing
Browse files Browse the repository at this point in the history
  • Loading branch information
2nikhiltom committed Sep 4, 2024
1 parent 2b4e86b commit d47ba9d
Showing 1 changed file with 55 additions and 52 deletions.
107 changes: 55 additions & 52 deletions packages/react/src/components/NumberInput/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@

import { Add, Subtract } from '@carbon/icons-react';
import cx from 'classnames';
import debounce from 'lodash.debounce';
import PropTypes from 'prop-types';
import React, {
ReactNode,
useContext,
useRef,
useState,
useEffect,
FC,
useCallback,
FC,
} from 'react';
import { useMergedRefs } from '../../internal/useMergedRefs';
import { useNormalizedInputProps as normalize } from '../../internal/useNormalizedInputProps';
Expand Down Expand Up @@ -304,24 +305,27 @@ const NumberInput = React.forwardRef<HTMLInputElement, NumberInputProps>(
ariaDescribedBy = helperText ? normalizedProps.helperId : undefined;
}

function handleOnChange(event) {
if (disabled) {
return;
}
const handleOnChange = useCallback(
(event) => {
if (disabled) {
return;
}

const state = {
value:
allowEmpty && event.target.value === ''
? ''
: Number(event.target.value),
direction: value < event.target.value ? 'up' : 'down',
};
setValue(state.value);

if (onChange) {
onChange(event, state);
}
}
const state = {
value:
allowEmpty && event.target.value === ''
? ''
: Number(event.target.value),
direction: value < event.target.value ? 'up' : 'down',
};
setValue(state.value);

if (onChange) {
onChange(event, state);
}
},
[disabled, allowEmpty, onChange, setValue, value]
);

const handleFocus: React.FocusEventHandler<
HTMLInputElement | HTMLDivElement
Expand All @@ -343,43 +347,42 @@ const NumberInput = React.forwardRef<HTMLInputElement, NumberInputProps>(
});

const Icon = normalizedProps.icon as any;

const handleStepperClick = useCallback(
(event, direction) => {
if (inputRef.current) {
const currentValue = Number(inputRef.current.value);
let newValue =
direction === 'up' ? currentValue + step : currentValue - step;
if (min !== undefined) {
newValue = Math.max(newValue, min);
}
if (max !== undefined) {
newValue = Math.min(newValue, max);
}
const currentInputValue = inputRef.current
? inputRef.current.value
: '';
const state = {
value:
allowEmpty && currentInputValue === '' && step === 0
? ''
: newValue,
direction: direction,
};
setValue(state.value);

if (onChange) {
onChange(event, state);
}

if (onClick) {
onClick(event, state);
}
const debouncedHandleStepperClick = useCallback(
debounce((event, direction) => {
if (!inputRef.current || disabled || readOnly) return;

const currentValue = Number(inputRef.current.value);
let newValue =
direction === 'up' ? currentValue + step : currentValue - step;
if (min !== undefined) {
newValue = Math.max(newValue, min);
}
},
[allowEmpty, step, min, max, onChange, onClick]
if (max !== undefined) {
newValue = Math.min(newValue, max);
}
const currentInputValue = inputRef.current
? inputRef.current.value
: '';
const state = {
value:
allowEmpty && currentInputValue === '' && step === 0
? ''
: newValue,
direction: direction,
};

setValue(state.value);

onChange?.(event, state);
onClick?.(event, state);
}, 50),
[step, min, max, allowEmpty, onChange, onClick]
);

function handleStepperClick(event, direction) {
debouncedHandleStepperClick(event, direction);
}

// Slug is always size `mini`
let normalizedSlug;
if (slug && slug['type']?.displayName === 'AILabel') {
Expand Down

0 comments on commit d47ba9d

Please sign in to comment.