DropDown: add select event, fix closing when click outside of body

This commit is contained in:
weaseldotro 2020-10-16 11:48:33 +03:00 committed by GitHub
commit 122d347492
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -113,6 +113,7 @@
* @typedef {{ id: DropdownItemId; text: DropdownItemText; }} DropdownItem
*/
import { createEventDispatcher } from "svelte";
import WarningFilled16 from "carbon-icons-svelte/lib/WarningFilled16";
import {
ListBox,
@ -121,6 +122,8 @@
ListBoxMenuItem,
} from "../ListBox";
const dispatch = createEventDispatcher();
let selectedId = undefined;
let highlightedIndex = -1;
@ -136,6 +139,9 @@
highlightedIndex = index;
}
$: if (selectedIndex > -1) {
dispatch("select", { selectedId, selectedIndex, selectedItem });
}
$: inline = type === "inline";
$: selectedItem = items[selectedIndex];
$: if (!open) {
@ -143,7 +149,7 @@
}
</script>
<svelte:body
<svelte:window
on:click="{({ target }) => {
if (open && ref && !ref.contains(target)) {
open = false;