fix: "instant update" feature

- Components do update to values received from backend even if instant
  update is not selected.
This commit is contained in:
Mose Müller 2023-08-02 12:06:22 +02:00
parent 4ff07aa587
commit 7a89168d14
2 changed files with 16 additions and 12 deletions

View File

@ -109,16 +109,6 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => {
useEffect(() => { useEffect(() => {
renderCount.current++; renderCount.current++;
// Parse the input string to a number for comparison
const numericInputString =
props.type === 'int' ? parseInt(inputString) : parseFloat(inputString);
// Update inputString only when value is different from numericInputString
// preventing the removal of trailing decimals or zeros after the decimal.
if (isInstantUpdate && props.value !== numericInputString) {
setInputString(props.value.toString());
}
// Set the cursor position after the component re-renders // Set the cursor position after the component re-renders
const inputElement = document.getElementsByName( const inputElement = document.getElementsByName(
parent_path.concat(name) parent_path.concat(name)
@ -128,6 +118,16 @@ export const NumberComponent = React.memo((props: NumberComponentProps) => {
} }
}); });
useEffect(() => {
// Parse the input string to a number for comparison
const numericInputString =
props.type === 'int' ? parseInt(inputString) : parseFloat(inputString);
// Only update the inputString if it's different from the prop value
if (props.value !== numericInputString) {
setInputString(props.value.toString());
}
}, [props.value]);
const handleNumericKey = (key: string, value: string, selectionStart: number) => { const handleNumericKey = (key: string, value: string, selectionStart: number) => {
// Check if a number key or a decimal point key is pressed // Check if a number key or a decimal point key is pressed
if (key === '.' && (value.includes('.') || props.type === 'int')) { if (key === '.' && (value.includes('.') || props.type === 'int')) {

View File

@ -21,10 +21,14 @@ export const StringComponent = React.memo((props: StringComponentProps) => {
const { name, parent_path, readOnly, docString, isInstantUpdate } = props; const { name, parent_path, readOnly, docString, isInstantUpdate } = props;
useEffect(() => { useEffect(() => {
renderCount.current++; renderCount.current++;
if (isInstantUpdate && props.value !== inputString) { }, [isInstantUpdate, inputString, renderCount]);
useEffect(() => {
// Only update the inputString if it's different from the prop value
if (props.value !== inputString) {
setInputString(props.value); setInputString(props.value);
} }
}, [isInstantUpdate, props.value, inputString, renderCount]); }, [props.value]);
const handleChange = (event) => { const handleChange = (event) => {
setInputString(event.target.value); setInputString(event.target.value);