Adding better selection control to fancy checkbox group.

This commit is contained in:
mike12345567 2023-06-06 16:52:06 +01:00
parent 1760fcb34d
commit f6108998f9
1 changed files with 21 additions and 12 deletions

View File

@ -3,26 +3,35 @@
import FancyForm from "./FancyForm.svelte" import FancyForm from "./FancyForm.svelte"
export let options = [] export let options = []
export let selected = [] export let selected
export let showSelectAll = true export let showSelectAll = true
export let selectAllText = "Select all" export let selectAllText = "Select all"
let allSelected = false let selectedBooleans = reset()
$: {
if (selected.length === options.length) { $: selected = updateSelected(selectedBooleans)
allSelected = true $: console.log(selected)
} else if (selected.length === 0) { $: allSelected = selected.length === options.length
allSelected = false
} else { function reset() {
allSelected = "partial" return Array(options.length).fill(true)
} }
function updateSelected(selectedArr) {
const array = []
for (let [i, isSelected] of Object.entries(selectedArr)) {
if (isSelected) {
array.push(options[i])
}
}
return array
} }
function toggleSelectAll() { function toggleSelectAll() {
if (allSelected === true) { if (allSelected === true) {
selected = [] selectedBooleans = []
} else { } else {
selected = [...options] selectedBooleans = reset()
} }
} }
</script> </script>
@ -41,7 +50,7 @@
</div> </div>
{/if} {/if}
{#each options as option, i} {#each options as option, i}
<FancyCheckbox bind:value={selected[i]} text={option} compress /> <FancyCheckbox bind:value={selectedBooleans[i]} text={option} compress />
{/each} {/each}
</FancyForm> </FancyForm>
{/if} {/if}