From 2cf5c8d96010a4dbe62f3f5cc75d95e7558702b6 Mon Sep 17 00:00:00 2001 From: amaliadwiy Date: Mon, 8 Jun 2026 16:26:26 +0800 Subject: [PATCH] feat: tambah fitur pilih semua pada modal pilih divisi Menambahkan baris "Pilih Semua" / "Batalkan Semua" di atas list pada ModalSelectMultiple untuk kedua kategori choose-division dan share-division. --- components/modalSelectMultiple.tsx | 75 ++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 19 deletions(-) diff --git a/components/modalSelectMultiple.tsx b/components/modalSelectMultiple.tsx index ebebbd7..2d8b628 100644 --- a/components/modalSelectMultiple.tsx +++ b/components/modalSelectMultiple.tsx @@ -128,6 +128,35 @@ export default function ModalSelectMultiple({ open, close, title, category, choo }; + const groupsWithDivisions = data.filter((group: any) => group.Division?.length > 0) + const isAllSelected = category === 'choose-division' + ? groupsWithDivisions.length > 0 && groupsWithDivisions.every((group: any) => + checked[group.id]?.length === group.Division?.length + ) + : data.length > 0 && selectedDivision.length === data.length + + const handleSelectAll = () => { + if (category === 'choose-division') { + if (isAllSelected) { + setChecked({}) + } else { + const newChecked: CheckedState = {} + data.forEach((group: any) => { + if (group.Division?.length > 0) { + newChecked[group.id] = group.Division.map((d: any) => d.id) + } + }) + setChecked(newChecked) + } + } else { + if (isAllSelected) { + setSelectedDivision([]) + } else { + setSelectedDivision(data.map((d: any) => ({ id: d.id, name: d.name }))) + } + } + } + const handleSubmit = () => { if (category == "choose-division") { const selectedGroups: GroupData[] = []; @@ -154,25 +183,32 @@ export default function ModalSelectMultiple({ open, close, title, category, choo { category == 'share-division' ? <> - { - data.map((item: any, index: number) => { - return ( - { - handleDivisionClick(index) - }}> - {item.name} - { - selectedDivision.some((i: any) => i.id == item.id) - ? - : <> - } - - ) - }) - } + + {isAllSelected ? 'Batalkan Semua' : 'Pilih Semua'} + {isAllSelected && } + + {data.map((item: any, index: number) => { + return ( + { + handleDivisionClick(index) + }}> + {item.name} + { + selectedDivision.some((i: any) => i.id == item.id) + ? + : <> + } + + ) + })} : - data.map((item: any, index: number) => { + <> + + {isAllSelected ? 'Batalkan Semua' : 'Pilih Semua'} + {isAllSelected && } + + {data.map((item: any, index: number) => { return ( { handleGroupCheck(item.id) }}> @@ -199,7 +235,8 @@ export default function ModalSelectMultiple({ open, close, title, category, choo } ) - }) + })} + } @@ -207,4 +244,4 @@ export default function ModalSelectMultiple({ open, close, title, category, choo ) -} \ No newline at end of file +}