Improve dropdown

This commit is contained in:
Marley 2023-07-14 20:32:41 -07:00
parent a135c12f03
commit 712da967ce
2 changed files with 14 additions and 4 deletions

View file

@ -7,19 +7,24 @@ const emit = defineEmits<{
select: [item: string | number] select: [item: string | number]
}>() }>()
const open = ref(false) const [open, toggleOpen] = useToggle()
const select = function (item) { const select = function (item) {
emit('select', typeof item === 'object' ? item.id : item) emit('select', typeof item === 'object' ? item.id : item)
toggleOpen(false)
} }
const dropdown = ref(null)
onClickOutside(dropdown, () => toggleOpen(false))
</script> </script>
<template> <template>
<div> <div ref="dropdown">
<PrimaryButton @click="open = !open"> <PrimaryButton @click="toggleOpen()">
<slot /> &downarrow; <slot /> &downarrow;
</PrimaryButton> </PrimaryButton>
<!--suppress VueUnrecognizedDirective -->
<ul v-show="open" v-bind="$attrs"> <ul v-show="open" v-bind="$attrs">
<li v-for="(item, index) in options" :key="index" @click="select(item)"> <li v-for="(item, index) in options" :key="index" @click="select(item)">
<template v-if="typeof item === 'object'"> <template v-if="typeof item === 'object'">

View file

@ -4,6 +4,8 @@ import medias from '@data/medias'
const mediaList = computed(() => { const mediaList = computed(() => {
return medias.filter((m) => m.type === 'book') return medias.filter((m) => m.type === 'book')
}) })
const category = ref('Books')
</script> </script>
<template> <template>
@ -15,7 +17,10 @@ const mediaList = computed(() => {
<TableTh>Copies</TableTh> <TableTh>Copies</TableTh>
<TableTh>Checked Out</TableTh> <TableTh>Checked Out</TableTh>
<TableTh actions> <TableTh actions>
<Dropdown :options="['books', 'films', 'albums']">Type</Dropdown> <Dropdown :options="['Books', 'Films', 'Albums']"
@select="(i) => category = (i as string)">
{{ category }}
</Dropdown>
<PrimaryButton>Add New</PrimaryButton> <PrimaryButton>Add New</PrimaryButton>
</TableTh> </TableTh>
</tr> </tr>