fix: use native binding for value prop in ComboBox, MultiSelect (#1343)

* fix(combo-box): use native binding for value prop (fixes #1262)

* fix(multi-select): use native binding for value prop
This commit is contained in:
metonym 2022-06-12 07:12:11 -07:00 committed by GitHub
commit 8e52e13ee9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 25 deletions

View file

@ -121,7 +121,6 @@
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
let selectedItem = undefined; let selectedItem = undefined;
let inputValue = value;
let prevSelectedId = null; let prevSelectedId = null;
let highlightedIndex = -1; let highlightedIndex = -1;
@ -161,7 +160,7 @@
selectedId = undefined; selectedId = undefined;
selectedItem = undefined; selectedItem = undefined;
open = false; open = false;
inputValue = ""; value = "";
if (options?.focus !== false) ref?.focus(); if (options?.focus !== false) ref?.focus();
} }
@ -174,12 +173,12 @@
filteredItems = []; filteredItems = [];
if (!selectedItem) { if (!selectedItem) {
selectedId = undefined; selectedId = undefined;
inputValue = ""; value = "";
highlightedIndex = -1; highlightedIndex = -1;
highlightedId = undefined; highlightedId = undefined;
} else { } else {
// programmatically set inputValue // programmatically set value
inputValue = selectedItem.text; value = selectedItem.text;
} }
} }
}); });
@ -207,7 +206,6 @@
$: comboId = `combo-${id}`; $: comboId = `combo-${id}`;
$: highlightedId = items[highlightedIndex] ? items[highlightedIndex].id : 0; $: highlightedId = items[highlightedIndex] ? items[highlightedIndex].id : 0;
$: filteredItems = items.filter((item) => shouldFilterItem(item, value)); $: filteredItems = items.filter((item) => shouldFilterItem(item, value));
$: value = inputValue;
</script> </script>
<svelte:window <svelte:window
@ -257,6 +255,7 @@
> >
<input <input
bind:this="{ref}" bind:this="{ref}"
bind:value
tabindex="0" tabindex="0"
autocomplete="off" autocomplete="off"
aria-autocomplete="list" aria-autocomplete="list"
@ -269,19 +268,17 @@
disabled="{disabled}" disabled="{disabled}"
placeholder="{placeholder}" placeholder="{placeholder}"
id="{id}" id="{id}"
value="{inputValue}"
name="{name}" name="{name}"
{...$$restProps} {...$$restProps}
class:bx--text-input="{true}" class:bx--text-input="{true}"
class:bx--text-input--light="{light}" class:bx--text-input--light="{light}"
class:bx--text-input--empty="{inputValue === ''}" class:bx--text-input--empty="{value === ''}"
on:input="{async ({ target }) => { on:input="{({ target }) => {
if (!open && target.value.length > 0) { if (!open && target.value.length > 0) {
open = true; open = true;
} }
inputValue = target.value; if (!value.length) {
if (!inputValue.length) {
clear(); clear();
open = true; open = true;
} }
@ -297,14 +294,14 @@
) { ) {
open = false; open = false;
if (filteredItems[highlightedIndex]) { if (filteredItems[highlightedIndex]) {
inputValue = filteredItems[highlightedIndex].text; value = filteredItems[highlightedIndex].text;
selectedItem = filteredItems[highlightedIndex]; selectedItem = filteredItems[highlightedIndex];
selectedId = filteredItems[highlightedIndex].id; selectedId = filteredItems[highlightedIndex].id;
} }
} else { } else {
open = false; open = false;
if (filteredItems[0]) { if (filteredItems[0]) {
inputValue = filteredItems[0].text; value = filteredItems[0].text;
selectedItem = filteredItems[0]; selectedItem = filteredItems[0];
selectedId = filteredItems[0].id; selectedId = filteredItems[0].id;
} }
@ -344,7 +341,7 @@
class="bx--list-box__invalid-icon bx--list-box__invalid-icon--warning" class="bx--list-box__invalid-icon bx--list-box__invalid-icon--warning"
/> />
{/if} {/if}
{#if inputValue} {#if value}
<ListBoxSelection <ListBoxSelection
on:clear on:clear
on:clear="{clear}" on:clear="{clear}"
@ -385,7 +382,7 @@
open = false; open = false;
if (filteredItems[i]) { if (filteredItems[i]) {
inputValue = filteredItems[i].text; value = filteredItems[i].text;
} }
}}" }}"
on:mouseenter="{() => { on:mouseenter="{() => {

View file

@ -183,7 +183,6 @@
const dispatch = createEventDispatcher(); const dispatch = createEventDispatcher();
let inputValue = "";
let initialSorted = false; let initialSorted = false;
let highlightedIndex = -1; let highlightedIndex = -1;
let prevChecked = []; let prevChecked = [];
@ -255,7 +254,7 @@
} }
highlightedIndex = -1; highlightedIndex = -1;
inputValue = ""; value = "";
} }
items = sortedItems; items = sortedItems;
@ -275,7 +274,6 @@
highlightedIndex > -1 highlightedIndex > -1
? (filterable ? filteredItems : sortedItems)[highlightedIndex]?.id ?? null ? (filterable ? filteredItems : sortedItems)[highlightedIndex]?.id ?? null
: null; : null;
$: value = inputValue;
</script> </script>
<svelte:window <svelte:window
@ -403,6 +401,7 @@
{#if filterable} {#if filterable}
<input <input
bind:this="{inputRef}" bind:this="{inputRef}"
bind:value
{...$$restProps} {...$$restProps}
role="combobox" role="combobox"
tabindex="0" tabindex="0"
@ -413,11 +412,8 @@
aria-disabled="{disabled}" aria-disabled="{disabled}"
aria-controls="{menuId}" aria-controls="{menuId}"
class:bx--text-input="{true}" class:bx--text-input="{true}"
class:bx--text-input--empty="{inputValue === ''}" class:bx--text-input--empty="{value === ''}"
class:bx--text-input--light="{light}" class:bx--text-input--light="{light}"
on:input="{({ target }) => {
inputValue = target.value;
}}"
on:keydown on:keydown
on:keydown|stopPropagation="{({ key }) => { on:keydown|stopPropagation="{({ key }) => {
if (key === 'Enter') { if (key === 'Enter') {
@ -451,15 +447,14 @@
placeholder="{placeholder}" placeholder="{placeholder}"
id="{id}" id="{id}"
name="{name}" name="{name}"
value="{inputValue}"
/> />
{#if invalid} {#if invalid}
<WarningFilled class="bx--list-box__invalid-icon" /> <WarningFilled class="bx--list-box__invalid-icon" />
{/if} {/if}
{#if inputValue} {#if value}
<ListBoxSelection <ListBoxSelection
on:clear="{() => { on:clear="{() => {
inputValue = ''; value = '';
open = false; open = false;
}}" }}"
translateWithId="{translateWithIdSelection}" translateWithId="{translateWithIdSelection}"