Skip to content

Commit

Permalink
Disable item dropdown when no items are available
Browse files Browse the repository at this point in the history
Disables the item dropdown when no items can be selected, typically
because no type has been selected yet.
Should improve the reliability of the test.

Also converts presentWidget to runes mode post Svelte 5 upgrade.
  • Loading branch information
SapiensAnatis committed Oct 22, 2024
1 parent a4a92d2 commit 468aaab
Showing 1 changed file with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,32 @@
type PresentWidgetData
} from './presentTypes.ts';
export let widgetData: PresentWidgetData;
let { widgetData }: { widgetData: PresentWidgetData } = $props();
let disableQuantity = false;
let disableQuantity = $state(false);
let typeValue: EntityType | '';
let itemValue: number | '';
let quantityValue: number = 1;
let typeValue: EntityType | '' = $state('');
let itemValue: number | '' = $state('');
let quantityValue: number = $state(1);
const form = createForm();
const type = form.field();
const item = form.field();
const quantity = form.field();
$: types = widgetData.types
const types = $derived(widgetData.types
.map(({ type }) => ({
value: type,
label: $t(`entity.${formatTypeKey(type)}.label`)
}))
.sort((a, b) => a.label.localeCompare(b.label));
.sort((a, b) => a.label.localeCompare(b.label)));
$: availableItems = getAvailableItems(typeValue);
const getAvailableItems = (type: EntityType | '') => {
if (!type) {
const availableItems = $derived.by(() => {
if (!typeValue) {
return [];
}
const itemList = widgetData.availableItems[type];
const itemList = widgetData.availableItems[typeValue];
if (!itemList) {
return [];
Expand All @@ -57,9 +55,13 @@
label: $t(`entity.${formatTypeKey(typeValue)}.item.${id}`)
}))
.sort((a, b) => a.label.localeCompare(b.label));
};
});
const disableItem = $derived(availableItems.length > 0 ? undefined : "true")
const onSubmit = (evt: SubmitEvent) => {
evt.preventDefault();
const onSubmit = () => {
if (typeValue === '' || itemValue === '') return;
const submission: PresentFormSubmission = {
Expand All @@ -84,6 +86,8 @@
};
</script>

{@debug disableItem}

Check warning on line 89 in Website/src/routes/(main)/account/save-editor/present/presentWidget.svelte

View workflow job for this annotation

GitHub Actions / Lint

Unexpected `{@debug}`

<Card.Root>
<Card.Header>
<Card.Title>
Expand All @@ -95,7 +99,7 @@
</Card.Header>
<Card.Content>
<p class="mb-5">Use this widget to add presents to your gift box.</p>
<form use:form on:submit|preventDefault={onSubmit} aria-labelledby="gift-box-title">
<form use:form onsubmit={onSubmit} aria-labelledby="gift-box-title">
<div class="flex flex-row flex-wrap gap-4">
<div class="labelled-input">
<Label for="type">Type</Label>
Expand All @@ -121,6 +125,7 @@
id="item"
placeholder="Select an item"
items={availableItems}
disabled={disableItem}
field={item}
required
class="
Expand Down

0 comments on commit 468aaab

Please sign in to comment.