2022-07-07 05:19:05 +08:00
|
|
|
<script>
|
|
|
|
import { createEventDispatcher, onMount } from "svelte";
|
|
|
|
import { fly } from "svelte/transition";
|
|
|
|
import { Collection } from "pocketbase";
|
|
|
|
import CommonHelper from "@/utils/CommonHelper";
|
|
|
|
|
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
const uniqueId = "search_" + CommonHelper.randomString(7);
|
|
|
|
|
|
|
|
export let value = "";
|
2023-04-14 19:37:51 +08:00
|
|
|
export let placeholder = 'Search term or filter like created > "2022-01-01"...';
|
2022-07-07 05:19:05 +08:00
|
|
|
|
|
|
|
// autocomplete filter component fields
|
|
|
|
export let autocompleteCollection = new Collection();
|
|
|
|
export let extraAutocompleteKeys = [];
|
2023-04-14 19:37:51 +08:00
|
|
|
|
2022-07-07 05:19:05 +08:00
|
|
|
let filterComponent;
|
|
|
|
let isFilterComponentLoading = false;
|
|
|
|
|
|
|
|
let searchInput;
|
|
|
|
let tempValue = "";
|
|
|
|
|
|
|
|
$: if (typeof value === "string") {
|
|
|
|
tempValue = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clear(focusInput = true) {
|
|
|
|
tempValue = "";
|
|
|
|
if (focusInput) {
|
|
|
|
searchInput?.focus();
|
|
|
|
}
|
|
|
|
dispatch("clear");
|
|
|
|
}
|
|
|
|
|
|
|
|
function submit() {
|
|
|
|
value = tempValue;
|
|
|
|
dispatch("submit", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function loadFilterComponent() {
|
|
|
|
if (filterComponent || isFilterComponentLoading) {
|
|
|
|
return; // already loaded or in the process
|
|
|
|
}
|
|
|
|
|
|
|
|
isFilterComponentLoading = true;
|
|
|
|
|
|
|
|
filterComponent = (await import("@/components/base/FilterAutocompleteInput.svelte")).default;
|
|
|
|
|
|
|
|
isFilterComponentLoading = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
loadFilterComponent();
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2023-01-24 03:57:35 +08:00
|
|
|
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
|
|
|
<form class="searchbar" on:click|stopPropagation on:submit|preventDefault={submit}>
|
|
|
|
<label for={uniqueId} class="m-l-10 txt-xl">
|
|
|
|
<i class="ri-search-line" />
|
|
|
|
</label>
|
|
|
|
|
|
|
|
{#if filterComponent && !isFilterComponentLoading}
|
|
|
|
<svelte:component
|
|
|
|
this={filterComponent}
|
|
|
|
id={uniqueId}
|
|
|
|
singleLine
|
|
|
|
disableRequestKeys
|
|
|
|
disableIndirectCollectionsKeys
|
|
|
|
{extraAutocompleteKeys}
|
|
|
|
baseCollection={autocompleteCollection}
|
|
|
|
placeholder={value || placeholder}
|
|
|
|
bind:value={tempValue}
|
|
|
|
on:submit={submit}
|
|
|
|
/>
|
|
|
|
{:else}
|
|
|
|
<input
|
|
|
|
bind:this={searchInput}
|
|
|
|
type="text"
|
|
|
|
id={uniqueId}
|
|
|
|
placeholder={value || placeholder}
|
|
|
|
bind:value={tempValue}
|
|
|
|
/>
|
|
|
|
{/if}
|
|
|
|
|
2023-03-17 01:21:16 +08:00
|
|
|
{#if (value.length || tempValue.length) && tempValue != value}
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
class="btn btn-expanded btn-sm btn-warning"
|
|
|
|
transition:fly|local={{ duration: 150, x: 5 }}
|
|
|
|
>
|
|
|
|
<span class="txt">Search</span>
|
|
|
|
</button>
|
|
|
|
{/if}
|
2023-01-24 03:57:35 +08:00
|
|
|
|
2023-03-17 01:21:16 +08:00
|
|
|
{#if value.length || tempValue.length}
|
2023-01-24 03:57:35 +08:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-transparent btn-sm btn-hint p-l-xs p-r-xs m-l-10"
|
|
|
|
transition:fly|local={{ duration: 150, x: 5 }}
|
|
|
|
on:click={() => {
|
|
|
|
clear(false);
|
|
|
|
submit();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<span class="txt">Clear</span>
|
|
|
|
</button>
|
|
|
|
{/if}
|
|
|
|
</form>
|