Skip to content

Commit

Permalink
Update MultiSelect.svelte
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym committed Nov 9, 2024
1 parent f5de892 commit 6fbedab
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions src/MultiSelect/MultiSelect.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
*/
export let highlightedId = null;
import { afterUpdate, createEventDispatcher, setContext, tick } from "svelte";
import { afterUpdate, createEventDispatcher, setContext } from "svelte";
import WarningFilled from "../icons/WarningFilled.svelte";
import WarningAltFilled from "../icons/WarningAltFilled.svelte";
import Checkbox from "../Checkbox/Checkbox.svelte";
Expand All @@ -183,7 +183,6 @@
const dispatch = createEventDispatcher();
let initialSorted = false;
let highlightedIndex = -1;
let prevChecked = [];
Expand Down Expand Up @@ -227,18 +226,8 @@
highlightedIndex = index;
}
function sort() {
return [
...(checked.length > 1 ? checked.sort(sortItem) : checked),
...unchecked.sort(sortItem),
];
}
afterUpdate(() => {
if (checked.length !== prevChecked.length) {
if (selectionFeedback === "top") {
items = sort();
}
prevChecked = checked;
selectedIds = checked.map(({ id }) => id);
dispatch("select", {
Expand All @@ -247,29 +236,44 @@
unselected: unchecked,
});
}
});
$: if (!open) {
if (!initialSorted || selectionFeedback !== "fixed") {
tick().then(() => {
items = sort();
initialSorted = true;
});
if (!open) {
highlightedIndex = -1;
value = "";
}
highlightedIndex = -1;
value = "";
}
});
$: menuId = `menu-${id}`;
$: inline = type === "inline";
$: ariaLabel = $$props["aria-label"] || "Choose an item";
$: checked = items.filter(({ checked }) => checked);
$: unchecked = items.filter(({ checked }) => !checked);
$: filteredItems = items.filter((item) => filterItem(item, value));
$: sortedItems = (() => {
if (selectionFeedback === "top" && selectedIds.length > 0) {
const checkedItems = items
.filter((item) => selectedIds.includes(item.id))
.map((item) => ({ ...item, checked: true }));
const uncheckedItems = items
.filter((item) => !selectedIds.includes(item.id))
.map((item) => ({ ...item, checked: false }));
return [
...(checkedItems.length > 1
? checkedItems.sort(sortItem)
: checkedItems),
...uncheckedItems.sort(sortItem),
];
}
return items.map((item) => ({
...item,
checked: selectedIds.includes(item.id),
}));
})();
$: checked = sortedItems.filter(({ checked }) => checked);
$: unchecked = sortedItems.filter(({ checked }) => !checked);
$: filteredItems = sortedItems.filter((item) => filterItem(item, value));
$: highlightedId =
highlightedIndex > -1
? (filterable ? filteredItems : items)[highlightedIndex]?.id ?? null
? (filterable ? filteredItems : sortedItems)[highlightedIndex]?.id ?? null
: null;
</script>
Expand Down Expand Up @@ -362,7 +366,7 @@
change(-1);
} else if (key === 'Enter') {
if (highlightedIndex > -1) {
items = items.map((item, i) => {
sortedItems = sortedItems.map((item, i) => {
if (i !== highlightedIndex) return item;
return { ...item, checked: !item.checked };
});
Expand Down Expand Up @@ -390,7 +394,7 @@
on:clear
on:clear="{() => {
selectedIds = [];
items = items.map((item) => ({
sortedItems = sortedItems.map((item) => ({
...item,
checked: false,
}));
Expand Down Expand Up @@ -420,10 +424,10 @@
on:keydown|stopPropagation="{({ key }) => {
if (key === 'Enter') {
if (highlightedId) {
const filteredItemIndex = items.findIndex(
const filteredItemIndex = sortedItems.findIndex(
(item) => item.id === highlightedId
);
items = items.map((item, i) => {
sortedItems = sortedItems.map((item, i) => {
if (i !== filteredItemIndex) return item;
return { ...item, checked: !item.checked };
});
Expand Down Expand Up @@ -485,7 +489,7 @@
id="{id}"
aria-multiselectable="true"
>
{#each filterable ? filteredItems : items as item, i (item.id)}
{#each filterable ? filteredItems : sortedItems as item, i (item.id)}
<ListBoxMenuItem
id="{item.id}"
role="option"
Expand All @@ -499,11 +503,9 @@
e.stopPropagation();
return;
}
items = items.map((_) =>
sortedItems = sortedItems.map((_) =>
_.id === item.id ? { ..._, checked: !_.checked } : _
);
fieldRef.focus();
}}"
on:mouseenter="{() => {
Expand Down

0 comments on commit 6fbedab

Please sign in to comment.