mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
Merge pull request #339 from weaseldotro/patch-1
DropDown: add select event, fix closing when click outside of body
This commit is contained in:
commit
b69fe25ed0
1 changed files with 7 additions and 1 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue