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

Add debounce for input material controls #1813

Merged
merged 1 commit into from
Oct 8, 2021
Merged
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
66 changes: 66 additions & 0 deletions packages/examples/src/1645.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
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
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { registerExamples } from './register';
import { UISchemaElement } from '@jsonforms/core';

export const schema = {
type: 'object',
properties: {
propText0: {type: 'string'},
propText1: {type: 'string'},
propText2: {type: 'string'},
propText3: {type: 'string'},
propText4: {type: 'string'},
propText5: {type: 'string'},
propText6: {type: 'string'},
propText7: {type: 'string'},
propText8: {type: 'string'},
propText9: {type: 'string'},
propNumber0: {type: 'number'},
propNumber1: {type: 'number'},
propNumber2: {type: 'number'},
propNumber3: {type: 'number'},
propNumber4: {type: 'number'},
propNumber5: {type: 'number'},
propNumber6: {type: 'number'},
propNumber7: {type: 'number'},
propNumber8: {type: 'number'},
propNumber9: {type: 'number'},
}
};

export const uischema: UISchemaElement = undefined;

export const data = {};

registerExamples([
{
name: '1645',
label: 'Issue 1645',
data,
schema,
uischema
}
]);
4 changes: 3 additions & 1 deletion packages/examples/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import * as multiEnum from './multi-enum';
import * as enumInArray from './enumInArray';
import * as readonly from './readonly';
import * as bug_1779 from './1779';
import * as bug_1645 from './1645';
export * from './register';
export * from './example';

Expand Down Expand Up @@ -134,5 +135,6 @@ export {
multiEnum,
enumInArray,
readonly,
bug_1779
bug_1779,
bug_1645
};
28 changes: 16 additions & 12 deletions packages/material/src/controls/MaterialAnyOfStringOrEnumControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ import { Control, withJsonFormsControlProps } from '@jsonforms/react';
import { Input } from '@material-ui/core';
import { InputBaseComponentProps } from '@material-ui/core/InputBase';
import merge from 'lodash/merge';
import React from 'react';
import React, { useMemo } from 'react';
import { useDebouncedChange } from '../util';
import { MaterialInputControl } from './MaterialInputControl';

const findEnumSchema = (schemas: JsonSchema[]) =>
Expand All @@ -64,17 +65,20 @@ const MuiAutocompleteInputText = (props: EnumCellProps & WithClassname) => {
const enumSchema = findEnumSchema(schema.anyOf);
const stringSchema = findTextSchema(schema.anyOf);
const maxLength = stringSchema.maxLength;
const appliedUiSchemaOptions = merge({}, config, uischema.options);
let inputProps: InputBaseComponentProps = {};
if (appliedUiSchemaOptions.restrict) {
inputProps = { maxLength: maxLength };
}
if (appliedUiSchemaOptions.trim && maxLength !== undefined) {
inputProps.size = maxLength;
}
const onChange = (ev: any) => handleChange(path, ev.target.value);
const appliedUiSchemaOptions = useMemo(() => merge({}, config, uischema.options),[config, uischema.options]);
const inputProps: InputBaseComponentProps = useMemo(() => {
let propMemo: InputBaseComponentProps = {};
if (appliedUiSchemaOptions.restrict) {
propMemo = { maxLength: maxLength };
}
if (appliedUiSchemaOptions.trim && maxLength !== undefined) {
propMemo.size = maxLength;
}
propMemo.list = props.id + 'datalist';
return propMemo;
},[appliedUiSchemaOptions,props.id]);
sdirix marked this conversation as resolved.
Show resolved Hide resolved
const [inputText, onChange] = useDebouncedChange(handleChange, '', data, path);

inputProps.list = props.id + 'datalist';
const dataList = (
<datalist id={props.id + 'datalist'}>
{enumSchema.enum.map(optionValue => (
Expand All @@ -85,7 +89,7 @@ const MuiAutocompleteInputText = (props: EnumCellProps & WithClassname) => {
return (
<Input
type='text'
value={data || ''}
value={inputText}
onChange={onChange}
className={className}
id={id}
Expand Down
14 changes: 10 additions & 4 deletions packages/material/src/mui-controls/MuiInputInteger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import { CellProps, WithClassname } from '@jsonforms/core';
import Input from '@material-ui/core/Input';
import { areEqual } from '@jsonforms/react';
import merge from 'lodash/merge';
import { useDebouncedChange } from '../util';

const toNumber = (value: string) =>
value === '' ? undefined : parseInt(value, 10);
const eventToValue = (ev:any) => toNumber(ev.target.value);

export const MuiInputInteger = React.memo(
(props: CellProps & WithClassname) => {
Expand All @@ -41,15 +46,16 @@ export const MuiInputInteger = React.memo(
config
} = props;
const inputProps = { step: '1' };
const toNumber = (value: string) =>
value === '' ? undefined : parseInt(value, 10);

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

const [inputValue, onChange] = useDebouncedChange(handleChange, '', data, path, eventToValue);

return (
<Input
type='number'
value={data !== undefined && data !== null ? data : ''}
onChange={ev => handleChange(path, toNumber(ev.target.value))}
value={inputValue}
onChange={onChange}
className={className}
id={id}
disabled={!enabled}
Expand Down
12 changes: 8 additions & 4 deletions packages/material/src/mui-controls/MuiInputNumber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import { CellProps, WithClassname } from '@jsonforms/core';
import Input from '@material-ui/core/Input';
import { areEqual } from '@jsonforms/react';
import merge from 'lodash/merge';
import {useDebouncedChange} from '../util';

const toNumber = (value: string) =>
value === '' ? undefined : parseFloat(value);
const eventToValue = (ev:any) => toNumber(ev.target.value);
export const MuiInputNumber = React.memo((props: CellProps & WithClassname) => {
const {
data,
Expand All @@ -40,15 +44,15 @@ export const MuiInputNumber = React.memo((props: CellProps & WithClassname) => {
config
} = props;
const inputProps = { step: '0.1' };
const toNumber = (value: string) =>
value === '' ? undefined : parseFloat(value);

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

return (
<Input
type='number'
value={data === undefined || data === null ? '' : data}
onChange={ev => handleChange(path, toNumber(ev.target.value))}
value={inputValue}
onChange={onChange}
className={className}
id={id}
disabled={!enabled}
Expand Down
12 changes: 6 additions & 6 deletions packages/material/src/mui-controls/MuiInputNumberFormat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import React from 'react';
import React, {useCallback} from 'react';
import { CellProps, Formatted, WithClassname } from '@jsonforms/core';
import Input from '@material-ui/core/Input';
import { areEqual } from '@jsonforms/react';
import merge from 'lodash/merge';
import { useDebouncedChange } from '../util';

export const MuiInputNumberFormat = React.memo(
(props: CellProps & WithClassname & Formatted<number>) => {
Expand All @@ -51,15 +52,14 @@ export const MuiInputNumberFormat = React.memo(
}
const formattedNumber = props.toFormatted(props.data);

const onChange = (ev: any) => {
const validStringNumber = props.fromFormatted(ev.currentTarget.value);
handleChange(path, validStringNumber);
};
const validStringNumber = useCallback((ev:any) => props.fromFormatted(ev.currentTarget.value),[props.fromFormatted]);
const [inputValue, onChange] = useDebouncedChange(handleChange, '', formattedNumber, path, validStringNumber);


return (
<Input
type='text'
value={formattedNumber}
value={inputValue}
onChange={onChange}
className={className}
id={id}
Expand Down
29 changes: 16 additions & 13 deletions packages/material/src/mui-controls/MuiInputText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,14 @@ import IconButton from '@material-ui/core/IconButton';
import InputAdornment from '@material-ui/core/InputAdornment';
import Close from '@material-ui/icons/Close';
import { useTheme } from '@material-ui/core/styles';
import { JsonFormsTheme } from '../util';
import { JsonFormsTheme, useDebouncedChange } from '../util';
import { InputBaseComponentProps } from '@material-ui/core';

interface MuiTextInputProps {
muiInputProps?: InputProps['inputProps'];
inputComponent?: InputProps['inputComponent'];
}

export const MuiInputText = React.memo((props: CellProps & WithClassname & MuiTextInputProps) => {
export const MuiInputText = React.memo((props: CellProps & WithClassname & MuiTextInputProps) => {
const [showAdornment, setShowAdornment] = useState(false);
const {
data,
Expand All @@ -63,23 +62,27 @@ export const MuiInputText = React.memo((props: CellProps & WithClassname & MuiTe
} else {
inputProps = {};
}

inputProps = merge(inputProps, muiInputProps);

if (appliedUiSchemaOptions.trim && maxLength !== undefined) {
inputProps.size = maxLength;
}
const onChange = (ev: any) => handleChange(path, ev.target.value);
};

const [inputText, onChange, onClear] = useDebouncedChange(handleChange, '', data, path);
const onPointerEnter = () => setShowAdornment(true);
const onPointerLeave = () => setShowAdornment(false);

const theme: JsonFormsTheme = useTheme();
const inputDeleteBackgroundColor = theme.jsonforms?.input?.delete?.background || theme.palette.background.default;

const closeStyle = {background: theme.jsonforms?.input?.delete?.background || theme.palette.background.default, borderRadius: '50%'};

return (
<Input
type={
appliedUiSchemaOptions.format === 'password' ? 'password' : 'text'
}
value={data || ''}
value={inputText}
onChange={onChange}
className={className}
id={id}
Expand All @@ -89,8 +92,8 @@ export const MuiInputText = React.memo((props: CellProps & WithClassname & MuiTe
fullWidth={!appliedUiSchemaOptions.trim || maxLength === undefined}
inputProps={inputProps}
error={!isValid}
onPointerEnter={() => setShowAdornment(true) }
onPointerLeave={() => setShowAdornment(false) }
onPointerEnter={onPointerEnter}
onPointerLeave={onPointerLeave}
endAdornment={
<InputAdornment
position='end'
Expand All @@ -103,9 +106,9 @@ export const MuiInputText = React.memo((props: CellProps & WithClassname & MuiTe
>
<IconButton
aria-label='Clear input field'
onClick={() => handleChange(path, undefined)}
onClick={onClear}
>
<Close style={{background: inputDeleteBackgroundColor, borderRadius: '50%'}}/>
<Close style={closeStyle}/>
</IconButton>
</InputAdornment>
}
Expand Down
7 changes: 5 additions & 2 deletions packages/material/src/mui-controls/MuiInputTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { CellProps, WithClassname } from '@jsonforms/core';
import Input from '@material-ui/core/Input';
import { areEqual } from '@jsonforms/react';
import merge from 'lodash/merge';
import { useDebouncedChange } from '../util';

export const MuiInputTime = React.memo((props: CellProps & WithClassname) => {
const {
Expand All @@ -40,11 +41,13 @@ export const MuiInputTime = React.memo((props: CellProps & WithClassname) => {
config
} = props;
const appliedUiSchemaOptions = merge({}, config, uischema.options);
const [inputValue, onChange] = useDebouncedChange(handleChange, '', data, path);

return (
<Input
type='time'
value={data || ''}
onChange={ev => handleChange(path, ev.target.value)}
value={inputValue}
onChange={onChange}
className={className}
id={id}
disabled={!enabled}
Expand Down
19 changes: 19 additions & 0 deletions packages/material/src/util/debounce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { debounce } from 'lodash';
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 [input, setInput] = useState(data ?? defaultValue);
useEffect(() => {
setInput(data ?? defaultValue);
}, [data]);
const debouncedUpdate = useCallback(debounce((newValue: string) => handleChange(path, newValue), timeout), [handleChange, path, timeout]);
const onChange = useCallback((ev: any) => {
const newValue = eventToValueFunction(ev);
setInput(newValue ?? defaultValue);
debouncedUpdate(newValue);
}, [debouncedUpdate, eventToValueFunction]);
const onClear = useCallback(() => { setInput(defaultValue); handleChange(path, undefined) }, [defaultValue, handleChange, path]);
return [input, onChange, onClear];
};
1 change: 1 addition & 0 deletions packages/material/src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@
export * from './datejs';
export * from './layout';
export * from './theme';
export * from './debounce';
Loading