mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 10:21:05 +00:00
DropDown: add select event, fix closing when click outside of body
This commit is contained in:
parent
f7626e5b17
commit
122d347492
1 changed files with 7 additions and 1 deletions
|
@ -113,6 +113,7 @@
|
||||||
* @typedef {{ id: DropdownItemId; text: DropdownItemText; }} DropdownItem
|
* @typedef {{ id: DropdownItemId; text: DropdownItemText; }} DropdownItem
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { createEventDispatcher } from "svelte";
|
||||||
import WarningFilled16 from "carbon-icons-svelte/lib/WarningFilled16";
|
import WarningFilled16 from "carbon-icons-svelte/lib/WarningFilled16";
|
||||||
import {
|
import {
|
||||||
ListBox,
|
ListBox,
|
||||||
|
@ -121,6 +122,8 @@
|
||||||
ListBoxMenuItem,
|
ListBoxMenuItem,
|
||||||
} from "../ListBox";
|
} from "../ListBox";
|
||||||
|
|
||||||
|
const dispatch = createEventDispatcher();
|
||||||
|
|
||||||
let selectedId = undefined;
|
let selectedId = undefined;
|
||||||
let highlightedIndex = -1;
|
let highlightedIndex = -1;
|
||||||
|
|
||||||
|
@ -136,6 +139,9 @@
|
||||||
highlightedIndex = index;
|
highlightedIndex = index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$: if (selectedIndex > -1) {
|
||||||
|
dispatch("select", { selectedId, selectedIndex, selectedItem });
|
||||||
|
}
|
||||||
$: inline = type === "inline";
|
$: inline = type === "inline";
|
||||||
$: selectedItem = items[selectedIndex];
|
$: selectedItem = items[selectedIndex];
|
||||||
$: if (!open) {
|
$: if (!open) {
|
||||||
|
@ -143,7 +149,7 @@
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:body
|
<svelte:window
|
||||||
on:click="{({ target }) => {
|
on:click="{({ target }) => {
|
||||||
if (open && ref && !ref.contains(target)) {
|
if (open && ref && !ref.contains(target)) {
|
||||||
open = false;
|
open = false;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue