Skip to content

Commit

Permalink
refactor: selected return 값 보장을 위한 타입 강제 변경 및 변수 네이밍 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
semnil5202 committed Apr 15, 2024
1 parent 5b0bd8b commit a17eaaf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/hooks/useCheckbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChangeEvent, useCallback, useState } from 'react';

interface CheckboxItem {
checked: boolean;
id: number;
[key: string]: any;
}

Expand All @@ -22,7 +23,7 @@ const getSelectedCheckboxValueId = <T extends Record<keyof T, CheckboxItem[]>>(
.map((checkbox) => checkbox.id);
return acc;
},
new Object() as Record<keyof T, CheckboxItem[]>,
new Object() as Record<keyof T, number[]>,
);

return selectedCheckboxValue;
Expand All @@ -32,7 +33,7 @@ const useCheckbox = <T extends Record<keyof T, CheckboxItem[]>>(
initialValue: T,
) => {
const [checkboxValue, setCheckboxValue] = useState<T>(initialValue);
const selectedCheckboxId = getSelectedCheckboxValueId(checkboxValue);
const selectedCheckboxIds = getSelectedCheckboxValueId(checkboxValue);

const onResetCheckbox = useCallback((checkboxKey: keyof T) => {
setCheckboxValue((prev) => ({
Expand Down Expand Up @@ -74,7 +75,7 @@ const useCheckbox = <T extends Record<keyof T, CheckboxItem[]>>(

return {
checkboxValue,
selectedCheckboxId,
selectedCheckboxIds,
setCheckboxValue,
onChangeCheckbox,
onResetCheckbox,
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/useRadio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ChangeEvent, useCallback, useState } from 'react';

interface RadioItem {
checked: boolean;
name: string;
[key: string]: any;
}

Expand All @@ -17,7 +18,7 @@ const getSelectedRadioValueName = <T extends Record<keyof T, RadioItem[]>>(

const selectedRadioValue = radioValueKeys.reduce(
(acc, key) => {
acc[key] = radioValue[key].find((radio) => radio.checked)?.name;
acc[key] = radioValue[key].find((radio) => radio.checked)?.name || '';
return acc;
},
new Object() as Record<keyof T, string>,
Expand Down

0 comments on commit a17eaaf

Please sign in to comment.