From 62f7676cdf044086d47ef7dc556271773383fde7 Mon Sep 17 00:00:00 2001 From: Maurits Lourens Date: Mon, 16 Aug 2021 23:31:51 +0200 Subject: [PATCH] add autocomplete component and make it optional for optionsField and relationshipField --- .../bbui/src/Form/Core/Autocomplete.svelte | 80 +++++++++++++++++++ packages/bbui/src/Form/Core/Picker.svelte | 31 ++++++- packages/bbui/src/Form/Core/Search.svelte | 3 + packages/bbui/src/Form/Core/index.js | 1 + packages/standard-components/manifest.json | 10 +++ .../src/forms/OptionsField.svelte | 12 ++- .../src/forms/RelationshipField.svelte | 10 ++- 7 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 packages/bbui/src/Form/Core/Autocomplete.svelte diff --git a/packages/bbui/src/Form/Core/Autocomplete.svelte b/packages/bbui/src/Form/Core/Autocomplete.svelte new file mode 100644 index 0000000000..ea36d82463 --- /dev/null +++ b/packages/bbui/src/Form/Core/Autocomplete.svelte @@ -0,0 +1,80 @@ + + + option === value} + onSelectOption={selectOption} +/> diff --git a/packages/bbui/src/Form/Core/Picker.svelte b/packages/bbui/src/Form/Core/Picker.svelte index 3de454668c..ce08a33c59 100644 --- a/packages/bbui/src/Form/Core/Picker.svelte +++ b/packages/bbui/src/Form/Core/Picker.svelte @@ -5,6 +5,7 @@ import { fly } from "svelte/transition" import { createEventDispatcher } from "svelte" import clickOutside from "../../Actions/click_outside" + import Search from "./Search.svelte" export let id = null export let disabled = false @@ -13,6 +14,7 @@ export let fieldIcon = "" export let isPlaceholder = false export let placeholderOption = null + export let autocompletePlaceholder = "" export let options = [] export let isOptionSelected = () => false export let onSelectOption = () => {} @@ -23,10 +25,17 @@ export let readonly = false export let quiet = false export let autoWidth = false + export let autocomplete = false const dispatch = createEventDispatcher() + let searchTerm = null $: sortedOptions = getSortedOptions(options, getOptionLabel) + $: filteredOptions = getFilteredOptions( + sortedOptions, + searchTerm, + getOptionLabel + ) const onClick = () => { dispatch("click") @@ -46,6 +55,16 @@ return labelA > labelB ? 1 : -1 }) } + + const getFilteredOptions = (options, term, getLabel) => { + if (autocomplete && term) { + const lowerCaseTerm = term.toLowerCase() + return options.filter(option => + getLabel(option).toLowerCase().includes(lowerCaseTerm) + ) + } + return options + }