Skip to content

Commit d1e0480

Browse files
anabyeCSimoesJr
authored andcommitted
feat(menu): adiciona propriedade p-search-tree-items
O componente po-menu não possui uma propriedade de busca que retorne por itens agrupadores do menu. Adiciona propriedade p-search-tree-items para que o filtro do componente retorne resultados de itens agrupadores do menu. Fixes DTHFUI-8971
1 parent 9dbdee1 commit d1e0480

File tree

6 files changed

+81
-2
lines changed

6 files changed

+81
-2
lines changed

projects/ui/src/lib/components/po-menu/po-menu-base.component.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ describe('PoMenuBaseComponent:', () => {
124124
expectPropertiesValues(component, 'filter', falseValues, false);
125125
});
126126

127+
it('should set searchTreeItems when filter is true', () => {
128+
const trueValues = ['', true, 1];
129+
const falseValues = [undefined, null, false, 0];
130+
131+
component.filter = true;
132+
133+
expectPropertiesValues(component, 'searchTreeItems', trueValues, true);
134+
expectPropertiesValues(component, 'searchTreeItems', falseValues, false);
135+
});
136+
127137
it('should set menu item properties', () => {
128138
const menuItem: any = { label: 'Utilidades', link: 'utilities' };
129139

projects/ui/src/lib/components/po-menu/po-menu-base.component.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export abstract class PoMenuBaseComponent {
9797

9898
private _collapsed = false;
9999
private _filter = false;
100+
private _searchTreeItems = false;
100101
private _level;
101102
private _maxLevel = 4;
102103
private _menus = [];
@@ -185,6 +186,27 @@ export abstract class PoMenuBaseComponent {
185186
return this._filter;
186187
}
187188

189+
/**
190+
* @optional
191+
*
192+
* @description
193+
*
194+
* Quando ativado, a pesquisa também retornará itens agrupadores além dos itens que contêm uma ação e/ou link definidos.
195+
* Isso pode ser útil quando se deseja encontrar rapidamente categorias ou seções do menu.
196+
*
197+
* > É necessário que a propriedade `p-filter` esteja habilitada.
198+
*
199+
* @default `false`
200+
*/
201+
@Input('p-search-tree-items') set searchTreeItems(searchTreeItems: boolean) {
202+
this._searchTreeItems = <any>searchTreeItems === '' ? true : convertToBoolean(searchTreeItems);
203+
this.filteredItems = [...this._menus];
204+
}
205+
206+
get searchTreeItems() {
207+
return this._searchTreeItems;
208+
}
209+
188210
/**
189211
* @optional
190212
*

projects/ui/src/lib/components/po-menu/po-menu.component.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,29 @@ describe('PoMenuComponent:', () => {
16031603
expect(component['findItems']).toHaveBeenCalled();
16041604
});
16051605

1606+
it('filterLocalItems: should return grouped items if `searchTreeItems` is true and `filter` is true', () => {
1607+
const menus = [
1608+
{ label: 'Account', link: '/account' },
1609+
{ label: 'Company Account', link: '/companyAccount' },
1610+
{ label: 'Test', subItems: [{ label: 'list', link: '/test/list' }] }
1611+
];
1612+
1613+
const foundMenu = [{ label: 'Test', subItems: [{ label: 'list', link: '/test/list' }], type: 'subItems' }];
1614+
1615+
const filter = 'Test';
1616+
1617+
component.filter = true;
1618+
component.searchTreeItems = true;
1619+
component.menus = menus;
1620+
1621+
spyOn(component, <any>'findItems').and.callThrough();
1622+
1623+
const filteredItems = component['filterLocalItems'](filter);
1624+
1625+
expect(filteredItems).toEqual(foundMenu);
1626+
expect(component['findItems']).toHaveBeenCalled();
1627+
});
1628+
16061629
it('filterOnService: should call `getFilteredData` and return filtered menu itens from service', async () => {
16071630
const menuItems = [{ label: 'Menu', link: '/menu', action: () => {} }];
16081631
const search = 'menu';

projects/ui/src/lib/components/po-menu/po-menu.component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,14 +452,16 @@ export class PoMenuComponent extends PoMenuBaseComponent implements AfterViewIni
452452

453453
private findItems(menus: Array<PoMenuItem>, filter: string, filteredItems: Array<any>) {
454454
menus.forEach(menu => {
455-
const hasAction = menu.action || menu.link;
455+
const hasAction = this.searchTreeItems ? this.searchTreeItems : menu.action || menu.link;
456456
const labelHasFilter = menu.label.toLowerCase().includes(filter);
457457

458458
if (labelHasFilter && hasAction) {
459459
const newMenu = { ...menu };
460460

461461
if (newMenu.subItems?.length) {
462-
delete newMenu.subItems;
462+
if (!this.searchTreeItems) {
463+
delete newMenu.subItems;
464+
}
463465
newMenu['type'] = this.setMenuType(newMenu);
464466
}
465467

projects/ui/src/lib/components/po-menu/samples/sample-po-menu-labs/sample-po-menu-labs.component.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
[p-params]="params"
77
[p-service]="service"
88
[p-short-logo]="shortLogo"
9+
[p-search-tree-items]="searchTreeItems"
910
>
1011
</po-menu>
1112

@@ -34,6 +35,18 @@
3435
p-label="Filter"
3536
p-label-off="Disabled"
3637
p-label-on="Enabled"
38+
(ngModelChange)="onFilterChange($event)"
39+
>
40+
</po-switch>
41+
42+
<po-switch
43+
class="po-lg-6"
44+
name="searchTreeItems"
45+
[(ngModel)]="searchTreeItems"
46+
p-label="Filter Search Tree Items"
47+
p-label-off="Disabled"
48+
p-label-on="Enabled"
49+
[p-disabled]="!filter"
3750
>
3851
</po-switch>
3952
</div>

projects/ui/src/lib/components/po-menu/samples/sample-po-menu-labs/sample-po-menu-labs.component.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export class SamplePoMenuLabsComponent implements OnInit {
4545
service: string;
4646
shortLabel: string;
4747
shortLogo: string;
48+
searchTreeItems: boolean;
4849

4950
public readonly badgeColorList: Array<PoSelectOption> = [
5051
{ label: 'color-01', value: 'color-01' },
@@ -133,6 +134,7 @@ export class SamplePoMenuLabsComponent implements OnInit {
133134
this.menuParams = undefined;
134135
this.service = '';
135136
this.shortLogo = undefined;
137+
this.searchTreeItems = false;
136138

137139
this.updateMenuItems();
138140
}
@@ -203,4 +205,11 @@ export class SamplePoMenuLabsComponent implements OnInit {
203205
}
204206
});
205207
}
208+
209+
onFilterChange(newValue: boolean) {
210+
this.filter = newValue;
211+
if (!this.filter && this.searchTreeItems) {
212+
this.searchTreeItems = false;
213+
}
214+
}
206215
}

0 commit comments

Comments
 (0)