diff --git a/packages/material-renderers/src/controls/MaterialAnyOfStringOrEnumControl.tsx b/packages/material-renderers/src/controls/MaterialAnyOfStringOrEnumControl.tsx index 7cb128f8b..87939a32c 100644 --- a/packages/material-renderers/src/controls/MaterialAnyOfStringOrEnumControl.tsx +++ b/packages/material-renderers/src/controls/MaterialAnyOfStringOrEnumControl.tsx @@ -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[]) => @@ -51,6 +56,7 @@ const findTextSchema = (schemas: JsonSchema[]) => const MuiAutocompleteInputText = ( props: EnumCellProps & WithClassname & WithInputProps ) => { + const [focused, onFocus, onBlur] = useFocus(); const { data, config, @@ -87,7 +93,11 @@ const MuiAutocompleteInputText = ( handleChange, '', data, - path + path, + undefined, + undefined, + true, + focused ); const dataList = ( @@ -102,6 +112,8 @@ const MuiAutocompleteInputText = ( type='text' value={inputText} onChange={onChange} + onFocus={onFocus} + onBlur={onBlur} className={className} id={id} label={label} diff --git a/packages/material-renderers/src/controls/MaterialNativeControl.tsx b/packages/material-renderers/src/controls/MaterialNativeControl.tsx index 358d45f71..6c650f1f5 100644 --- a/packages/material-renderers/src/controls/MaterialNativeControl.tsx +++ b/packages/material-renderers/src/controls/MaterialNativeControl.tsx @@ -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 @@ -60,7 +60,11 @@ export const MaterialNativeControl = (props: ControlProps) => { handleChange, '', data, - path + path, + undefined, + undefined, + true, + focused ); const fieldType = appliedUiSchemaOptions.format ?? schema.format; const showDescription = !isDescriptionHidden( diff --git a/packages/material-renderers/src/mui-controls/MuiInputInteger.tsx b/packages/material-renderers/src/mui-controls/MuiInputInteger.tsx index 4d2f8f376..8d69b1e63 100644 --- a/packages/material-renderers/src/mui-controls/MuiInputInteger.tsx +++ b/packages/material-renderers/src/mui-controls/MuiInputInteger.tsx @@ -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); @@ -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, @@ -56,7 +62,10 @@ export const MuiInputInteger = React.memo(function MuiInputInteger( '', data, path, - eventToValue + eventToValue, + undefined, + true, + focused ); return ( @@ -64,6 +73,8 @@ export const MuiInputInteger = React.memo(function MuiInputInteger( label={label} type='number' value={inputValue} + onFocus={onFocus} + onBlur={onBlur} onChange={onChange} className={className} id={id} diff --git a/packages/material-renderers/src/mui-controls/MuiInputNumber.tsx b/packages/material-renderers/src/mui-controls/MuiInputNumber.tsx index 1d626f6b1..f31e90f96 100644 --- a/packages/material-renderers/src/mui-controls/MuiInputNumber.tsx +++ b/packages/material-renderers/src/mui-controls/MuiInputNumber.tsx @@ -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 : parseFloat(value); @@ -33,6 +38,7 @@ 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, @@ -54,7 +60,10 @@ export const MuiInputNumber = React.memo(function MuiInputNumber( '', data, path, - eventToValue + eventToValue, + undefined, + true, + focused ); return ( @@ -63,6 +72,8 @@ export const MuiInputNumber = React.memo(function MuiInputNumber( label={label} value={inputValue} onChange={onChange} + onFocus={onFocus} + onBlur={onBlur} className={className} id={id} disabled={!enabled} diff --git a/packages/material-renderers/src/mui-controls/MuiInputNumberFormat.tsx b/packages/material-renderers/src/mui-controls/MuiInputNumberFormat.tsx index a7e8bf686..e7ee02099 100644 --- a/packages/material-renderers/src/mui-controls/MuiInputNumberFormat.tsx +++ b/packages/material-renderers/src/mui-controls/MuiInputNumberFormat.tsx @@ -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 & WithInputProps ) { + const [focused, onFocus, onBlur] = useFocus(); const { className, id, @@ -62,7 +68,10 @@ export const MuiInputNumberFormat = React.memo(function MuiInputNumberFormat( '', formattedNumber, path, - validStringNumber + validStringNumber, + undefined, + true, + focused ); return ( @@ -70,6 +79,8 @@ export const MuiInputNumberFormat = React.memo(function MuiInputNumberFormat( type='text' value={inputValue} onChange={onChange} + onFocus={onFocus} + onBlur={onBlur} className={className} id={id} label={label} diff --git a/packages/material-renderers/src/mui-controls/MuiInputText.tsx b/packages/material-renderers/src/mui-controls/MuiInputText.tsx index de4845af4..7a8e96ce2 100644 --- a/packages/material-renderers/src/mui-controls/MuiInputText.tsx +++ b/packages/material-renderers/src/mui-controls/MuiInputText.tsx @@ -38,6 +38,7 @@ import { WithInputProps, useDebouncedChange, useInputComponent, + useFocus, } from '../util'; interface MuiTextInputProps { @@ -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, @@ -88,7 +90,10 @@ export const MuiInputText = React.memo(function MuiInputText( '', data, path, - eventToValue + eventToValue, + undefined, + true, + focused ); const onPointerEnter = () => setShowAdornment(true); const onPointerLeave = () => setShowAdornment(false); @@ -109,6 +114,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} diff --git a/packages/material-renderers/src/util/debounce.ts b/packages/material-renderers/src/util/debounce.ts index 2971df827..2df8a13cc 100644 --- a/packages/material-renderers/src/util/debounce.ts +++ b/packages/material-renderers/src/util/debounce.ts @@ -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 @@ -32,7 +32,9 @@ export const useDebouncedChange = ( data: any, path: string, eventToValueFunction: (ev: any) => any = eventToValue, - timeout = 300 + timeout = 300, + flushOnBlur = false, + focused = false ): [any, React.ChangeEventHandler, () => void] => { const [input, setInput] = useState(data ?? defaultValue); useEffect(() => { @@ -42,6 +44,11 @@ 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);