Files
stufflog2/svelte-ui/src/components/ItemSelect.svelte
T
gisle eeba5bc9c8
continuous-integration/drone/push Build is passing
add new fields to logs and goals.
2021-01-17 19:01:13 +01:00

36 lines
967 B
Svelte

<script lang="ts">
import groupStore from "../stores/group";
export let value = "";
export let name = "";
export let disabled = false;
export let optional = false;
$: {
if ($groupStore.stale && !$groupStore.loading) {
groupStore.load();
}
}
$: {
if ($groupStore.groups.length > 0 && value === "" && !optional) {
const nonEmpty = $groupStore.groups.find(g => g.items.length > 0);
if (nonEmpty != null) {
value = nonEmpty.items[0].id;
}
}
}
</script>
<select name={name} bind:value={value} disabled={disabled || $groupStore.loading}>
{#if optional}
<option value={""} selected={"" === value}>None</option>
{/if}
{#each $groupStore.groups as group (group.id)}
<optgroup label={group.name}>
{#each group.items as item (item.id)}
<option value={item.id} selected={item.id === value}>{item.name} ({item.groupWeight})</option>
{/each}
</optgroup>
{/each}
</select>