Skip to content

Commit

Permalink
chore: revert debounce functionality on number fields
Browse files Browse the repository at this point in the history
Related to #958
  • Loading branch information
Skaiir committed Jan 5, 2024
1 parent c567397 commit 58299e1
Showing 1 changed file with 9 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import Big from 'big.js';
import classNames from 'classnames';

import { useCallback, useMemo, useRef, useState } from 'preact/hooks';
import useFlushDebounce from '../../hooks/useFlushDebounce';

import Description from '../Description';
import Errors from '../Errors';
Expand Down Expand Up @@ -34,7 +32,8 @@ export default function Numberfield(props) {
onFocus,
field,
value,
readonly
readonly,
onChange
} = props;

const {
Expand All @@ -58,19 +57,6 @@ export default function Numberfield(props) {

const [ stringValueCache, setStringValueCache ] = useState('');

const [ onChangeDebounced, flushOnChange ] = useFlushDebounce((params) => {
props.onChange(params);
}, [ props.onChange ]);

const onInputBlur = () => {
flushOnChange && flushOnChange();
onBlur && onBlur();
};

const onInputFocus = () => {
onFocus && onFocus();
};

// checks whether the value currently in the form data is practically different from the one in the input field cache
// this allows us to guarantee the field always displays valid form data, but without auto-simplifying values like 1.000 to 1
const cacheValueMatchesState = useMemo(() => Numberfield.config.sanitizeValue({ value, formField: field }) === Numberfield.config.sanitizeValue({ value: stringValueCache, formField: field }), [ stringValueCache, value, field ]);
Expand All @@ -96,7 +82,7 @@ export default function Numberfield(props) {

if (isNullEquivalentValue(stringValue)) {
setStringValueCache('');
onChangeDebounced({ field, value: null });
onChange({ field, value: null });
return;
}

Expand All @@ -110,14 +96,14 @@ export default function Numberfield(props) {

if (isNaN(Number(stringValue))) {
setStringValueCache('NaN');
onChangeDebounced({ field, value: 'NaN' });
onChange({ field, value: 'NaN' });
return;
}

setStringValueCache(stringValue);
onChangeDebounced({ field, value: serializeToString ? stringValue : Number(stringValue) });
onChange({ field, value: serializeToString ? stringValue : Number(stringValue) });

}, [ field, onChangeDebounced, serializeToString ]);
}, [ field, onChange, serializeToString ]);

const increment = () => {
if (readonly) {
Expand Down Expand Up @@ -201,8 +187,8 @@ export default function Numberfield(props) {
id={ domId }
onKeyDown={ onKeyDown }
onKeyPress={ onKeyPress }
onBlur={ onInputBlur }
onFocus={ onInputFocus }
onBlur={ () => onBlur && onBlur() }
onFocus={ () => onFocus && onFocus() }

// @ts-ignore
onInput={ (e) => setValue(e.target.value) }
Expand Down Expand Up @@ -251,4 +237,4 @@ Numberfield.config = {
create: (options = {}) => ({
...options
})
};
};

0 comments on commit 58299e1

Please sign in to comment.