Allow collapsing of groups

This commit is contained in:
Tom Hubrecht 2022-03-07 11:29:45 +01:00
parent 27d904871d
commit 232a06feee
2 changed files with 45 additions and 21 deletions

View file

@ -16,6 +16,8 @@
let subfiltering = createTriStates(UNCHECKED, Object.entries(children).length);
let subselected = Array.from({ length: Object.entries(children).length }, _ => []);
let collapsed = Object.entries(children).length ? false : undefined;
function isVal(val) {
return function (other) {
return other === val;
@ -78,31 +80,39 @@
$: selected =
$filtering === CHECKED ? [item, ...subselected.flat()] : subselected.flat();
$: subHidden = () => (collapsed ? 'd-none' : '');
</script>
{#if item}
<div class="ml-1" style="padding-left: {2 * level}em">
<TriStateCheckbox state={$filtering} on:change={handleChange} value={item} />
<TriStateCheckbox
state={$filtering}
on:change={handleChange}
value={item}
bind:collapsed
/>
</div>
{#each Object.entries(children) as [toplevel, subchildren], i}
{#if subchildren}
<svelte:self
item={toplevel}
children={subchildren}
on:change={handleChildChange(i)}
filtering={subfiltering.storeAt(i)}
bind:selected={subselected[i]}
level={level + 1}
/>
{:else}
<svelte:self
item={toplevel}
on:change={handleChildChange(i)}
filtering={subfiltering.storeAt(i)}
bind:selected={subselected[i]}
level={level + 1}
/>
{/if}
{/each}
<div class={subHidden()}>
{#each Object.entries(children) as [toplevel, subchildren], i}
{#if subchildren}
<svelte:self
item={toplevel}
children={subchildren}
on:change={handleChildChange(i)}
filtering={subfiltering.storeAt(i)}
bind:selected={subselected[i]}
level={level + 1}
/>
{:else}
<svelte:self
item={toplevel}
on:change={handleChildChange(i)}
filtering={subfiltering.storeAt(i)}
bind:selected={subselected[i]}
level={level + 1}
/>
{/if}
{/each}
</div>
{/if}

View file

@ -3,6 +3,7 @@
export let state;
export let value;
export let collapsed = undefined;
import { UNCHECKED, CHECKED, WEIRD } from './stores';
import { createEventDispatcher } from 'svelte';
@ -18,6 +19,10 @@
dispatch('change', { value: state });
}
function handleCollapse(evt) {
collapsed = !collapsed;
}
$: icon = () => {
switch (state) {
case UNCHECKED:
@ -28,6 +33,10 @@
return 'check-square-fill';
}
};
$: colIcon = () => {
return collapsed ? 'chevron-expand' : 'chevron-contract';
};
</script>
<span class="cal w-100 d-block" on:click={handleClick}>
@ -35,6 +44,11 @@
<Icon name={icon()} />
</span>
<span class="ms-1">{value}</span>
{#if collapsed !== undefined}
<span class="float-end" on:click|stopPropagation={handleCollapse}>
<Icon name={colIcon()} />
</span>
{/if}
</span>
<style>