Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(itemSelection): add support for items getter #27

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/controllers/itemSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ export class ItemSelectionController<T> {
* @return {T[]}
*/
public get selectedItems(): T[] {
return this.__items.filter((_item, idx) => this.__selectedIndices.has(idx));
return this.items.filter((_item, idx) => this.__selectedIndices.has(idx));
}

/**
* Gets the current set of items
* @return {T[]}
*/
public get items(): T[] {
if (this.__itemsGetter) {
return this.__itemsGetter();
}
return this.__items;
}

private __selectedIndices: Set<number> = new Set<number>();
private __items: T[] = [];
private __itemsGetter?: () => T[];
private __host: ReactiveControllerHost;
private __options?: ItemSelectionOptions<T>;

Expand All @@ -38,11 +42,15 @@ export class ItemSelectionController<T> {
*/
public constructor(
host: ReactiveControllerHost,
items: T[],
items: T[] | (() => T[]),
options?: ItemSelectionOptions<T>
) {
this.__host = host;
this.__items = items;
if (typeof items === 'function') {
this.__itemsGetter = items;
} else {
this.__items = items;
}

if (options) {
this.__options = options;
Expand Down Expand Up @@ -99,15 +107,16 @@ export class ItemSelectionController<T> {
}

const [currentSelection] = [...this.__selectedIndices];
const items = this.items;
let newIndex = 0;

if (currentSelection !== undefined) {
newIndex = currentSelection + offset;

if (newIndex < 0) {
newIndex = 0;
} else if (newIndex >= this.__items.length) {
newIndex = this.__items.length - 1;
} else if (newIndex >= items.length) {
newIndex = items.length - 1;
}
}

Expand Down Expand Up @@ -142,7 +151,9 @@ export class ItemSelectionController<T> {
throw new Error('selectAll() cannot be used when in single-select mode');
}

for (const item of this.__items) {
const items = this.items;

for (const item of items) {
this.select(item);
}
}
Expand Down Expand Up @@ -185,7 +196,9 @@ export class ItemSelectionController<T> {
throw new Error('Index cannot be below 0');
}

if (idx >= this.__items.length) {
const items = this.items;

if (idx >= items.length) {
throw new Error('Index cannot be greater than size of items array');
}

Expand Down Expand Up @@ -213,7 +226,7 @@ export class ItemSelectionController<T> {
* @return {void}
*/
public toggle(item: T, selected?: boolean): void {
const idx = this.__items.indexOf(item);
const idx = this.items.indexOf(item);

if (idx === -1) {
throw new Error('Item was not in items array');
Expand Down
14 changes: 14 additions & 0 deletions src/test/controllers/itemSelection_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,4 +408,18 @@ suite('ItemSelectionController', () => {
assert.equal(element.shadowRoot!.textContent, 'Selected: b');
});
});

suite('with items function', () => {
setup(async () => {
const itemsFn = (): string[] => items;
controller = new ItemSelectionController<string>(element, itemsFn);
element.controllers.push(controller as ReactiveController);
document.body.appendChild(element);
});

test('initialises to default selection', () => {
assert.is(controller.selectedItems.length, 0);
assert.is(controller.items, items);
});
});
});
Loading