itemSelection
allows you to navigate around an array of items (via methods
like next()
and prev()
), and react to the selection having changed.
class MyElement extends LitElement {
@state()
public items: string[] = ['one', 'two', 'three'];
constructor() {
super();
this._itemSelectionCtrl = new ItemSelectionController(
this,
this.items
);
}
render() {
const {selectedItems} = this._itemSelectionCtrl;
return html`
Selected strings are: ${selectedItems}.
<button
@click=${() => this._itemSelectionCtrl.next()}>
Next item
</button>
<button
@click=${() => this._itemSelectionCtrl.prev()}>
Previous item
</button>
`;
}
}
In this example, we would initially render nothing (no selection) and would use single-selection mode (the default).
On clicking Next item
, we would select the first item ("one"
).
If we continued to click Next item
, we would select the second item ("two"
).
This controller accepts an optional options
parameter, which is an object
used to configure various features.
If you set this to true
, multiple items may be selected.
For example, you may call selectByIndex(0)
followed by selectByIndex(1)
and
both will remain marked as selected.
This also means you can no longer use the following methods (as they will only function with a single selection):
next()
prev()
selectByOffset()
You can specify the item indices to select by default using this option.
For example:
const items = ['a', 'b', 'c'];
const ctrl = new ItemSelectionController(
this,
items,
{ defaultSelectedIndices: [1] }
);
// ctrl will default to `b` being selected as it is at index 1.
You can specify the items to select by default using this option.
For example:
const items = ['a', 'b', 'c'];
const ctrl = new ItemSelectionController(
this,
items,
{ defaultSelection: ['b'] }
);
// ctrl will default to `b` being selected
In single-selection mode, moves the selection to the next item.
If there is no next item, it will remain at the last item.
In single-selection mode, moves the selection to the previous item.
If there is no previous item, it will remain at the first item.
In single-selection mode, moves the selection by the specified offset.
For example, selectByOffset(-2)
will select the item positioned two places
before the current selection.
If there is no item at such an offset, the item at the boundary will be selected (the first or last item, depending on if you moved positively or negatively).
Selects the item at the given index.
Selects the given item.
Deselects all items.
Selects all items.
Deselects the given item.
Deselects the item at the given index.
Toggles the item at the given index.
For example, if the item at 2
is selected, toggleByIndex(2)
will deselect
it.
You can optionally also specify the selection state to force a select or
deselect (e.g. toggleByIndex(2, true)
to force 2
to be selected).
Toggles the given item.
As with toggleByIndex
, you can optionally specify the selection state to
force a select or a deselect.