feat: use new search input
This commit is contained in:
1 parent
1f9bb5418d
commit
d1776284b6
8 files changed
+41
-183
No files matched your search
@@ -1,60 +0,0 @@
|
||||
<script lang="ts">
|
||||
import Menu from '$lib/components/ui/menu/Menu.svelte'
|
||||
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
|
||||
import { TextInput } from 'mono-svelte'
|
||||
import { createEventDispatcher } from 'svelte'
|
||||
|
||||
export let query: string
|
||||
export let debounceTime: number = 300
|
||||
|
||||
type T = $$Generic
|
||||
|
||||
export let options: T[]
|
||||
export let extractSelected: (item: T | null) => any
|
||||
export let extractName: (item: T) => string
|
||||
export let showWhenEmpty: boolean = false
|
||||
let canSearch: boolean = false
|
||||
export { canSearch as searchOnMount }
|
||||
|
||||
const debounce = (fn: Function, ms = 300) => {
|
||||
let timeoutId: ReturnType<typeof setTimeout>
|
||||
return function (this: any, ...args: any[]) {
|
||||
clearTimeout(timeoutId)
|
||||
timeoutId = setTimeout(() => fn.apply(this, args), ms)
|
||||
}
|
||||
}
|
||||
|
||||
const dispatcher = createEventDispatcher<{ search: string }>()
|
||||
|
||||
const debounceFunc = debounce(() => {
|
||||
canSearch = true
|
||||
dispatcher('search', query)
|
||||
}, debounceTime)
|
||||
</script>
|
||||
|
||||
<div class="relative">
|
||||
<TextInput
|
||||
bind:value={query}
|
||||
on:input={() => {
|
||||
extractSelected(null)
|
||||
debounceFunc()
|
||||
}}
|
||||
{...$$restProps}
|
||||
/>
|
||||
<Menu
|
||||
open={(options.length != 0 || showWhenEmpty) && canSearch}
|
||||
alignment="bottom-left"
|
||||
>
|
||||
{#if query == '' && showWhenEmpty}
|
||||
<slot {extractName} {extractSelected} {query} />
|
||||
{:else}
|
||||
{#each options as option}
|
||||
<slot {extractName} {extractSelected} {option} {query}>
|
||||
<MenuButton on:click={() => extractSelected(option)}>
|
||||
{extractName(option)}
|
||||
</MenuButton>
|
||||
</slot>
|
||||
{/each}
|
||||
{/if}
|
||||
</Menu>
|
||||
</div>
|
||||
@@ -1,42 +0,0 @@
|
||||
<script lang="ts">
|
||||
import Menu from '$lib/components/ui/menu/Menu.svelte'
|
||||
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
|
||||
import type { Alignment } from '$lib/components/ui/menu/menu.js'
|
||||
import { Button } from 'mono-svelte'
|
||||
import { createEventDispatcher } from 'svelte'
|
||||
|
||||
type T = $$Generic
|
||||
|
||||
export let options: T[]
|
||||
export let optionNames: string[] = []
|
||||
export let selected: T
|
||||
export let label: string | undefined = undefined
|
||||
export let alignment: Alignment = 'bottom-left'
|
||||
|
||||
let open = false
|
||||
|
||||
const dispatcher = createEventDispatcher<{ select: T }>()
|
||||
</script>
|
||||
|
||||
{#if label}
|
||||
<div class="block font-bold text-sm w-max">{label}</div>
|
||||
{/if}
|
||||
<Menu let:toggleOpen {alignment}>
|
||||
<div let:toggleOpen slot="button" class="w-max relative">
|
||||
<Button on:click={toggleOpen} color="secondary">
|
||||
<slot>
|
||||
{optionNames[options.findIndex((o) => selected == o)] || selected}
|
||||
</slot>
|
||||
</Button>
|
||||
</div>
|
||||
{#each options as option, index}
|
||||
<MenuButton
|
||||
on:click={() => {
|
||||
selected = option
|
||||
dispatcher('select', option)
|
||||
}}
|
||||
>
|
||||
{optionNames[index] || option}
|
||||
</MenuButton>
|
||||
{/each}
|
||||
</Menu>
|
||||
@@ -1,28 +0,0 @@
|
||||
<script lang="ts">
|
||||
import { createEventDispatcher } from 'svelte'
|
||||
|
||||
export let enabled = false
|
||||
const dispatcher = createEventDispatcher<{ change: boolean }>()
|
||||
</script>
|
||||
|
||||
<label>
|
||||
<div
|
||||
class="relative w-10 border-2 h-6 transition-colors cursor-pointer
|
||||
rounded-full flex flex-row items-center {enabled
|
||||
? 'bg-blue-500 border-blue-500'
|
||||
: 'bg-slate-200 dark:bg-zinc-800 border-slate-200 dark:border-zinc-800'}"
|
||||
>
|
||||
<div
|
||||
class="w-5 h-5 rounded-full transition-transform
|
||||
bg-white dark:bg-zinc-700 {enabled ? 'translate-x-4' : 'translate-x-0'}"
|
||||
/>
|
||||
</div>
|
||||
<input
|
||||
type="checkbox"
|
||||
bind:checked={enabled}
|
||||
class="hidden"
|
||||
on:input={(e) => {
|
||||
dispatcher('change', !enabled)
|
||||
}}
|
||||
/>
|
||||
</label>
|
||||
@@ -1,31 +1,26 @@
|
||||
<script lang="ts">
|
||||
import SearchInput from '$lib/components/input/SearchInput.svelte'
|
||||
import Avatar from '$lib/components/ui/Avatar.svelte'
|
||||
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
|
||||
import { getClient } from '$lib/lemmy.js'
|
||||
import type { Community, ListingType } from 'lemmy-js-client'
|
||||
import { Search } from 'mono-svelte'
|
||||
import { createEventDispatcher } from 'svelte'
|
||||
import { Icon, XCircle } from 'svelte-hero-icons'
|
||||
import { fly } from 'svelte/transition'
|
||||
|
||||
let type: 'community' = 'community'
|
||||
export let q: string = ''
|
||||
|
||||
export let jwt: string | undefined = undefined
|
||||
export let listing_type: ListingType = 'Subscribed'
|
||||
export let items: Community[] | undefined = undefined
|
||||
export let showWhenEmpty: boolean = false
|
||||
let showNone: boolean = false
|
||||
|
||||
const dispatcher = createEventDispatcher<{ select: Community | undefined }>()
|
||||
</script>
|
||||
|
||||
{#if type == 'community'}
|
||||
<SearchInput
|
||||
options={items || []}
|
||||
on:search={async () => {
|
||||
if (q == '') showNone = true
|
||||
else showNone = false
|
||||
|
||||
<Search
|
||||
search={async (q) => {
|
||||
const results = await getClient().search({
|
||||
q: q || ' ',
|
||||
auth: jwt,
|
||||
@@ -35,38 +30,38 @@
|
||||
sort: 'TopAll',
|
||||
})
|
||||
|
||||
items = results.communities.map((c) => c.community)
|
||||
return results.communities.map((c) => c.community)
|
||||
}}
|
||||
debounceTime={600}
|
||||
extractName={(c) => c.title}
|
||||
bind:query={q}
|
||||
extractSelected={(c) => {
|
||||
if (c) dispatcher('select', c)
|
||||
}}
|
||||
let:extractName
|
||||
let:extractSelected
|
||||
let:option
|
||||
extractName={(c) => `${c.title}@${new URL(c.actor_id).hostname}`}
|
||||
on:select
|
||||
bind:q
|
||||
{showWhenEmpty}
|
||||
{...$$restProps}
|
||||
let:query
|
||||
let:item
|
||||
let:select
|
||||
>
|
||||
{#if query == '' && showWhenEmpty}
|
||||
<MenuButton on:click={() => dispatcher('select', undefined)}>
|
||||
<Icon src={XCircle} size="16" mini />
|
||||
<div class="w-full h-full" slot="noresults">
|
||||
{#if q == '' && showWhenEmpty}
|
||||
<MenuButton on:click={() => dispatcher('select', undefined)}>
|
||||
<Icon src={XCircle} size="16" mini />
|
||||
<div class="flex flex-col text-left">
|
||||
<span>None</span>
|
||||
</div>
|
||||
</MenuButton>
|
||||
{:else}
|
||||
<span class="mx-auto my-auto">No results.</span>
|
||||
{/if}
|
||||
</div>
|
||||
<div in:fly|global={{ y: -4, opacity: 0 }}>
|
||||
<MenuButton on:click={() => select(item)}>
|
||||
<Avatar url={item.icon} alt={item.title} width={24} />
|
||||
<div class="flex flex-col text-left">
|
||||
<span>None</span>
|
||||
</div>
|
||||
</MenuButton>
|
||||
{:else if option}
|
||||
<MenuButton on:click={() => extractSelected(option)}>
|
||||
<Avatar url={option.icon} alt={option.title} width={24} />
|
||||
<div class="flex flex-col text-left">
|
||||
<span>{option.title}</span>
|
||||
<span>{item.title}</span>
|
||||
<span class="text-xs opacity-80">
|
||||
{new URL(option.actor_id).hostname}
|
||||
{new URL(item.actor_id).hostname}
|
||||
</span>
|
||||
</div>
|
||||
</MenuButton>
|
||||
{/if}
|
||||
</SearchInput>
|
||||
</div>
|
||||
</Search>
|
||||
{/if}
|
||||
@@ -39,7 +39,7 @@
|
||||
on:click={(e) => click('click', e)}
|
||||
type="button"
|
||||
class="flex flex-row gap-2 items-center px-4 py-2 w-full text-sm
|
||||
transition-colors {buttonColors[
|
||||
transition-colors list-none {buttonColors[
|
||||
color
|
||||
]} disabled:opacity-50 disabled:pointer-events-none"
|
||||
{disabled}
|
||||
|
||||
Reference in new issue
Block a user