Skip to content

Commit

Permalink
define DispatchEvents type used to annotate createEventDispatcher() (#32
Browse files Browse the repository at this point in the history
)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Janosh Riebesell <[email protected]>
  • Loading branch information
3 people authored Feb 21, 2022
1 parent e79fab2 commit adfdc23
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/lib/MultiSelect.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { createEventDispatcher, onMount } from 'svelte'
import { fly } from 'svelte/transition'
import type { Option, Primitive, ProtoOption } from './'
import type { Option, Primitive, ProtoOption, DispatchEvents } from './'
import { onClickOutside } from './actions'
import { CrossIcon, ExpandIcon, ReadOnlyIcon } from './icons'
import Wiggle from './Wiggle.svelte'
Expand Down Expand Up @@ -49,7 +49,7 @@
})
let wiggle = false
const dispatch = createEventDispatcher()
const dispatch = createEventDispatcher<DispatchEvents>()
function isObject(item: unknown) {
return typeof item === `object` && !Array.isArray(item) && item !== null
Expand Down Expand Up @@ -127,8 +127,11 @@
function remove(label: Primitive) {
if (selected.length === 0 || readonly) return
selected = selected.filter((option) => label !== option.label)
const option = _options.find((option) => option.label === label)
if (!option) {
return console.error(`MultiSelect: option with label ${label} not found`)
}
selected = selected.filter((option) => label !== option.label)
dispatch(`remove`, { option })
dispatch(`change`, { option, type: `remove` })
}
Expand Down
12 changes: 12 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ export type ProtoOption =
| (Omit<Option, `value`> & {
value?: Primitive
})

export type DispatchEvents = {
add: { option: Option }
remove: { option: Option }
removeAll: { options: Option[] }
change: {
option?: Option
options?: Option[]
type: 'add' | 'remove' | 'removeAll'
}
blur: undefined
}

0 comments on commit adfdc23

Please sign in to comment.