Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert debounce functionality on number fields #969

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 }
onFocus={ onFocus }

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