Skip to content

Commit

Permalink
[SelectField] Fix handling of disabled options
Browse files Browse the repository at this point in the history
  • Loading branch information
techniq committed Jul 15, 2024
1 parent 19ca5a6 commit 295d5eb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-colts-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte-ux': patch
---

[SelectField] Fix handling of disabled options
15 changes: 13 additions & 2 deletions packages/svelte-ux/src/lib/components/_SelectListOptions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,22 @@
// Find the index of the clicked on element (ignoring group headers)
const optionIndex = slotEl
? [...menuOptionsEl.children]
.filter((el) => !el.classList.contains('group-header'))
.filter((el) => {
if (el.classList.contains('group-header')) {
// ignore clicks on group options
return false;
// @ts-expect-error
} else if (el.disabled) {
// ignore disabled items
return false;
} else {
return true;
}
})
.indexOf(slotEl)
: -1;
logger.debug({ slotEl, optionIndex });
// ignore clicks on group options

if (optionIndex !== -1) {
selectIndex(optionIndex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,29 +221,30 @@

<Preview>
<SelectField {options}>
<div slot="option" let:option let:index let:selected let:highlightIndex>
<svelte:fragment slot="option" let:option let:index let:selected let:highlightIndex>
<MenuItem
class={cls(
index === highlightIndex && 'bg-surface-content/5',
option === selected && 'font-semibold',
option.group ? 'px-4' : 'px-2'
)}
scrollIntoView={index === highlightIndex}
disabled={index === 2}
>
<div>
<div>{option.label}</div>
<div class="text-sm text-text-surface-content/50">{option.value}</div>
</div>
</MenuItem>
</div>
</svelte:fragment>
</SelectField>
</Preview>

<h2>option slot with icon (field icon updates based on selected option)</h2>

<Preview>
<SelectField {options} bind:value activeOptionIcon={true}>
<div slot="option" let:option let:index let:selected let:highlightIndex>
<svelte:fragment slot="option" let:option let:index let:selected let:highlightIndex>
<MenuItem
class={cls(
index === highlightIndex && 'bg-surface-content/5',
Expand All @@ -255,7 +256,7 @@
>
{option.label}
</MenuItem>
</div>
</svelte:fragment>
</SelectField>
</Preview>

Expand Down

0 comments on commit 295d5eb

Please sign in to comment.