Skip to content

Commit

Permalink
refactor: useRadio onResetRadio initId 옵셔널로 변경 및 resetId로 네이밍 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
semnil5202 committed Mar 23, 2024
1 parent 5126190 commit efdbcef
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/hooks/useRadio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,26 @@ interface RadioItem {
checked: boolean;
}

interface OnResetRadioProps<T> {
radioKey: keyof T;
resetId?: number;
}

const useRadio = <T extends Record<keyof T, RadioItem[]>>(initialValue: T) => {
const [radioValue, setRadioValue] = useState<T>(initialValue);

const onResetRadio = useCallback((radioKey: keyof T, initId: number) => {
setRadioValue((prev) => ({
...prev,
[radioKey]: prev[radioKey].map((radio) => ({
...radio,
checked: radio.id === initId,
})),
}));
}, []);
const onResetRadio = useCallback(
({ radioKey, resetId }: OnResetRadioProps<T>) => {
setRadioValue((prev) => ({
...prev,
[radioKey]: prev[radioKey].map((radio) => ({
...radio,
checked: resetId ? radio.id === resetId : false,
})),
}));
},
[],
);

const onChangeRadio = useCallback(
(e: ChangeEvent<HTMLInputElement>, radioKey: keyof T) => {
Expand Down

0 comments on commit efdbcef

Please sign in to comment.