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
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "photon-lemmy",
|
"name": "photon-lemmy",
|
||||||
"version": "0.4.32",
|
"version": "0.4.33",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite dev",
|
"dev": "vite dev",
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import Button from '$lib/components/input/Button.svelte'
|
||||||
|
|
||||||
|
import SelectMenu from '$lib/components/input/SelectMenu.svelte'
|
||||||
|
|
||||||
import { createEventDispatcher } from 'svelte'
|
import { createEventDispatcher } from 'svelte'
|
||||||
|
import { ChevronDown, Icon } from 'svelte-hero-icons'
|
||||||
type T = $$Generic
|
type T = $$Generic
|
||||||
export let options: T[]
|
export let options: T[]
|
||||||
export let disabled: boolean[] = []
|
export let disabled: boolean[] = []
|
||||||
export let optionNames: string[] = []
|
export let optionNames: string[] = []
|
||||||
export let selected: T
|
export let selected: T
|
||||||
|
|
||||||
const dispatcher = createEventDispatcher<{ select: T }>()
|
const dispatcher = createEventDispatcher<{ select: T }>()
|
||||||
$: {
|
|
||||||
dispatcher('select', selected)
|
let extraSelected: T | null = null
|
||||||
}
|
$: dispatcher('select', selected)
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
@@ -16,7 +22,7 @@
|
|||||||
rounded-lg dark:bg-zinc-900 gap-1 p-1 border border-slate-200 dark:border-zinc-800
|
rounded-lg dark:bg-zinc-900 gap-1 p-1 border border-slate-200 dark:border-zinc-800
|
||||||
{$$props.class}"
|
{$$props.class}"
|
||||||
>
|
>
|
||||||
{#each options as option, index}
|
{#each options.slice(0, 4) as option, index}
|
||||||
<button
|
<button
|
||||||
class="px-3 py-1.5 text-sm rounded-md
|
class="px-3 py-1.5 text-sm rounded-md
|
||||||
transition-colors
|
transition-colors
|
||||||
@@ -31,5 +37,33 @@
|
|||||||
{optionNames[index] || option}
|
{optionNames[index] || option}
|
||||||
</button>
|
</button>
|
||||||
{/each}
|
{/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>
|
</div>
|
||||||
<slot {selected} />
|
<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">
|
<script lang="ts">
|
||||||
import SelectMenu from '$lib/components/input/SelectMenu.svelte'
|
|
||||||
import TextInput from '$lib/components/input/TextInput.svelte'
|
import TextInput from '$lib/components/input/TextInput.svelte'
|
||||||
import Menu from '$lib/components/ui/menu/Menu.svelte'
|
import Menu from '$lib/components/ui/menu/Menu.svelte'
|
||||||
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
|
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import Button from '$lib/components/input/Button.svelte'
|
||||||
|
|
||||||
import { Color } from '$lib/ui/colors'
|
import { Color } from '$lib/ui/colors'
|
||||||
import Menu from '$lib/components/ui/menu/Menu.svelte'
|
import Menu from '$lib/components/ui/menu/Menu.svelte'
|
||||||
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
|
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
|
||||||
@@ -6,29 +8,19 @@
|
|||||||
type T = $$Generic
|
type T = $$Generic
|
||||||
|
|
||||||
export let options: T[]
|
export let options: T[]
|
||||||
export let optionNames: Map<any, string> = new Map()
|
export let optionNames: string[]
|
||||||
export let selected: T
|
export let selected: T
|
||||||
|
|
||||||
let open = false
|
let open = false
|
||||||
|
|
||||||
optionNames.set(null, 'None')
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Menu let:toggleOpen>
|
<Menu let:toggleOpen>
|
||||||
<button
|
<Button slot="button" on:click={toggleOpen} color="tertiary" size="square-md">
|
||||||
class="flex flex-row gap-2 items-center px-3 py-1.5 w-full text-sm
|
<slot />
|
||||||
transition-all max-w-64 rounded-md bg-slate-100 border border-slate-200
|
</Button>
|
||||||
dark:bg-zinc-800 dark:border-zinc-700"
|
{#each options as option, index}
|
||||||
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}
|
|
||||||
<MenuButton on:click={() => (selected = option)}>
|
<MenuButton on:click={() => (selected = option)}>
|
||||||
{optionNames.get(option) || option}
|
{optionNames[index] || option}
|
||||||
</MenuButton>
|
</MenuButton>
|
||||||
{/each}
|
{/each}
|
||||||
</Menu>
|
</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 =>
|
export const findClosestNumber = (numbers: number[], target: number): number =>
|
||||||
numbers.reduce((prev, curr) =>
|
numbers.reduce((prev, curr) =>
|
||||||
Math.abs(curr - target) < Math.abs(prev - target) ? curr : prev
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
+4
-20
@@ -12,6 +12,8 @@
|
|||||||
import Pageination from '$lib/components/ui/Pageination.svelte'
|
import Pageination from '$lib/components/ui/Pageination.svelte'
|
||||||
import Modal from '$lib/components/ui/modal/Modal.svelte'
|
import Modal from '$lib/components/ui/modal/Modal.svelte'
|
||||||
import { profile } from '$lib/auth.js'
|
import { profile } from '$lib/auth.js'
|
||||||
|
import Sort from '$lib/components/lemmy/Sort.svelte'
|
||||||
|
import { searchParam } from '$lib/util.js'
|
||||||
|
|
||||||
export let data
|
export let data
|
||||||
|
|
||||||
@@ -54,20 +56,7 @@
|
|||||||
})
|
})
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<MultiSelect
|
<Sort selected={data.sort} />
|
||||||
options={['Active', 'Hot', 'TopAll', 'New', 'Old']}
|
|
||||||
optionNames={['Active', 'Hot', 'Top', 'New', 'Old']}
|
|
||||||
selected={data.sort}
|
|
||||||
on:select={(e) => {
|
|
||||||
const url = $page.url
|
|
||||||
url.searchParams.set('sort', e.detail)
|
|
||||||
url.searchParams.delete('page')
|
|
||||||
goto(url, {
|
|
||||||
invalidateAll: true,
|
|
||||||
})
|
|
||||||
}}
|
|
||||||
class="w-full md:max-w-[20rem]"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
<section class="flex flex-col gap-4">
|
<section class="flex flex-col gap-4">
|
||||||
{#if data.posts.posts.length == 0}
|
{#if data.posts.posts.length == 0}
|
||||||
@@ -99,12 +88,7 @@
|
|||||||
</section>
|
</section>
|
||||||
<Pageination
|
<Pageination
|
||||||
page={data.page}
|
page={data.page}
|
||||||
on:change={(p) => {
|
on:change={(p) => searchParam($page.url, 'page', p.detail.toString())}
|
||||||
$page.url.searchParams.set('page', p.detail.toString())
|
|
||||||
goto($page.url.toString(), {
|
|
||||||
invalidateAll: true,
|
|
||||||
})
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<aside class="hidden xl:block">
|
<aside class="hidden xl:block">
|
||||||
|
|||||||
@@ -13,6 +13,8 @@
|
|||||||
import Modal from '$lib/components/ui/modal/Modal.svelte'
|
import Modal from '$lib/components/ui/modal/Modal.svelte'
|
||||||
import Pageination from '$lib/components/ui/Pageination.svelte'
|
import Pageination from '$lib/components/ui/Pageination.svelte'
|
||||||
import Avatar from '$lib/components/ui/Avatar.svelte'
|
import Avatar from '$lib/components/ui/Avatar.svelte'
|
||||||
|
import Sort from '$lib/components/lemmy/Sort.svelte'
|
||||||
|
import { searchParam } from '$lib/util.js'
|
||||||
|
|
||||||
export let data
|
export let data
|
||||||
|
|
||||||
@@ -61,20 +63,7 @@
|
|||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex flex-col sm:flex-row gap-4 max-w-full w-full">
|
<div class="flex flex-col sm:flex-row gap-4 max-w-full w-full">
|
||||||
<MultiSelect
|
<Sort selected={data.sort} />
|
||||||
options={['Active', 'Hot', 'TopAll', 'New', 'Old']}
|
|
||||||
optionNames={['Active', 'Hot', 'Top', 'New', 'Old']}
|
|
||||||
selected={$page.url.searchParams.get('sort') ??
|
|
||||||
$userSettings.defaultSort.sort}
|
|
||||||
on:select={(e) => {
|
|
||||||
const url = $page.url
|
|
||||||
url.searchParams.set('sort', e.detail)
|
|
||||||
url.searchParams.delete('page')
|
|
||||||
goto(url, {
|
|
||||||
invalidateAll: true,
|
|
||||||
})
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
{#each data.posts.posts as post, index (post.post.id)}
|
{#each data.posts.posts as post, index (post.post.id)}
|
||||||
<div in:fly={{ y: -8, opacity: 0, delay: index < 4 ? index * 100 : 0 }}>
|
<div in:fly={{ y: -8, opacity: 0, delay: index < 4 ? index * 100 : 0 }}>
|
||||||
@@ -83,12 +72,7 @@
|
|||||||
{/each}
|
{/each}
|
||||||
<Pageination
|
<Pageination
|
||||||
page={data.page}
|
page={data.page}
|
||||||
on:change={(p) => {
|
on:change={(p) => searchParam($page.url, 'page', p.detail.toString())}
|
||||||
$page.url.searchParams.set('page', p.detail.toString())
|
|
||||||
goto($page.url.toString(), {
|
|
||||||
invalidateAll: true,
|
|
||||||
})
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div class="hidden xl:block">
|
<div class="hidden xl:block">
|
||||||
|
|||||||
Reference in new issue
Block a user