2023-06-06 17:32:20 +02:00
|
|
|
<script>
|
|
|
|
import FancyCheckbox from "./FancyCheckbox.svelte"
|
|
|
|
import FancyForm from "./FancyForm.svelte"
|
2023-06-06 18:40:14 +02:00
|
|
|
import { createEventDispatcher } from "svelte"
|
2023-06-06 17:32:20 +02:00
|
|
|
|
|
|
|
export let options = []
|
2023-06-06 18:40:14 +02:00
|
|
|
export let selected = []
|
2023-06-06 17:32:20 +02:00
|
|
|
export let showSelectAll = true
|
|
|
|
export let selectAllText = "Select all"
|
|
|
|
|
2023-06-06 17:52:06 +02:00
|
|
|
let selectedBooleans = reset()
|
2023-06-06 18:40:14 +02:00
|
|
|
const dispatch = createEventDispatcher()
|
2023-06-06 17:52:06 +02:00
|
|
|
|
2023-06-06 18:40:14 +02:00
|
|
|
$: updateSelected(selectedBooleans)
|
|
|
|
$: dispatch("change", selected)
|
|
|
|
$: allSelected = selected?.length === options.length
|
2023-06-06 17:52:06 +02:00
|
|
|
|
|
|
|
function reset() {
|
|
|
|
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])
|
|
|
|
}
|
2023-06-06 17:32:20 +02:00
|
|
|
}
|
2023-06-06 18:40:14 +02:00
|
|
|
selected = array
|
2023-06-06 17:32:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function toggleSelectAll() {
|
|
|
|
if (allSelected === true) {
|
2023-06-06 17:52:06 +02:00
|
|
|
selectedBooleans = []
|
2023-06-06 17:32:20 +02:00
|
|
|
} else {
|
2023-06-06 17:52:06 +02:00
|
|
|
selectedBooleans = reset()
|
2023-06-06 17:32:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
{#if options && Array.isArray(options)}
|
|
|
|
<FancyForm compact noMaxWidth on:change>
|
|
|
|
{#if showSelectAll}
|
|
|
|
<div class="checkbox-item">
|
|
|
|
<FancyCheckbox
|
|
|
|
bind:value={allSelected}
|
|
|
|
on:change={toggleSelectAll}
|
|
|
|
text={selectAllText}
|
|
|
|
compress
|
|
|
|
lighter
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
{#each options as option, i}
|
2023-06-06 17:52:06 +02:00
|
|
|
<FancyCheckbox bind:value={selectedBooleans[i]} text={option} compress />
|
2023-06-06 17:32:20 +02:00
|
|
|
{/each}
|
|
|
|
</FancyForm>
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.checkbox-item:hover {
|
|
|
|
background-color: rgba(255, 255, 255, 0.01);
|
|
|
|
}
|
|
|
|
</style>
|