feat: use new search input

This commit is contained in:
Xyphyn committed 2023-09-03 19:36:41 -07:00
1 parent 1f9bb5418d
commit d1776284b6
8 files changed
+41 -183

No files matched your search

+1 -1
View File
@@ -42,7 +42,7 @@
"@dicebear/initials": "^6.0.4", "@dicebear/initials": "^6.0.4",
"lemmy-js-client": "^0.18.0-rc.2", "lemmy-js-client": "^0.18.0-rc.2",
"markdown-it": "^13.0.1", "markdown-it": "^13.0.1",
"mono-svelte": "^0.1.6" "mono-svelte": "^0.1.9"
}, },
"type": "module" "type": "module"
} }
@@ -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>
-28
View File
@@ -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"> <script lang="ts">
import SearchInput from '$lib/components/input/SearchInput.svelte'
import Avatar from '$lib/components/ui/Avatar.svelte' import Avatar from '$lib/components/ui/Avatar.svelte'
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte' import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
import { getClient } from '$lib/lemmy.js' import { getClient } from '$lib/lemmy.js'
import type { Community, ListingType } from 'lemmy-js-client' import type { Community, ListingType } from 'lemmy-js-client'
import { Search } from 'mono-svelte'
import { createEventDispatcher } from 'svelte' import { createEventDispatcher } from 'svelte'
import { Icon, XCircle } from 'svelte-hero-icons' import { Icon, XCircle } from 'svelte-hero-icons'
import { fly } from 'svelte/transition'
let type: 'community' = 'community' let type: 'community' = 'community'
export let q: string = '' export let q: string = ''
export let jwt: string | undefined = undefined export let jwt: string | undefined = undefined
export let listing_type: ListingType = 'Subscribed' export let listing_type: ListingType = 'Subscribed'
export let items: Community[] | undefined = undefined
export let showWhenEmpty: boolean = false export let showWhenEmpty: boolean = false
let showNone: boolean = false
const dispatcher = createEventDispatcher<{ select: Community | undefined }>() const dispatcher = createEventDispatcher<{ select: Community | undefined }>()
</script> </script>
{#if type == 'community'} {#if type == 'community'}
<SearchInput <Search
options={items || []} search={async (q) => {
on:search={async () => {
if (q == '') showNone = true
else showNone = false
const results = await getClient().search({ const results = await getClient().search({
q: q || ' ', q: q || ' ',
auth: jwt, auth: jwt,
@@ -35,38 +30,38 @@
sort: 'TopAll', sort: 'TopAll',
}) })
items = results.communities.map((c) => c.community) return results.communities.map((c) => c.community)
}} }}
debounceTime={600} extractName={(c) => `${c.title}@${new URL(c.actor_id).hostname}`}
extractName={(c) => c.title} on:select
bind:query={q} bind:q
extractSelected={(c) => {
if (c) dispatcher('select', c)
}}
let:extractName
let:extractSelected
let:option
{showWhenEmpty} {showWhenEmpty}
{...$$restProps} {...$$restProps}
let:query let:item
let:select
> >
{#if query == '' && showWhenEmpty} <div class="w-full h-full" slot="noresults">
<MenuButton on:click={() => dispatcher('select', undefined)}> {#if q == '' && showWhenEmpty}
<Icon src={XCircle} size="16" mini /> <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"> <div class="flex flex-col text-left">
<span>None</span> <span>{item.title}</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 class="text-xs opacity-80"> <span class="text-xs opacity-80">
{new URL(option.actor_id).hostname} {new URL(item.actor_id).hostname}
</span> </span>
</div> </div>
</MenuButton> </MenuButton>
{/if} </div>
</SearchInput> </Search>
{/if} {/if}
+1 -1
View File
@@ -39,7 +39,7 @@
on:click={(e) => click('click', e)} on:click={(e) => click('click', e)}
type="button" type="button"
class="flex flex-row gap-2 items-center px-4 py-2 w-full text-sm 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 color
]} disabled:opacity-50 disabled:pointer-events-none" ]} disabled:opacity-50 disabled:pointer-events-none"
{disabled} {disabled}
+6 -13
View File
@@ -1,12 +1,11 @@
<script lang="ts"> <script lang="ts">
import { profile } from '$lib/auth.js' import { profile } from '$lib/auth.js'
import SelectMenu from '$lib/components/input/SelectMenu.svelte'
import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte' import MarkdownEditor from '$lib/components/markdown/MarkdownEditor.svelte'
import { toast } from '$lib/components/ui/toasts/toasts.js' import { toast } from '$lib/components/ui/toasts/toasts.js'
import { getClient } from '$lib/lemmy.js' import { getClient } from '$lib/lemmy.js'
import type { EditSite } from 'lemmy-js-client' import type { EditSite } from 'lemmy-js-client'
import type { PageData } from './$types.js' import type { PageData } from './$types.js'
import { Button, Checkbox, TextInput } from 'mono-svelte' import { Button, Checkbox, Select, TextInput } from 'mono-svelte'
export let data: PageData export let data: PageData
@@ -70,17 +69,11 @@
<Checkbox checked={formData.enable_nsfw} defaultValue={true}> <Checkbox checked={formData.enable_nsfw} defaultValue={true}>
Enable NSFW Enable NSFW
</Checkbox> </Checkbox>
<SelectMenu <Select label="Registration Mode" bind:value={formData.registration_mode}>
label="Registration mode" <option value="Open">Open</option>
alignment="top-left" <option value="RequireApplication">Require Application</option>
options={['Closed', 'RequireApplication', 'Open']} <option value="Closed">Closed</option>
optionNames={['Closed', 'Require Application', 'Open Registration']} </Select>
selected={formData.registration_mode ?? 'Open'}
on:select={(e) => {
// @ts-ignore
formData.registration_mode = e.detail
}}
/>
{#if formData.registration_mode == 'RequireApplication'} {#if formData.registration_mode == 'RequireApplication'}
<MarkdownEditor <MarkdownEditor
previewButton previewButton
+4 -4
View File
@@ -2977,10 +2977,10 @@ mlly@^1.2.0, mlly@^1.4.0:
pkg-types "^1.0.3" pkg-types "^1.0.3"
ufo "^1.1.2" ufo "^1.1.2"
mono-svelte@^0.1.6: mono-svelte@^0.1.9:
version "0.1.6" version "0.1.9"
resolved "https://registry.yarnpkg.com/mono-svelte/-/mono-svelte-0.1.6.tgz#2b2bdfea6a52813592658992d0676f9b7a6135e0" resolved "https://registry.yarnpkg.com/mono-svelte/-/mono-svelte-0.1.9.tgz#6ba5638a1b0cf62dce0bd46c6a7818b2ec272d6b"
integrity sha512-HcsNhWwdXdCdpEmCa2RsAwJsROGZZy3XRymiLJH+Bglk0bcW+kTO9yRxDCVWqrWu9ac7GRKYvtY54kvLNpdJ6g== integrity sha512-/oxreFAoaI6ZKwpyrwqC7apd14zZYdu2lv8cjQbiz+xB82MgLyfWcKjYI4KyqZAyZ50jgP3ugGD8aTRhTyy/Gg==
mri@^1.1.0: mri@^1.1.0:
version "1.2.0" version "1.2.0"