web: Sync TextInputRow value with initialValue changes

dev
Alinson S. Xavier 2 weeks ago
parent af7cb92282
commit 055faefa28

@ -6,7 +6,7 @@
import formStyles from "./Form.module.css"; import formStyles from "./Form.module.css";
import HelpButton from "../Buttons/HelpButton"; import HelpButton from "../Buttons/HelpButton";
import React, { useRef, useState } from "react"; import React, { useEffect, useRef, useState } from "react";
import { ValidationError } from "../../../core/Data/validate"; import { ValidationError } from "../../../core/Data/validate";
interface TextInputRowProps { interface TextInputRowProps {
@ -21,6 +21,13 @@ function TextInputRow(props: TextInputRowProps) {
const [savedValue, setSavedValue] = useState(props.initialValue); const [savedValue, setSavedValue] = useState(props.initialValue);
const inputRef = useRef<HTMLInputElement>(null); const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (inputRef.current) {
inputRef.current.value = props.initialValue;
}
setSavedValue(props.initialValue);
}, [props.initialValue]);
const onBlur = (event: React.FocusEvent<HTMLInputElement>) => { const onBlur = (event: React.FocusEvent<HTMLInputElement>) => {
const newValue = event.target.value; const newValue = event.target.value;
if (newValue === savedValue) return; if (newValue === savedValue) return;
@ -29,8 +36,8 @@ function TextInputRow(props: TextInputRowProps) {
inputRef.current!.value = savedValue; inputRef.current!.value = savedValue;
return; return;
} }
setSavedValue(newValue);
}; };
return ( return (
<div className={formStyles.FormRow}> <div className={formStyles.FormRow}>
<label> <label>

Loading…
Cancel
Save