feat: add dropdown to multiselect
This commit is contained in:
1 parent
af1bdf194c
commit
dc1aba6c17
8 files changed
+107
-62
No files matched your search
@@ -1,14 +1,20 @@
|
||||
<script lang="ts">
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
|
||||
import SelectMenu from '$lib/components/input/SelectMenu.svelte'
|
||||
|
||||
import { createEventDispatcher } from 'svelte'
|
||||
import { ChevronDown, Icon } from 'svelte-hero-icons'
|
||||
type T = $$Generic
|
||||
export let options: T[]
|
||||
export let disabled: boolean[] = []
|
||||
export let optionNames: string[] = []
|
||||
export let selected: T
|
||||
|
||||
const dispatcher = createEventDispatcher<{ select: T }>()
|
||||
$: {
|
||||
dispatcher('select', selected)
|
||||
}
|
||||
|
||||
let extraSelected: T | null = null
|
||||
$: dispatcher('select', selected)
|
||||
</script>
|
||||
|
||||
<div
|
||||
@@ -16,7 +22,7 @@
|
||||
rounded-lg dark:bg-zinc-900 gap-1 p-1 border border-slate-200 dark:border-zinc-800
|
||||
{$$props.class}"
|
||||
>
|
||||
{#each options as option, index}
|
||||
{#each options.slice(0, 4) as option, index}
|
||||
<button
|
||||
class="px-3 py-1.5 text-sm rounded-md
|
||||
transition-colors
|
||||
@@ -31,5 +37,33 @@
|
||||
{optionNames[index] || option}
|
||||
</button>
|
||||
{/each}
|
||||
{#if options.length > 4}
|
||||
<select
|
||||
bind:value={selected}
|
||||
class="bg-inherit text-sm mr-2 p-1.5 rounded-md {options
|
||||
.slice(4)
|
||||
.includes(selected)
|
||||
? 'bg-slate-900 text-slate-50 dark:bg-zinc-100 dark:text-black w-max'
|
||||
: 'w-4'}"
|
||||
>
|
||||
<Button color="tertiary">
|
||||
<Icon src={ChevronDown} size="16" mini />
|
||||
</Button>
|
||||
{#each options.slice(4) as option, index}
|
||||
<option value={option}>{optionNames[index + 4] || option}</option>
|
||||
{/each}
|
||||
</select>
|
||||
{/if}
|
||||
</div>
|
||||
<slot {selected} />
|
||||
|
||||
<style lang="postcss">
|
||||
.custom-select {
|
||||
@apply bg-slate-50 relative;
|
||||
}
|
||||
|
||||
.custom-select select {
|
||||
appearance: none;
|
||||
@apply bg-inherit hidden;
|
||||
}
|
||||
</style>
|
||||
@@ -1,5 +1,4 @@
|
||||
<script lang="ts">
|
||||
import SelectMenu from '$lib/components/input/SelectMenu.svelte'
|
||||
import TextInput from '$lib/components/input/TextInput.svelte'
|
||||
import Menu from '$lib/components/ui/menu/Menu.svelte'
|
||||
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
<script lang="ts">
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
|
||||
import { Color } from '$lib/ui/colors'
|
||||
import Menu from '$lib/components/ui/menu/Menu.svelte'
|
||||
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
|
||||
@@ -6,29 +8,19 @@
|
||||
type T = $$Generic
|
||||
|
||||
export let options: T[]
|
||||
export let optionNames: Map<any, string> = new Map()
|
||||
export let optionNames: string[]
|
||||
export let selected: T
|
||||
|
||||
let open = false
|
||||
|
||||
optionNames.set(null, 'None')
|
||||
</script>
|
||||
|
||||
<Menu let:toggleOpen>
|
||||
<button
|
||||
class="flex flex-row gap-2 items-center px-3 py-1.5 w-full text-sm
|
||||
transition-all max-w-64 rounded-md bg-slate-100 border border-slate-200
|
||||
dark:bg-zinc-800 dark:border-zinc-700"
|
||||
slot="button"
|
||||
type="button"
|
||||
on:click|preventDefault={toggleOpen}
|
||||
>
|
||||
{optionNames.get(selected) || selected}
|
||||
</button>
|
||||
<li class="text-xs opacity-80 my-1 py-1 mx-4">Communities</li>
|
||||
{#each options as option}
|
||||
<Button slot="button" on:click={toggleOpen} color="tertiary" size="square-md">
|
||||
<slot />
|
||||
</Button>
|
||||
{#each options as option, index}
|
||||
<MenuButton on:click={() => (selected = option)}>
|
||||
{optionNames.get(option) || option}
|
||||
{optionNames[index] || option}
|
||||
</MenuButton>
|
||||
{/each}
|
||||
</Menu>
|
||||
@@ -0,0 +1,37 @@
|
||||
<script lang="ts">
|
||||
import { page } from '$app/stores'
|
||||
import { searchParam } from '$lib/util.js'
|
||||
import MultiSelect from '../input/MultiSelect.svelte'
|
||||
|
||||
export let selected: string
|
||||
</script>
|
||||
|
||||
<MultiSelect
|
||||
options={[
|
||||
'Active',
|
||||
'Hot',
|
||||
'TopAll',
|
||||
'New',
|
||||
'Old',
|
||||
'TopMonth',
|
||||
'TopWeek',
|
||||
'TopDay',
|
||||
'MostComments',
|
||||
'NewComments',
|
||||
]}
|
||||
optionNames={[
|
||||
'Active',
|
||||
'Hot',
|
||||
'Top',
|
||||
'New',
|
||||
'Old',
|
||||
'Top Month',
|
||||
'Top Week',
|
||||
'Top Day',
|
||||
'Most Comments',
|
||||
'New Comments',
|
||||
]}
|
||||
{selected}
|
||||
on:select={(e) => searchParam($page.url, 'sort', e.detail, 'page')}
|
||||
class="w-full md:max-w-[20rem]"
|
||||
/>
|
||||
@@ -1,4 +1,19 @@
|
||||
import { goto } from '$app/navigation'
|
||||
|
||||
export const findClosestNumber = (numbers: number[], target: number): number =>
|
||||
numbers.reduce((prev, curr) =>
|
||||
Math.abs(curr - target) < Math.abs(prev - target) ? curr : prev
|
||||
)
|
||||
|
||||
export const searchParam = (
|
||||
url: URL,
|
||||
key: string,
|
||||
value: string,
|
||||
...deleteKeys: string[]
|
||||
) => {
|
||||
url.searchParams.set(key, value)
|
||||
deleteKeys.forEach((k) => url.searchParams.delete(k))
|
||||
goto(url, {
|
||||
invalidateAll: true,
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user