-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #306 from Roaryyyy/roary-features/chat-window
AII-449
- Loading branch information
Showing
6 changed files
with
108 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,85 @@ | ||
<script> | ||
import { Input, Dropdown, DropdownMenu, DropdownItem, Spinner, DropdownToggle } from '@sveltestrap/sveltestrap'; | ||
import { debounce } from 'lodash'; | ||
/** @type {{id: string, name: string} | null} */ | ||
export let selectedValue; | ||
export let disabled = false; | ||
export let placeholder = ''; | ||
/** | ||
import { | ||
Input, | ||
Dropdown, | ||
DropdownMenu, | ||
DropdownItem, | ||
Spinner, | ||
DropdownToggle | ||
} from '@sveltestrap/sveltestrap'; | ||
import { debounce } from 'lodash'; | ||
* @type {(arg0: any) => any[] | PromiseLike<any[]>} | ||
*/ | ||
export let onSearch; | ||
export let loading = false; | ||
/** @type {string} */ | ||
export let value; | ||
export let disabled = false; | ||
export let placeholder = ''; | ||
/** | ||
* @type {(query: string) => Promise<({id: string; name: string} | string)[]>} | ||
*/ | ||
export let onSearch; | ||
export let loading = false; | ||
/** @type {any[]} */ | ||
let searchResults = []; | ||
let isOpen = false; | ||
/** @type {({id: string; name: string} | string)[]} */ | ||
let searchResults = []; | ||
let isOpen = false; | ||
// @ts-ignore | ||
const debouncedSearch = debounce(async (query) => { | ||
if (query.length) { | ||
loading = true; | ||
searchResults = await onSearch(query); | ||
loading = false; | ||
isOpen = true; | ||
} else { | ||
searchResults = []; | ||
isOpen = false; | ||
} | ||
}, 500); | ||
// @ts-ignore | ||
const debouncedSearch = debounce(async (query) => { | ||
if (query.length) { | ||
loading = true; | ||
searchResults = await onSearch(query); | ||
loading = false; | ||
} else { | ||
searchResults = []; | ||
isOpen = false; | ||
} | ||
}, 500); | ||
/** | ||
* @param {any} e | ||
*/ | ||
async function handleInput(e) { | ||
const query = e.target.value; | ||
selectedValue = { id: query, name: query }; | ||
await debouncedSearch(query); | ||
} | ||
/** | ||
* @param {any} e | ||
*/ | ||
async function handleInput(e) { | ||
const query = e.target.value; | ||
isOpen = true; | ||
value = query; | ||
await debouncedSearch(query); | ||
} | ||
/** | ||
* @param {{ id: string; name: string; }} result | ||
*/ | ||
function selectResult(result) { | ||
selectedValue = result; | ||
} | ||
export function clearSearchResults() { | ||
searchResults = []; | ||
} | ||
/** | ||
* @param { {id: string; name: string} | string } result | ||
*/ | ||
function selectResult(result) { | ||
value = typeof result === 'string' ? result : result?.id; | ||
} | ||
export function clearSearchResults() { | ||
searchResults = []; | ||
} | ||
</script> | ||
<div class="position-relative"> | ||
<Dropdown class="scrollable-dropdown" isOpen={isOpen && (searchResults.length > 0 || loading)} toggle={() => isOpen = !isOpen}> | ||
<DropdownToggle tag="div"> | ||
<Input | ||
type="text" | ||
value={selectedValue?.name} | ||
on:input={handleInput} | ||
{disabled} | ||
{placeholder} | ||
/> | ||
</DropdownToggle> | ||
<DropdownMenu class="w-100"> | ||
{#if loading} | ||
<DropdownItem> | ||
<Spinner size="sm" /> | ||
</DropdownItem> | ||
{:else} | ||
{#each searchResults as result, index} | ||
<DropdownItem | ||
active={selectedValue?.id === result.id} | ||
on:click={() => selectResult(result)} | ||
title={result.name} | ||
> | ||
{result.name} | ||
</DropdownItem> | ||
{/each} | ||
{/if} | ||
</DropdownMenu> | ||
</Dropdown> | ||
</div> | ||
<Dropdown | ||
class="scrollable-dropdown" | ||
isOpen={isOpen && (searchResults.length > 0 || loading)} | ||
toggle={() => (isOpen = !isOpen)} | ||
> | ||
<DropdownToggle tag="div"> | ||
<Input type="text" {value} on:input={handleInput} {disabled} {placeholder} /> | ||
</DropdownToggle> | ||
<DropdownMenu class="w-100"> | ||
{#if loading} | ||
<li class="text-center"><Spinner size="sm" /></li> | ||
{:else} | ||
{#each searchResults as result, index} | ||
<DropdownItem | ||
active={value === (typeof result === 'string' ? result : result?.id)} | ||
on:click={() => selectResult(result)} | ||
title={typeof result === 'string' ? result : result?.name} | ||
> | ||
{typeof result === 'string' ? result : result?.name} | ||
</DropdownItem> | ||
{/each} | ||
{/if} | ||
</DropdownMenu> | ||
</Dropdown> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters