refactor: improve delay component

This commit is contained in:
Carlos Valente
2025-10-05 09:42:33 +02:00
committed by Alex Christoffer Rasmussen
parent c2012bd9e3
commit d986f438d9
@@ -20,7 +20,7 @@ export default function DelayInput(props: DelayInputProps) {
const [value, setValue] = useState<string>(''); const [value, setValue] = useState<string>('');
const inputRef = useRef<HTMLInputElement | null>(null); const inputRef = useRef<HTMLInputElement | null>(null);
// avoid wrong submit on cancel // avoid wrong submit on cancel
let ignoreChange = false; const ignoreChangeRef = useRef(false);
// set internal value on duration change // set internal value on duration change
useEffect(() => { useEffect(() => {
@@ -35,8 +35,8 @@ export default function DelayInput(props: DelayInputProps) {
* @param {string} newValue string to be parsed * @param {string} newValue string to be parsed
*/ */
const validateAndSubmit = (newValue: string) => { const validateAndSubmit = (newValue: string) => {
if (ignoreChange) { if (ignoreChangeRef.current) {
ignoreChange = false; ignoreChangeRef.current = false;
return; return;
} }
@@ -78,7 +78,7 @@ export default function DelayInput(props: DelayInputProps) {
} else if (event.key === 'Tab') { } else if (event.key === 'Tab') {
validateAndSubmit((event.target as HTMLInputElement).value); validateAndSubmit((event.target as HTMLInputElement).value);
} else if (event.key === 'Escape') { } else if (event.key === 'Escape') {
ignoreChange = true; ignoreChangeRef.current = true;
setValue(millisToString(duration)); setValue(millisToString(duration));
inputRef.current?.blur(); inputRef.current?.blur();
} }