Skip to content

Commit

Permalink
Merge pull request #66 from FRC2713/fix-autoIncrement2
Browse files Browse the repository at this point in the history
Fix autoIncrementOnReset
  • Loading branch information
tytremblay authored Jan 19, 2025
2 parents 6ef0912 + 64d4b5d commit 560f03d
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/components/inputs/CheckboxInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ 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;
}
setChecked(data.defaultValue);
}, [data.defaultValue]);

useEvent('resetFields', resetState);
useEvent('forceResetFields', () => resetState(true) );


useEffect(() => {
updateValue(props.code, checked);
Expand Down
9 changes: 6 additions & 3 deletions src/components/inputs/CounterInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
12 changes: 6 additions & 6 deletions src/components/inputs/NumberInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ export default function NumberInput(props: ConfigurableInputProps) {

const [value, setValue] = React.useState<number | ''>(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);
Expand Down
7 changes: 5 additions & 2 deletions src/components/inputs/RangeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
3 changes: 1 addition & 2 deletions src/components/inputs/SelectInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ 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;
}
setValue(data.defaultValue);
}, [data.defaultValue]);

useEvent('resetFields', resetState);
useEvent('forceResetFields', () => resetState(true) );

const handleSelect = useCallback((value: string) => {
setValue(value);
Expand Down
3 changes: 1 addition & 2 deletions src/components/inputs/StringInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ 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;
}
setValue(data.defaultValue);
}, [data.defaultValue]);

useEvent('resetFields', resetState);
useEvent('forceResetFields', () => resetState(true) );

useEffect(() => {
updateValue(props.code, value);
Expand Down
5 changes: 2 additions & 3 deletions src/components/inputs/TimerInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -43,7 +43,6 @@ export default function TimerInput(props: ConfigurableInputProps) {
}, []);

useEvent('resetFields', resetState);
useEvent('forceResetFields', () => resetState(true) );

function startStop() {
toggleTimer(!isRunning);
Expand Down Expand Up @@ -85,7 +84,7 @@ export default function TimerInput(props: ConfigurableInputProps) {
<Button variant="outline" disabled={time === 0} onClick={() => lap()}>
<TimerReset className="size-4" />
</Button>
<Button variant="outline" onClick={() => resetState()}>
<Button variant="outline" onClick={() => resetState({force: false})}>
<Undo className="size-4" />
</Button>
</div>
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/useEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ interface AppEvent<PayloadType = unknown> extends Event {

export interface CustomWindowEventMap extends WindowEventMap {
/* Custom Event */
resetFields: AppEvent<string>;
forceResetFields: AppEvent<string>;
resetFields: AppEvent<{force: boolean}>;
}

export const useEvent = <PayloadType = unknown>(
Expand Down

0 comments on commit 560f03d

Please sign in to comment.