Skip to content

Commit

Permalink
#8129 - allow search in subitems
Browse files Browse the repository at this point in the history
  • Loading branch information
novikov82 committed May 3, 2024
1 parent fbb0142 commit 12973f4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/actions/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ export class Action extends BaseAction implements IAction, ILocalizableOwner {
public setItems(items: Array<IAction>, onSelectionChanged: (item: Action, ...params: any[]) => void): void {
this.markerIconName = "icon-next_16x16";
this.component = "sv-list-item-group";
this.items = [...items];
const { innerPopupModel, listModel }: { innerPopupModel: PopupModel<any>, listModel: ListModel<Action> } =
createPopupModelWithListModel(
{ items: items, onSelectionChanged: onSelectionChanged },
Expand Down
15 changes: 15 additions & 0 deletions src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ export class ListModel<T extends BaseAction = Action> extends ActionContainer<T>
public isItemVisible(item: T): boolean {
return item.visible && (!this.shouldProcessFilter || this.hasText(item, this.filterString));
}

protected getRenderedActions(): Array<T> {
let actions = super.getRenderedActions();

if (this.filterString) {
let newActions = [] as Array<T>;
actions.forEach(action => {
newActions.push(action);
if (action.items) action.items.forEach(item => newActions.push(item as T));
});
return newActions;
}

return actions;
}
public get visibleItems(): Array<T> {
return this.visibleActions.filter(item => this.isItemVisible(item));
}
Expand Down
34 changes: 34 additions & 0 deletions tests/listModelTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,37 @@ QUnit.test("ListModel filter & comparator.normalize text (brouillé=brouille)",
assert.equal(filteredActions.length, 2, "include brouillé");
settings.comparator.normalizeTextCallback = (str: string, reason: string): string => { return str; };
});
QUnit.test("ListModel search in subitems", function (assert) {
ListModel.MINELEMENTCOUNT = 5;

const items: Array<IAction> = [];
for (let index = 0; index < 7; ++index) {
items.push(new Action({ id: "test" + index, title: "test" + index }));
}

const subitems = [new Action({ id: "test28", title: "test28" }), new Action({ id: "test29", title: "test29" })];
(items[2] as Action).setItems(subitems, () => { });
const list = new ListModel(items, () => { }, true);
let filteredActions;
filteredActions = list.renderedActions.filter(item => list.isItemVisible(item));
assert.equal(filteredActions.length, 7);
list.filterString = "t";
filteredActions = list.renderedActions.filter(item => list.isItemVisible(item));
assert.equal(filteredActions.length, 9);

list.filterString = "2";
filteredActions = list.renderedActions.filter(item => list.isItemVisible(item));
assert.equal(filteredActions.length, 3);
assert.deepEqual(filteredActions.map(a => a.title), ["test2", "test28", "test29"]);

list.filterString = "1";
filteredActions = list.renderedActions.filter(item => list.isItemVisible(item));
assert.equal(filteredActions.length, 1);
assert.deepEqual(filteredActions.map(a => a.title), ["test1"]);
list.filterString = "28";
filteredActions = list.renderedActions.filter(item => list.isItemVisible(item));
assert.equal(filteredActions.length, 1);
assert.deepEqual(filteredActions.map(a => a.title), ["test28"]);

ListModel.MINELEMENTCOUNT = oldValueMINELEMENTCOUNT;
});

0 comments on commit 12973f4

Please sign in to comment.