From 64d4b5d7e2d313fbde58f51a7083f64d81f71a7e Mon Sep 17 00:00:00 2001 From: Owen-Morgan825 Date: Sat, 18 Jan 2025 11:14:02 -0500 Subject: [PATCH] Fix autoIncrementOnReset --- src/components/inputs/CheckboxInput.tsx | 4 ++-- src/components/inputs/CounterInput.tsx | 9 ++++++--- src/components/inputs/NumberInput.tsx | 12 ++++++------ src/components/inputs/RangeInput.tsx | 7 +++++-- src/components/inputs/SelectInput.tsx | 3 +-- src/components/inputs/StringInput.tsx | 3 +-- src/components/inputs/TimerInput.tsx | 5 ++--- src/hooks/useEvent.ts | 3 +-- 8 files changed, 24 insertions(+), 22 deletions(-) diff --git a/src/components/inputs/CheckboxInput.tsx b/src/components/inputs/CheckboxInput.tsx index b7495ef..c7e80c7 100644 --- a/src/components/inputs/CheckboxInput.tsx +++ b/src/components/inputs/CheckboxInput.tsx @@ -16,7 +16,7 @@ export default function CheckboxInput(props: ConfigurableInputProps) { const [checked, setChecked] = React.useState(data.defaultValue); - const resetState = React.useCallback((force = false) => { + const resetState = React.useCallback(({force}: {force: boolean}) => { if (!force && (data.preserveDataOnReset || props.preserveSection)) { return; } @@ -24,7 +24,7 @@ export default function CheckboxInput(props: ConfigurableInputProps) { }, [data.defaultValue]); useEvent('resetFields', resetState); - useEvent('forceResetFields', () => resetState(true) ); + useEffect(() => { updateValue(props.code, checked); diff --git a/src/components/inputs/CounterInput.tsx b/src/components/inputs/CounterInput.tsx index f5698a0..3439e56 100644 --- a/src/components/inputs/CounterInput.tsx +++ b/src/components/inputs/CounterInput.tsx @@ -17,15 +17,18 @@ export default function CounterInput(props: ConfigurableInputProps) { const [value, setValue] = useState(data.defaultValue); - const resetState = useCallback((force = false) => { + const resetState = useCallback(({force}: {force: boolean}) => { if(!force && (data.preserveDataOnReset || props.preserveSection)) { + if (data.autoIncrementOnReset) { + const newVal = typeof value === 'number' ? value + data.step : 1; + setValue(newVal); + } return; }; setValue(data.defaultValue); - }, []); + }, [value]); useEvent('resetFields', resetState); - useEvent('forceResetFields', () => resetState(true) ); const handleChange = useCallback( (increment: number) => { diff --git a/src/components/inputs/NumberInput.tsx b/src/components/inputs/NumberInput.tsx index 038852f..c366208 100644 --- a/src/components/inputs/NumberInput.tsx +++ b/src/components/inputs/NumberInput.tsx @@ -16,19 +16,19 @@ export default function NumberInput(props: ConfigurableInputProps) { const [value, setValue] = React.useState(data.defaultValue); - const resetState = useCallback((force = false) => { + const resetState = useCallback(({force}: {force: boolean}) => { if (!force && (data.preserveDataOnReset || props.preserveSection)) { + if (data.autoIncrementOnReset) { + const newVal = typeof value === 'number' ? value + 1 : 1; + setValue(newVal); + } return; - } - if (!force && data.autoIncrementOnReset) { - setValue(value ?? 0 + 1); } else { setValue(data.defaultValue); } - }, [data.defaultValue]); + }, [data.defaultValue, value]); useEvent('resetFields', resetState); - useEvent('forceResetFields', () => resetState(true) ); useEffect(() => { updateValue(props.code, value); diff --git a/src/components/inputs/RangeInput.tsx b/src/components/inputs/RangeInput.tsx index 35b4276..156c064 100644 --- a/src/components/inputs/RangeInput.tsx +++ b/src/components/inputs/RangeInput.tsx @@ -16,15 +16,18 @@ export default function RangeInput(props: ConfigurableInputProps) { const [value, setValue] = useState(data.defaultValue); - const resetState = useCallback((force = false) => { + const resetState = useCallback(({force}: {force: boolean}) => { if (!force && (data.preserveDataOnReset || props.preserveSection)) { + if (data.autoIncrementOnReset) { + const newVal = typeof value === 'number' ? value + data.step : 1; + setValue(newVal); + } return; } setValue(data.defaultValue); }, [data.defaultValue]); useEvent('resetFields', resetState); - useEvent('forceResetFields', () => resetState(true) ); const handleChange = useCallback((value: number[]) => { setValue(value[0]); diff --git a/src/components/inputs/SelectInput.tsx b/src/components/inputs/SelectInput.tsx index 2c11871..833576f 100644 --- a/src/components/inputs/SelectInput.tsx +++ b/src/components/inputs/SelectInput.tsx @@ -26,7 +26,7 @@ export default function SelectInput(props: ConfigurableInputProps) { updateValue(props.code, value); }, [value]); - const resetState = useCallback((force = false) => { + const resetState = useCallback(({force}: {force: boolean}) => { if (!force && (data.preserveDataOnReset || props.preserveSection)) { return; } @@ -34,7 +34,6 @@ export default function SelectInput(props: ConfigurableInputProps) { }, [data.defaultValue]); useEvent('resetFields', resetState); - useEvent('forceResetFields', () => resetState(true) ); const handleSelect = useCallback((value: string) => { setValue(value); diff --git a/src/components/inputs/StringInput.tsx b/src/components/inputs/StringInput.tsx index 042b2f1..218580f 100644 --- a/src/components/inputs/StringInput.tsx +++ b/src/components/inputs/StringInput.tsx @@ -16,7 +16,7 @@ export default function StringInput(props: ConfigurableInputProps) { const [value, setValue] = React.useState(data.defaultValue); - const resetState = useCallback((force = false) => { + const resetState = useCallback(({force}: {force: boolean}) => { if (!force && (data.preserveDataOnReset || props.preserveSection)) { return; } @@ -24,7 +24,6 @@ export default function StringInput(props: ConfigurableInputProps) { }, [data.defaultValue]); useEvent('resetFields', resetState); - useEvent('forceResetFields', () => resetState(true) ); useEffect(() => { updateValue(props.code, value); diff --git a/src/components/inputs/TimerInput.tsx b/src/components/inputs/TimerInput.tsx index c135599..7e6dea7 100644 --- a/src/components/inputs/TimerInput.tsx +++ b/src/components/inputs/TimerInput.tsx @@ -31,7 +31,7 @@ export default function TimerInput(props: ConfigurableInputProps) { const average = useMemo(() => getAvg(times), [times]); - const resetState = useCallback((force = false) => { + const resetState = useCallback(({force}: {force: boolean}) => { if (!force && (data.preserveDataOnReset || props.preserveSection)) { return; } @@ -43,7 +43,6 @@ export default function TimerInput(props: ConfigurableInputProps) { }, []); useEvent('resetFields', resetState); - useEvent('forceResetFields', () => resetState(true) ); function startStop() { toggleTimer(!isRunning); @@ -85,7 +84,7 @@ export default function TimerInput(props: ConfigurableInputProps) { - diff --git a/src/hooks/useEvent.ts b/src/hooks/useEvent.ts index e5fb024..34a73de 100644 --- a/src/hooks/useEvent.ts +++ b/src/hooks/useEvent.ts @@ -6,8 +6,7 @@ interface AppEvent extends Event { export interface CustomWindowEventMap extends WindowEventMap { /* Custom Event */ - resetFields: AppEvent; - forceResetFields: AppEvent; + resetFields: AppEvent<{force: boolean}>; } export const useEvent = (