Skip to content

refactor(material-renderers): flushable debounce #2440

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -38,7 +38,12 @@ import { Control, withJsonFormsControlProps } from '@jsonforms/react';
import { InputBaseComponentProps } from '@mui/material';
import merge from 'lodash/merge';
import React, { useMemo } from 'react';
import { useDebouncedChange, useInputComponent, WithInputProps } from '../util';
import {
useDebouncedChange,
useInputComponent,
WithInputProps,
useFocus,
} from '../util';
import { MaterialInputControl } from './MaterialInputControl';

const findEnumSchema = (schemas: JsonSchema[]) =>
Expand All @@ -51,6 +56,7 @@ const findTextSchema = (schemas: JsonSchema[]) =>
const MuiAutocompleteInputText = (
props: EnumCellProps & WithClassname & WithInputProps
) => {
const [focused, onFocus, onBlur] = useFocus();
const {
data,
config,
Expand Down Expand Up @@ -83,12 +89,13 @@ const MuiAutocompleteInputText = (
propMemo.list = props.id + 'datalist';
return propMemo;
}, [appliedUiSchemaOptions, props.id]);
const [inputText, onChange] = useDebouncedChange(
const [inputText, onChange] = useDebouncedChange({
handleChange,
'',
data,
path
);
path,
focused,
flushOnBlur: true,
});

const dataList = (
<datalist id={props.id + 'datalist'}>
Expand All @@ -102,6 +109,8 @@ const MuiAutocompleteInputText = (
type='text'
value={inputText}
onChange={onChange}
onFocus={onFocus}
onBlur={onBlur}
className={className}
id={id}
label={label}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
The MIT License

Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -56,12 +56,11 @@ export const MaterialNativeControl = (props: ControlProps) => {
} = props;
const isValid = errors.length === 0;
const appliedUiSchemaOptions = merge({}, config, props.uischema.options);
const [inputValue, onChange] = useDebouncedChange(
const [inputValue, onChange] = useDebouncedChange({
handleChange,
'',
data,
path
);
path,
});
const fieldType = appliedUiSchemaOptions.format ?? schema.format;
const showDescription = !isDescriptionHidden(
visible,
Expand Down
19 changes: 14 additions & 5 deletions packages/material-renderers/src/mui-controls/MuiInputInteger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@
import React from 'react';
import { CellProps, WithClassname } from '@jsonforms/core';
import merge from 'lodash/merge';
import { useDebouncedChange, useInputComponent, WithInputProps } from '../util';
import {
useDebouncedChange,
useInputComponent,
WithInputProps,
useFocus,
} from '../util';

const toNumber = (value: string) =>
value === '' ? undefined : parseInt(value, 10);
Expand All @@ -34,6 +39,7 @@ const eventToValue = (ev: any) => toNumber(ev.target.value);
export const MuiInputInteger = React.memo(function MuiInputInteger(
props: CellProps & WithClassname & WithInputProps
) {
const [focused, onFocus, onBlur] = useFocus();
const {
data,
className,
Expand All @@ -51,19 +57,22 @@ export const MuiInputInteger = React.memo(function MuiInputInteger(

const appliedUiSchemaOptions = merge({}, config, uischema.options);

const [inputValue, onChange] = useDebouncedChange(
const [inputValue, onChange] = useDebouncedChange({
handleChange,
'',
data,
path,
eventToValue
);
eventToValue,
focused,
flushOnBlur: true,
});

return (
<InputComponent
label={label}
type='number'
value={inputValue}
onFocus={onFocus}
onBlur={onBlur}
onChange={onChange}
className={className}
id={id}
Expand Down
19 changes: 14 additions & 5 deletions packages/material-renderers/src/mui-controls/MuiInputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@
import React from 'react';
import { CellProps, WithClassname } from '@jsonforms/core';
import merge from 'lodash/merge';
import { useDebouncedChange, useInputComponent, WithInputProps } from '../util';
import {
useDebouncedChange,
useInputComponent,
WithInputProps,
useFocus,
} from '../util';

const toNumber = (value: string) =>
value === '' ? undefined : parseFloat(value);
const eventToValue = (ev: any) => toNumber(ev.target.value);
export const MuiInputNumber = React.memo(function MuiInputNumber(
props: CellProps & WithClassname & WithInputProps
) {
const [focused, onFocus, onBlur] = useFocus();
const {
data,
className,
Expand All @@ -49,20 +55,23 @@ export const MuiInputNumber = React.memo(function MuiInputNumber(
const inputProps = { step: '0.1' };

const appliedUiSchemaOptions = merge({}, config, uischema.options);
const [inputValue, onChange] = useDebouncedChange(
const [inputValue, onChange] = useDebouncedChange({
handleChange,
'',
data,
path,
eventToValue
);
eventToValue,
focused,
flushOnBlur: true,
});

return (
<InputComponent
type='number'
label={label}
value={inputValue}
onChange={onChange}
onFocus={onFocus}
onBlur={onBlur}
className={className}
id={id}
disabled={!enabled}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@
import React, { useCallback } from 'react';
import { CellProps, Formatted, WithClassname } from '@jsonforms/core';
import merge from 'lodash/merge';
import { useDebouncedChange, useInputComponent, WithInputProps } from '../util';
import {
useDebouncedChange,
useInputComponent,
WithInputProps,
useFocus,
} from '../util';

export const MuiInputNumberFormat = React.memo(function MuiInputNumberFormat(
props: CellProps & WithClassname & Formatted<number> & WithInputProps
) {
const [focused, onFocus, onBlur] = useFocus();
const {
className,
id,
Expand Down Expand Up @@ -57,19 +63,22 @@ export const MuiInputNumberFormat = React.memo(function MuiInputNumberFormat(
(ev: any) => props.fromFormatted(ev.currentTarget.value),
[props.fromFormatted]
);
const [inputValue, onChange] = useDebouncedChange(
const [inputValue, onChange] = useDebouncedChange({
handleChange,
'',
formattedNumber,
data: formattedNumber,
path,
validStringNumber
);
eventToValue: validStringNumber,
focused,
flushOnBlur: true,
});

return (
<InputComponent
type='text'
value={inputValue}
onChange={onChange}
onFocus={onFocus}
onBlur={onBlur}
className={className}
id={id}
label={label}
Expand Down
13 changes: 9 additions & 4 deletions packages/material-renderers/src/mui-controls/MuiInputText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
WithInputProps,
useDebouncedChange,
useInputComponent,
useFocus,
} from '../util';

interface MuiTextInputProps {
Expand All @@ -51,6 +52,7 @@ const eventToValue = (ev: any) =>
export const MuiInputText = React.memo(function MuiInputText(
props: CellProps & WithClassname & MuiTextInputProps & WithInputProps
) {
const [focused, onFocus, onBlur] = useFocus();
const [showAdornment, setShowAdornment] = useState(false);
const {
data,
Expand Down Expand Up @@ -83,13 +85,14 @@ export const MuiInputText = React.memo(function MuiInputText(
inputProps.size = maxLength;
}

const [inputText, onChange, onClear] = useDebouncedChange(
const [inputText, onChange, onClear] = useDebouncedChange({
handleChange,
'',
data,
path,
eventToValue
);
eventToValue,
flushOnBlur: true,
focused,
});
const onPointerEnter = () => setShowAdornment(true);
const onPointerLeave = () => setShowAdornment(false);

Expand All @@ -109,6 +112,8 @@ export const MuiInputText = React.memo(function MuiInputText(
value={inputText}
onChange={onChange}
className={className}
onBlur={onBlur}
onFocus={onFocus}
id={id}
disabled={!enabled}
autoFocus={appliedUiSchemaOptions.focus}
Expand Down
7 changes: 3 additions & 4 deletions packages/material-renderers/src/mui-controls/MuiInputTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ export const MuiInputTime = React.memo(function MuiInputTime(
} = props;
const InputComponent = useInputComponent();
const appliedUiSchemaOptions = merge({}, config, uischema.options);
const [inputValue, onChange] = useDebouncedChange(
const [inputValue, onChange] = useDebouncedChange({
handleChange,
'',
data,
path
);
path,
});

return (
<InputComponent
Expand Down
49 changes: 34 additions & 15 deletions packages/material-renderers/src/util/debounce.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
The MIT License

Copyright (c) 2021 EclipseSource Munich
https://github.com/eclipsesource/jsonforms

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -25,15 +25,27 @@
import debounce from 'lodash/debounce';
import { useState, useCallback, useEffect } from 'react';

const eventToValue = (ev: any) => ev.target.value;
export const useDebouncedChange = (
handleChange: (path: string, value: any) => void,
defaultValue: any,
data: any,
path: string,
eventToValueFunction: (ev: any) => any = eventToValue,
timeout = 300
): [any, React.ChangeEventHandler, () => void] => {
const defaultEventToValue = (ev: any) => ev.target.value;
interface DebouncedChangeParams {
handleChange: (path: string, value: any) => void;
data: any;
path: string;
eventToValue?: (ev: any) => any;
defaultValue?: any;
flushOnBlur?: boolean;
focused?: boolean;
timeout?: number;
}
export const useDebouncedChange = ({
handleChange,
data,
path,
eventToValue = undefined,
defaultValue = '',
flushOnBlur = false,
focused = false,
timeout = 300,
}: DebouncedChangeParams): [any, React.ChangeEventHandler, () => void] => {
const [input, setInput] = useState(data ?? defaultValue);
useEffect(() => {
setInput(data ?? defaultValue);
Expand All @@ -42,13 +54,20 @@ export const useDebouncedChange = (
debounce((newValue: string) => handleChange(path, newValue), timeout),
[handleChange, path, timeout]
);
useEffect(() => {
if (!focused && flushOnBlur) {
debouncedUpdate.flush();
}
}, [focused, flushOnBlur, debouncedUpdate]);
const onChange = useCallback(
(ev: any) => {
const newValue = eventToValueFunction(ev);
const newValue = eventToValue
? eventToValue(ev)
: defaultEventToValue(ev);
setInput(newValue ?? defaultValue);
debouncedUpdate(newValue);
},
[debouncedUpdate, eventToValueFunction]
[debouncedUpdate, eventToValue]
);
const onClear = useCallback(() => {
setInput(defaultValue);
Expand Down