mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
fetch upstream
This commit is contained in:
parent
397c53b48e
commit
298025c30f
8 changed files with 645 additions and 0 deletions
53
src/components/ComboBox/ComboBox.Story.svelte
Normal file
53
src/components/ComboBox/ComboBox.Story.svelte
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
<script>
|
||||||
|
import Layout from '../../internal/ui/Layout.svelte';
|
||||||
|
import ToggleSmall from '../ToggleSmall';
|
||||||
|
import Button from '../Button';
|
||||||
|
import ComboBox from './ComboBox.svelte';
|
||||||
|
|
||||||
|
let items = [
|
||||||
|
{ id: 'option-0', text: 'Option 1' },
|
||||||
|
{ id: 'option-1', text: 'Option 2' },
|
||||||
|
{ id: 'option-2', text: 'Option 3' },
|
||||||
|
{ id: 'option-3', text: 'Option 4' },
|
||||||
|
{
|
||||||
|
id: 'option-4',
|
||||||
|
text: 'An example option that is really long to show what should be done to handle long text'
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
let toggled = false;
|
||||||
|
let value = undefined;
|
||||||
|
let selectedIndex = -1;
|
||||||
|
|
||||||
|
function shouldFilterItem(item, value) {
|
||||||
|
if (!toggled || !value) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return item.text.toLowerCase().includes(value.toLowerCase());
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Layout>
|
||||||
|
<p>Currently, this component does not support items as slots.</p>
|
||||||
|
<p>
|
||||||
|
<code>items</code>
|
||||||
|
must be an array of objects; mandatory fields are `id` and `text`.
|
||||||
|
</p>
|
||||||
|
<pre style="margin-top: 1rem;">
|
||||||
|
<code>{'items = Array<{ id: string; text: string; }>'}</code>
|
||||||
|
</pre>
|
||||||
|
<div style="margin-top: 2rem;">
|
||||||
|
<ToggleSmall labelText="Enable filtering" bind:toggled />
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
on:click={() => {
|
||||||
|
selectedIndex = 1;
|
||||||
|
}}>
|
||||||
|
Set item to 'Option 2'
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div style="width: 300px; margin-top: 2rem;">
|
||||||
|
<ComboBox {...$$props} id="combobox" bind:value bind:selectedIndex {items} {shouldFilterItem} />
|
||||||
|
</div>
|
||||||
|
</Layout>
|
24
src/components/ComboBox/ComboBox.stories.js
Normal file
24
src/components/ComboBox/ComboBox.stories.js
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import { withKnobs, select, boolean, text } from '@storybook/addon-knobs';
|
||||||
|
import Component from './ComboBox.Story.svelte';
|
||||||
|
|
||||||
|
export default { title: 'ComboBox', decorators: [withKnobs] };
|
||||||
|
|
||||||
|
const sizes = {
|
||||||
|
'Extra large size (xl)': 'xl',
|
||||||
|
'Regular size (lg)': '',
|
||||||
|
'Small size (sm)': 'sm'
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Default = () => ({
|
||||||
|
Component,
|
||||||
|
props: {
|
||||||
|
size: select('Field size (size)', sizes, ''),
|
||||||
|
placeholder: text('Placeholder text (placeholder)', 'Filter...'),
|
||||||
|
titleText: text('Title (titleText)', 'Combobox title'),
|
||||||
|
helperText: text('Helper text (helperText)', 'Optional helper text here'),
|
||||||
|
light: boolean('Light (light)', false),
|
||||||
|
disabled: boolean('Disabled (disabled)', false),
|
||||||
|
invalid: boolean('Invalid (invalid)', false),
|
||||||
|
invalidText: text('Invalid text (invalidText)', 'A valid value is required')
|
||||||
|
}
|
||||||
|
});
|
188
src/components/ComboBox/ComboBox.svelte
Normal file
188
src/components/ComboBox/ComboBox.svelte
Normal file
|
@ -0,0 +1,188 @@
|
||||||
|
<script>
|
||||||
|
let className = undefined;
|
||||||
|
export { className as class };
|
||||||
|
export let disabled = false;
|
||||||
|
export let helperText = '';
|
||||||
|
export let id = Math.random();
|
||||||
|
export let invalid = false;
|
||||||
|
export let invalidText = '';
|
||||||
|
export let items = [];
|
||||||
|
export let itemToString = item => item.text || item.id;
|
||||||
|
export let light = false;
|
||||||
|
export let open = false;
|
||||||
|
export let placeholder = '';
|
||||||
|
export let selectedIndex = -1;
|
||||||
|
export let shouldFilterItem = () => true;
|
||||||
|
export let size = undefined;
|
||||||
|
export let style = undefined;
|
||||||
|
export let titleText = '';
|
||||||
|
export let translateWithId = undefined;
|
||||||
|
export let value = '';
|
||||||
|
|
||||||
|
import { afterUpdate } from 'svelte';
|
||||||
|
import WarningFilled16 from 'carbon-icons-svelte/lib/WarningFilled16';
|
||||||
|
import { cx } from '../../lib';
|
||||||
|
import ListBox, {
|
||||||
|
ListBoxField,
|
||||||
|
ListBoxMenu,
|
||||||
|
ListBoxMenuIcon,
|
||||||
|
ListBoxMenuItem,
|
||||||
|
ListBoxSelection
|
||||||
|
} from '../ListBox';
|
||||||
|
|
||||||
|
let selectedId = undefined;
|
||||||
|
let inputRef = undefined;
|
||||||
|
let inputValue = '';
|
||||||
|
let highlightedIndex = -1;
|
||||||
|
|
||||||
|
function change(direction) {
|
||||||
|
let index = highlightedIndex + direction;
|
||||||
|
|
||||||
|
if (index < 0) {
|
||||||
|
index = items.length - 1;
|
||||||
|
} else if (index >= items.length) {
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
highlightedIndex = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
afterUpdate(() => {
|
||||||
|
if (open) {
|
||||||
|
inputRef.focus();
|
||||||
|
filteredItems = items.filter(item => shouldFilterItem(item, value));
|
||||||
|
} else {
|
||||||
|
highlightedIndex = -1;
|
||||||
|
inputValue = selectedItem ? selectedItem.text : '';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
$: ariaLabel = $$props['aria-label'] || 'Choose an item';
|
||||||
|
$: menuId = `menu-${id}`;
|
||||||
|
$: comboId = `combo-${id}`;
|
||||||
|
$: highlightedId = items[highlightedIndex] ? items[highlightedIndex].id : undefined;
|
||||||
|
$: filteredItems = items.filter(item => shouldFilterItem(item, value));
|
||||||
|
$: selectedItem = items[selectedIndex];
|
||||||
|
$: inputValue = selectedItem ? selectedItem.text : undefined;
|
||||||
|
$: value = inputValue;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<svelte:body
|
||||||
|
on:click={({ target }) => {
|
||||||
|
if (open && inputRef && !inputRef.contains(target)) {
|
||||||
|
open = false;
|
||||||
|
}
|
||||||
|
}} />
|
||||||
|
|
||||||
|
<div class={cx('--list-box__wrapper', className)} {style}>
|
||||||
|
{#if titleText}
|
||||||
|
<label class={cx('--label', disabled && '--label--disabled')} for={id}>{titleText}</label>
|
||||||
|
{/if}
|
||||||
|
{#if helperText}
|
||||||
|
<div class={cx('--form__helper-text', disabled && '--form__helper-text--disabled')}>
|
||||||
|
{helperText}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
<ListBox
|
||||||
|
class={cx('--combo-box')}
|
||||||
|
id={comboId}
|
||||||
|
aria-label={ariaLabel}
|
||||||
|
{disabled}
|
||||||
|
{invalid}
|
||||||
|
{invalidText}
|
||||||
|
{open}
|
||||||
|
{light}
|
||||||
|
{size}>
|
||||||
|
<ListBoxField
|
||||||
|
role="button"
|
||||||
|
aria-expanded={open}
|
||||||
|
on:click={() => {
|
||||||
|
open = true;
|
||||||
|
}}
|
||||||
|
{id}
|
||||||
|
{disabled}
|
||||||
|
{translateWithId}>
|
||||||
|
<input
|
||||||
|
bind:this={inputRef}
|
||||||
|
tabindex="0"
|
||||||
|
autocomplete="off"
|
||||||
|
aria-autocomplete="list"
|
||||||
|
aria-expanded={open}
|
||||||
|
aria-activedescendant={highlightedId}
|
||||||
|
aria-labelledby={comboId}
|
||||||
|
aria-disabled={disabled}
|
||||||
|
aria-controls={open ? menuId : undefined}
|
||||||
|
aria-owns={open ? menuId : undefined}
|
||||||
|
class={cx('--text-input', inputValue === '' && '--text-input--empty')}
|
||||||
|
on:input={({ target }) => {
|
||||||
|
inputValue = target.value;
|
||||||
|
}}
|
||||||
|
on:keydown
|
||||||
|
on:keydown|stopPropagation={({ key }) => {
|
||||||
|
if (key === 'Enter') {
|
||||||
|
open = !open;
|
||||||
|
if (highlightedIndex > -1 && highlightedIndex !== selectedIndex) {
|
||||||
|
selectedIndex = highlightedIndex;
|
||||||
|
open = false;
|
||||||
|
}
|
||||||
|
} else if (key === 'Tab') {
|
||||||
|
open = false;
|
||||||
|
} else if (key === 'ArrowDown') {
|
||||||
|
change(1);
|
||||||
|
} else if (key === 'ArrowUp') {
|
||||||
|
change(-1);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
on:focus
|
||||||
|
on:blur
|
||||||
|
on:blur={({ relatedTarget }) => {
|
||||||
|
if (relatedTarget && relatedTarget.getAttribute('role') !== 'button') {
|
||||||
|
inputRef.focus();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
{disabled}
|
||||||
|
{placeholder}
|
||||||
|
{id}
|
||||||
|
value={inputValue} />
|
||||||
|
{#if invalid}
|
||||||
|
<WarningFilled16 class={cx('--list-box__invalid-icon')} />
|
||||||
|
{/if}
|
||||||
|
{#if inputValue}
|
||||||
|
<ListBoxSelection
|
||||||
|
on:clear={() => {
|
||||||
|
selectedIndex = -1;
|
||||||
|
open = false;
|
||||||
|
}}
|
||||||
|
{translateWithId}
|
||||||
|
{disabled}
|
||||||
|
{open} />
|
||||||
|
{/if}
|
||||||
|
<ListBoxMenuIcon
|
||||||
|
on:click={() => {
|
||||||
|
open = !open;
|
||||||
|
}}
|
||||||
|
{translateWithId}
|
||||||
|
{open} />
|
||||||
|
</ListBoxField>
|
||||||
|
{#if open}
|
||||||
|
<ListBoxMenu aria-label={ariaLabel} {id}>
|
||||||
|
{#each filteredItems as item, i (item.id || i)}
|
||||||
|
<ListBoxMenuItem
|
||||||
|
id={item.id}
|
||||||
|
active={selectedIndex === i || selectedId === item.id}
|
||||||
|
highlighted={highlightedIndex === i || selectedIndex === i}
|
||||||
|
on:click={() => {
|
||||||
|
selectedId = item.id;
|
||||||
|
selectedIndex = items.map(({ id }) => id).indexOf(filteredItems[i].id);
|
||||||
|
open = false;
|
||||||
|
}}
|
||||||
|
on:mouseenter={() => {
|
||||||
|
highlightedIndex = i;
|
||||||
|
}}>
|
||||||
|
{itemToString(item)}
|
||||||
|
</ListBoxMenuItem>
|
||||||
|
{/each}
|
||||||
|
</ListBoxMenu>
|
||||||
|
{/if}
|
||||||
|
</ListBox>
|
||||||
|
</div>
|
3
src/components/ComboBox/index.js
Normal file
3
src/components/ComboBox/index.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import ComboBox from './ComboBox.svelte';
|
||||||
|
|
||||||
|
export default ComboBox;
|
41
src/components/MultiSelect/MultiSelect.Story.svelte
Normal file
41
src/components/MultiSelect/MultiSelect.Story.svelte
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
<script>
|
||||||
|
import Layout from '../../internal/ui/Layout.svelte';
|
||||||
|
import Button from '../Button';
|
||||||
|
import MultiSelect from './MultiSelect.svelte';
|
||||||
|
|
||||||
|
let value = '';
|
||||||
|
|
||||||
|
let items = [
|
||||||
|
{ id: 'option-0', text: 'Option 1' },
|
||||||
|
{ id: 'option-1', text: 'Option 2' },
|
||||||
|
{ id: 'option-2', text: 'Option 3' },
|
||||||
|
{ id: 'option-3', text: 'Option 4' },
|
||||||
|
{
|
||||||
|
id: 'option-4',
|
||||||
|
text: 'An example option that is really long to show what should be done to handle long text'
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
let selectedIds = [];
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<Layout>
|
||||||
|
<div>
|
||||||
|
<Button
|
||||||
|
size="small"
|
||||||
|
on:click={() => {
|
||||||
|
selectedIds = selectedIds.length > 0 ? [] : [items[1].id, items[2].id];
|
||||||
|
}}>
|
||||||
|
{selectedIds.length > 0 ? 'Clear' : 'Set initial'} selected items
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
<div style="width: 300px; margin-top: 2rem;">
|
||||||
|
<MultiSelect
|
||||||
|
{...$$props}
|
||||||
|
id="multiselect"
|
||||||
|
placeholder="Filter..."
|
||||||
|
bind:selectedIds
|
||||||
|
bind:items
|
||||||
|
bind:value />
|
||||||
|
</div>
|
||||||
|
</Layout>
|
38
src/components/MultiSelect/MultiSelect.stories.js
Normal file
38
src/components/MultiSelect/MultiSelect.stories.js
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import { withKnobs, select, boolean, text } from '@storybook/addon-knobs';
|
||||||
|
import Component from './MultiSelect.Story.svelte';
|
||||||
|
|
||||||
|
export default { title: 'MultiSelect', decorators: [withKnobs] };
|
||||||
|
|
||||||
|
const types = {
|
||||||
|
'Default (default)': 'default',
|
||||||
|
'Inline (inline)': 'inline'
|
||||||
|
};
|
||||||
|
|
||||||
|
const sizes = {
|
||||||
|
'Extra large size (xl)': 'xl',
|
||||||
|
'Regular size (lg)': '',
|
||||||
|
'Small size (sm)': 'sm'
|
||||||
|
};
|
||||||
|
|
||||||
|
export const Default = () => ({
|
||||||
|
Component,
|
||||||
|
props: {
|
||||||
|
id: 'multiselect',
|
||||||
|
titleText: text('Title (titleText)', 'Multiselect Title'),
|
||||||
|
helperText: text('Helper text (helperText)', 'This is not helper text'),
|
||||||
|
filterable: boolean('Filterable (filterable)', false),
|
||||||
|
selectionFeedback: select(
|
||||||
|
'Selection feedback (selectionFeedback)',
|
||||||
|
['top', 'fixed', 'top-after-reopen'],
|
||||||
|
'top-after-reopen'
|
||||||
|
),
|
||||||
|
disabled: boolean('Disabled (disabled)', false),
|
||||||
|
light: boolean('Light variant (light)', false),
|
||||||
|
useTitleInItem: boolean('Show tooltip on hover', false),
|
||||||
|
type: select('UI type (Only for `<MultiSelect>`) (type)', types, 'default'),
|
||||||
|
size: select('Field size (size)', sizes, '') || undefined,
|
||||||
|
label: text('Label (label)', 'MultiSelect Label'),
|
||||||
|
invalid: boolean('Show form validation UI (invalid)', false),
|
||||||
|
invalidText: text('Form validation UI content (invalidText)', 'Invalid Selection')
|
||||||
|
}
|
||||||
|
});
|
295
src/components/MultiSelect/MultiSelect.svelte
Normal file
295
src/components/MultiSelect/MultiSelect.svelte
Normal file
|
@ -0,0 +1,295 @@
|
||||||
|
<script>
|
||||||
|
let className = undefined;
|
||||||
|
export { className as class };
|
||||||
|
export let disabled = false;
|
||||||
|
export let filterable = false;
|
||||||
|
export let filterItem = (item, value) => item.text.toLowerCase().includes(value.toLowerCase());
|
||||||
|
export let helperText = '';
|
||||||
|
export let id = Math.random();
|
||||||
|
export let invalid = false;
|
||||||
|
export let invalidText = '';
|
||||||
|
export let items = [];
|
||||||
|
export let itemToString = item => item.text || item.id;
|
||||||
|
export let label = '';
|
||||||
|
export let light = false;
|
||||||
|
export let locale = 'en';
|
||||||
|
export let open = false;
|
||||||
|
export let placeholder = '';
|
||||||
|
export let selectedIds = [];
|
||||||
|
export let selectionFeedback = 'top-after-reopen';
|
||||||
|
export let size = undefined;
|
||||||
|
export let sortItem = (a, b) => a.text.localeCompare(b.text, locale, { numeric: true });
|
||||||
|
export let style = undefined;
|
||||||
|
export let titleText = '';
|
||||||
|
export let translateWithId = undefined;
|
||||||
|
export let type = 'default';
|
||||||
|
export let useTitleInItem = false;
|
||||||
|
export let value = '';
|
||||||
|
|
||||||
|
import { afterUpdate, setContext } from 'svelte';
|
||||||
|
import WarningFilled16 from 'carbon-icons-svelte/lib/WarningFilled16';
|
||||||
|
import { cx } from '../../lib';
|
||||||
|
import Checkbox from '../Checkbox';
|
||||||
|
import ListBox, {
|
||||||
|
ListBoxField,
|
||||||
|
ListBoxMenu,
|
||||||
|
ListBoxMenuIcon,
|
||||||
|
ListBoxMenuItem,
|
||||||
|
ListBoxSelection
|
||||||
|
} from '../ListBox';
|
||||||
|
|
||||||
|
let multiSelectRef = undefined;
|
||||||
|
let fieldRef = undefined;
|
||||||
|
let selectionRef = undefined;
|
||||||
|
let inputRef = undefined;
|
||||||
|
let inputValue = '';
|
||||||
|
let initialSorted = false;
|
||||||
|
let highlightedIndex = -1;
|
||||||
|
let prevChecked = [];
|
||||||
|
|
||||||
|
setContext('MultiSelect', {
|
||||||
|
declareRef: ({ key, ref }) => {
|
||||||
|
switch (key) {
|
||||||
|
case 'field':
|
||||||
|
fieldRef = ref;
|
||||||
|
break;
|
||||||
|
case 'selection':
|
||||||
|
selectionRef = ref;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
function change(direction) {
|
||||||
|
let index = highlightedIndex + direction;
|
||||||
|
|
||||||
|
if (index < 0) {
|
||||||
|
index = items.length - 1;
|
||||||
|
} else if (index >= items.length) {
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
highlightedIndex = index;
|
||||||
|
}
|
||||||
|
|
||||||
|
function sort() {
|
||||||
|
return [
|
||||||
|
...(checked.length > 1 ? checked.sort(sortItem) : checked),
|
||||||
|
...unchecked.sort(sortItem)
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
afterUpdate(() => {
|
||||||
|
if (checked.length !== prevChecked.length) {
|
||||||
|
if (selectionFeedback === 'top') {
|
||||||
|
sortedItems = sort();
|
||||||
|
}
|
||||||
|
prevChecked = checked;
|
||||||
|
selectedIds = checked.map(({ id }) => id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!open) {
|
||||||
|
if (!initialSorted || selectionFeedback !== 'fixed') {
|
||||||
|
sortedItems = sort();
|
||||||
|
initialSorted = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
highlightedIndex = -1;
|
||||||
|
inputValue = '';
|
||||||
|
}
|
||||||
|
|
||||||
|
items = sortedItems;
|
||||||
|
});
|
||||||
|
|
||||||
|
$: menuId = `menu-${id}`;
|
||||||
|
$: inline = type === 'inline';
|
||||||
|
$: ariaLabel = $$props['aria-label'] || 'Choose an item';
|
||||||
|
$: sortedItems = items.map(item => ({ ...item, checked: selectedIds.includes(item.id) }));
|
||||||
|
$: checked = sortedItems.filter(({ checked }) => checked);
|
||||||
|
$: unchecked = sortedItems.filter(({ checked }) => !checked);
|
||||||
|
$: filteredItems = sortedItems.filter(item => filterItem(item, value));
|
||||||
|
$: highlightedId = sortedItems[highlightedIndex] ? sortedItems[highlightedIndex].id : undefined;
|
||||||
|
$: value = inputValue;
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<svelte:body
|
||||||
|
on:click={({ target }) => {
|
||||||
|
if (open && multiSelectRef && !multiSelectRef.contains(target)) {
|
||||||
|
open = false;
|
||||||
|
}
|
||||||
|
}} />
|
||||||
|
|
||||||
|
<div
|
||||||
|
bind:this={multiSelectRef}
|
||||||
|
class={cx('--multi-select__wrapper', '--list-box__wrapper', inline && '--multi-select__wrapper--inline', inline && '--list-box__wrapper--inline', inline && invalid && '--multi-select__wrapper--inline--invalid', inline && invalid && '--list-box__wrapper--inline--invalid', className)}
|
||||||
|
{style}>
|
||||||
|
{#if titleText}
|
||||||
|
<label class={cx('--label', disabled && '--label--disabled')} for={id}>{titleText}</label>
|
||||||
|
{/if}
|
||||||
|
{#if !inline && helperText}
|
||||||
|
<div class={cx('--form__helper-text', disabled && '--form__helper-text--disabled')}>
|
||||||
|
{helperText}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
<ListBox
|
||||||
|
aria-label={ariaLabel}
|
||||||
|
class={cx('--multi-select', filterable && '--combo-box', filterable && '--multi-select--filterable', invalid && '--multi-select--invalid', inline && '--multi-select--inline', checked.length > 0 && '--multi-select--selected')}
|
||||||
|
{id}
|
||||||
|
{disabled}
|
||||||
|
{invalid}
|
||||||
|
{invalidText}
|
||||||
|
{open}
|
||||||
|
{light}
|
||||||
|
{size}>
|
||||||
|
{#if invalid}
|
||||||
|
<WarningFilled16 class={cx('--list-box__invalid-icon')} />
|
||||||
|
{/if}
|
||||||
|
<ListBoxField
|
||||||
|
role="button"
|
||||||
|
tabindex="0"
|
||||||
|
aria-expanded={open}
|
||||||
|
on:click={() => {
|
||||||
|
if (filterable) {
|
||||||
|
open = true;
|
||||||
|
inputRef.focus();
|
||||||
|
} else {
|
||||||
|
open = !open;
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
on:keydown={({ key }) => {
|
||||||
|
if (filterable) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (key === ' ') {
|
||||||
|
open = !open;
|
||||||
|
} else if (key === 'Tab') {
|
||||||
|
if (selectionRef && checked.length > 0) {
|
||||||
|
selectionRef.focus();
|
||||||
|
} else {
|
||||||
|
open = false;
|
||||||
|
fieldRef.blur();
|
||||||
|
}
|
||||||
|
} else if (key === 'ArrowDown') {
|
||||||
|
change(1);
|
||||||
|
} else if (key === 'ArrowUp') {
|
||||||
|
change(-1);
|
||||||
|
} else if (key === 'Enter') {
|
||||||
|
if (highlightedIndex > -1) {
|
||||||
|
sortedItems[highlightedIndex].checked = !sortedItems[highlightedIndex].checked;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
on:blur={({ relatedTarget }) => {
|
||||||
|
if (relatedTarget && relatedTarget.getAttribute('role') !== 'button') {
|
||||||
|
fieldRef.focus();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
{id}
|
||||||
|
{disabled}
|
||||||
|
{translateWithId}>
|
||||||
|
{#if checked.length > 0}
|
||||||
|
<ListBoxSelection
|
||||||
|
selectionCount={checked.length}
|
||||||
|
on:clear={() => {
|
||||||
|
sortedItems = sortedItems.map(item => ({ ...item, checked: false }));
|
||||||
|
fieldRef.blur();
|
||||||
|
}}
|
||||||
|
{translateWithId}
|
||||||
|
{disabled} />
|
||||||
|
{/if}
|
||||||
|
{#if filterable}
|
||||||
|
<input
|
||||||
|
bind:this={inputRef}
|
||||||
|
role="combobox"
|
||||||
|
tabindex="0"
|
||||||
|
autocomplete="off"
|
||||||
|
aria-autocomplete="list"
|
||||||
|
aria-expanded={open}
|
||||||
|
aria-activedescendant={highlightedId}
|
||||||
|
aria-disabled={disabled}
|
||||||
|
aria-controls={menuId}
|
||||||
|
class={cx('--text-input', inputValue === '' && '--text-input--empty')}
|
||||||
|
on:input={({ target }) => {
|
||||||
|
inputValue = target.value;
|
||||||
|
}}
|
||||||
|
on:keydown
|
||||||
|
on:keydown|stopPropagation={({ key }) => {
|
||||||
|
if (key === 'Enter') {
|
||||||
|
if (highlightedIndex > -1) {
|
||||||
|
sortedItems[highlightedIndex].checked = !sortedItems[highlightedIndex].checked;
|
||||||
|
}
|
||||||
|
} else if (key === 'Tab') {
|
||||||
|
open = false;
|
||||||
|
} else if (key === 'ArrowDown') {
|
||||||
|
change(1);
|
||||||
|
} else if (key === 'ArrowUp') {
|
||||||
|
change(-1);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
on:focus
|
||||||
|
on:blur
|
||||||
|
on:blur={({ relatedTarget }) => {
|
||||||
|
if (relatedTarget && relatedTarget.getAttribute('role') !== 'button') {
|
||||||
|
inputRef.focus();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
{disabled}
|
||||||
|
{placeholder}
|
||||||
|
{id}
|
||||||
|
value={inputValue} />
|
||||||
|
{#if invalid}
|
||||||
|
<WarningFilled16 class={cx('--list-box__invalid-icon')} />
|
||||||
|
{/if}
|
||||||
|
{#if inputValue}
|
||||||
|
<ListBoxSelection
|
||||||
|
on:clear={() => {
|
||||||
|
inputValue = '';
|
||||||
|
open = false;
|
||||||
|
}}
|
||||||
|
{translateWithId}
|
||||||
|
{disabled}
|
||||||
|
{open} />
|
||||||
|
{/if}
|
||||||
|
<ListBoxMenuIcon
|
||||||
|
on:click={() => {
|
||||||
|
open = !open;
|
||||||
|
}}
|
||||||
|
{translateWithId}
|
||||||
|
{open} />
|
||||||
|
{/if}
|
||||||
|
{#if !filterable}
|
||||||
|
<span class={cx('--list-box__label')}>{label}</span>
|
||||||
|
<ListBoxMenuIcon {open} {translateWithId} />
|
||||||
|
{/if}
|
||||||
|
</ListBoxField>
|
||||||
|
{#if open}
|
||||||
|
<ListBoxMenu aria-label={ariaLabel} {id}>
|
||||||
|
{#each filterable ? filteredItems : sortedItems as item, i (item.id || i)}
|
||||||
|
<ListBoxMenuItem
|
||||||
|
id={item.id}
|
||||||
|
active={item.checked}
|
||||||
|
highlighted={highlightedIndex === i}
|
||||||
|
on:click={() => {
|
||||||
|
sortedItems = sortedItems.map(_ =>
|
||||||
|
_.id === item.id ? { ..._, checked: !_.checked } : _
|
||||||
|
);
|
||||||
|
fieldRef.focus();
|
||||||
|
}}
|
||||||
|
on:mouseenter={() => {
|
||||||
|
highlightedIndex = i;
|
||||||
|
}}>
|
||||||
|
<Checkbox
|
||||||
|
readonly
|
||||||
|
tabindex="-1"
|
||||||
|
id={`checkbox-${item.id}`}
|
||||||
|
title={useTitleInItem ? itemToString(item) : undefined}
|
||||||
|
name={itemToString(item)}
|
||||||
|
labelText={itemToString(item)}
|
||||||
|
checked={item.checked}
|
||||||
|
{disabled} />
|
||||||
|
</ListBoxMenuItem>
|
||||||
|
{/each}
|
||||||
|
</ListBoxMenu>
|
||||||
|
{/if}
|
||||||
|
</ListBox>
|
||||||
|
</div>
|
3
src/components/MultiSelect/index.js
Normal file
3
src/components/MultiSelect/index.js
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import MultiSelect from './MultiSelect.svelte';
|
||||||
|
|
||||||
|
export default MultiSelect;
|
Loading…
Add table
Add a link
Reference in a new issue