fix(combobox): value should call itemToString with filtered item (#1411)

Fixes #1405

When using the `shouldFilterItem` prop, the `ComboBox` does not display the custom label set with `itemToString` in the input after a selection.
This commit is contained in:
Johannes 2022-07-23 15:15:11 +02:00 committed by GitHub
commit 8bd615b250
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 4 deletions

View file

@ -54,6 +54,12 @@ Specify `focus: false` in the method options to avoid re-focusing the input.
<FileSource src="/framed/ComboBox/FilterableComboBox" /> <FileSource src="/framed/ComboBox/FilterableComboBox" />
### Filterable with custom label
Combine a custom label `itemToString` with the filterable option (e.g., internationalization).
<FileSource src="/framed/ComboBox/FilterableComboBoxCustomLabel" />
### Top direction ### Top direction
Set `direction` to `"top"` for the combobox dropdown menu to appear above the input. Set `direction` to `"top"` for the combobox dropdown menu to appear above the input.

View file

@ -0,0 +1,34 @@
<script>
import { ComboBox } from "carbon-components-svelte";
const translation = {
Slack: 'Custom label for "Slack"',
Email: 'Custom label for "Email"',
Fax: 'Custom label for "Fax"',
};
function itemToString(item) {
return translation[item.key];
}
function shouldFilterItem(item, value) {
if (!value) return true;
const comparison = value.toLowerCase();
return (
item.key.toLowerCase().includes(comparison) ||
itemToString(item).toLowerCase().includes(comparison)
);
}
</script>
<ComboBox
titleText="Contact"
placeholder="Select contact method"
items="{[
{ id: '0', key: 'Slack' },
{ id: '1', key: 'Email' },
{ id: '2', key: 'Fax' },
]}"
shouldFilterItem="{shouldFilterItem}"
itemToString="{itemToString}"
/>

View file

@ -178,7 +178,7 @@
highlightedId = undefined; highlightedId = undefined;
} else { } else {
// programmatically set value // programmatically set value
value = selectedItem.text; value = itemToString(selectedItem);
} }
} }
}); });
@ -294,14 +294,14 @@
) { ) {
open = false; open = false;
if (filteredItems[highlightedIndex]) { if (filteredItems[highlightedIndex]) {
value = filteredItems[highlightedIndex].text; value = itemToString(filteredItems[highlightedIndex]);
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]) {
value = filteredItems[0].text; value = itemToString(filteredItems[0]);
selectedItem = filteredItems[0]; selectedItem = filteredItems[0];
selectedId = filteredItems[0].id; selectedId = filteredItems[0].id;
} }
@ -382,7 +382,7 @@
open = false; open = false;
if (filteredItems[i]) { if (filteredItems[i]) {
value = filteredItems[i].text; value = itemToString(filteredItems[i]);
} }
}}" }}"
on:mouseenter="{() => { on:mouseenter="{() => {