use theetrain requested change with

`svelte:window` instead of `mousedown` and `blur`
This commit is contained in:
jqlio18 2022-12-21 23:31:10 -05:00
commit ed43556679
2 changed files with 18 additions and 4 deletions

View file

@ -98,7 +98,7 @@
/** Obtain a reference to the button HTML element */ /** Obtain a reference to the button HTML element */
export let ref = null; export let ref = null;
import { createEventDispatcher } from "svelte"; import { createEventDispatcher, onMount } from "svelte";
import WarningFilled from "../icons/WarningFilled.svelte"; import WarningFilled from "../icons/WarningFilled.svelte";
import WarningAltFilled from "../icons/WarningAltFilled.svelte"; import WarningAltFilled from "../icons/WarningAltFilled.svelte";
import { import {
@ -111,6 +111,7 @@
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
let highlightedIndex = -1; let highlightedIndex = -1;
let mainDivRef;
$: inline = type === "inline"; $: inline = type === "inline";
$: selectedItem = items.find((item) => item.id === selectedId); $: selectedItem = items.find((item) => item.id === selectedId);
@ -148,9 +149,24 @@
const dispatchSelect = () => { const dispatchSelect = () => {
dispatch("select", { selectedId, selectedItem }); dispatch("select", { selectedId, selectedItem });
}; };
const pageClickHandler = ({ target }) => {
if (open && ref && !ref.contains(target)) {
open = false;
}
};
onMount(() => {
if (parent) {
parent.addEventListener("click", pageClickHandler);
}
});
</script> </script>
<svelte:window on:click="{pageClickHandler}" />
<div <div
bind:this="{mainDivRef}"
class:bx--dropdown__wrapper="{true}" class:bx--dropdown__wrapper="{true}"
class:bx--list-box__wrapper="{true}" class:bx--list-box__wrapper="{true}"
class:bx--dropdown__wrapper--inline="{inline}" class:bx--dropdown__wrapper--inline="{inline}"
@ -237,7 +253,6 @@
open = false; open = false;
} }
}}" }}"
on:blur="{() => (open = false)}"
on:keyup="{(e) => { on:keyup="{(e) => {
const { key } = e; const { key } = e;
if ([' '].includes(key)) { if ([' '].includes(key)) {
@ -281,7 +296,7 @@
active="{selectedId === item.id}" active="{selectedId === item.id}"
highlighted="{highlightedIndex === i}" highlighted="{highlightedIndex === i}"
disabled="{item.disabled}" disabled="{item.disabled}"
on:mousedown="{(e) => { on:click="{(e) => {
if (item.disabled) { if (item.disabled) {
e.stopPropagation(); e.stopPropagation();
return; return;

View file

@ -27,7 +27,6 @@
disabled="{disabled ? true : undefined}" disabled="{disabled ? true : undefined}"
{...$$restProps} {...$$restProps}
on:click on:click
on:mousedown
on:mouseenter on:mouseenter
on:mouseleave on:mouseleave
> >