@kubex/zinc 1.1.158 → 1.1.159
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/custom-elements.json +23 -1
- package/dist/web-types.json +1 -1
- package/dist/zn.d.ts +3 -0
- package/dist/zn.min.js +227 -227
- package/package.json +1 -1
- package/src/components/filter-container/filter-container.component.ts +32 -48
- package/src/components/filter-container/filter-container.test.ts +25 -0
package/package.json
CHANGED
|
@@ -26,62 +26,41 @@ export default class ZnFilterContainer extends ZincElement {
|
|
|
26
26
|
|
|
27
27
|
@property() attr = 'filter';
|
|
28
28
|
|
|
29
|
+
private _filterFrame = 0;
|
|
30
|
+
|
|
29
31
|
public handleSearchChange(event: Event) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
const searchTerm = (event.target as HTMLInputElement).value.toLowerCase();
|
|
33
|
+
cancelAnimationFrame(this._filterFrame);
|
|
34
|
+
this._filterFrame = requestAnimationFrame(() => this.applyFilter(searchTerm));
|
|
35
|
+
}
|
|
34
36
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
} else {
|
|
40
|
-
(el as HTMLElement).style.display = 'none';
|
|
41
|
-
}
|
|
37
|
+
disconnectedCallback() {
|
|
38
|
+
super.disconnectedCallback();
|
|
39
|
+
cancelAnimationFrame(this._filterFrame);
|
|
40
|
+
}
|
|
42
41
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
anyVisible = true;
|
|
50
|
-
}
|
|
42
|
+
private applyFilter(searchTerm: string) {
|
|
43
|
+
const attr = `data-${this.attr}`;
|
|
44
|
+
const filterableElements = this.querySelectorAll<HTMLElement>(`[${attr}]`);
|
|
45
|
+
const panels = new Map<HTMLElement, boolean>();
|
|
46
|
+
const accordions = new Map<HTMLElement, boolean>();
|
|
47
|
+
let anyVisibleElements = false;
|
|
51
48
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
(panel as HTMLElement).style.display = 'none';
|
|
57
|
-
}
|
|
58
|
-
}
|
|
49
|
+
filterableElements.forEach((el) => {
|
|
50
|
+
const visible = searchTerm === '' || (el.getAttribute(attr)?.toLowerCase() || '').includes(searchTerm);
|
|
51
|
+
setDisplay(el, visible);
|
|
52
|
+
if (visible) anyVisibleElements = true;
|
|
59
53
|
|
|
60
|
-
const
|
|
61
|
-
if (
|
|
62
|
-
const itemElements = accordionItem.querySelectorAll(`[data-${this.attr}]`);
|
|
63
|
-
let anyVisible = false;
|
|
64
|
-
itemElements.forEach((item) => {
|
|
65
|
-
if ((item as HTMLElement).style.display !== 'none') {
|
|
66
|
-
anyVisible = true;
|
|
67
|
-
}
|
|
68
|
-
});
|
|
54
|
+
const panel = el.closest<HTMLElement>('zn-panel');
|
|
55
|
+
if (panel) panels.set(panel, (panels.get(panel) ?? false) || visible);
|
|
69
56
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}
|
|
73
|
-
}
|
|
57
|
+
const accordion = el.closest<HTMLElement>('zn-accordion');
|
|
58
|
+
if (accordion) accordions.set(accordion, (accordions.get(accordion) ?? false) || visible);
|
|
74
59
|
});
|
|
75
60
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// if nothing is visible, show a "no results found" message
|
|
83
|
-
const anyVisibleElements = Array.from(filterableElements).some((el) => {
|
|
84
|
-
return (el as HTMLElement).style.display !== 'none';
|
|
61
|
+
panels.forEach((visible, panel) => setDisplay(panel, visible));
|
|
62
|
+
accordions.forEach((visible, accordion) => {
|
|
63
|
+
if (visible) setDisplay(accordion, true);
|
|
85
64
|
});
|
|
86
65
|
|
|
87
66
|
let noResultsMessage = this.querySelector<HTMLElement>('.no-results-message');
|
|
@@ -107,3 +86,8 @@ export default class ZnFilterContainer extends ZincElement {
|
|
|
107
86
|
<slot></slot> `;
|
|
108
87
|
}
|
|
109
88
|
}
|
|
89
|
+
|
|
90
|
+
function setDisplay(el: HTMLElement, visible: boolean) {
|
|
91
|
+
const display = visible ? '' : 'none';
|
|
92
|
+
if (el.style.display !== display) el.style.display = display;
|
|
93
|
+
}
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import '../../../dist/zn.min.js';
|
|
2
2
|
import { expect, fixture, html } from '@open-wc/testing';
|
|
3
|
+
import type ZnFilterContainer from './filter-container.component';
|
|
4
|
+
|
|
5
|
+
const nextFrame = () => new Promise(requestAnimationFrame);
|
|
3
6
|
|
|
4
7
|
describe('<zn-filter-container>', () => {
|
|
5
8
|
it('should render a component', async () => {
|
|
@@ -7,4 +10,26 @@ describe('<zn-filter-container>', () => {
|
|
|
7
10
|
|
|
8
11
|
expect(el).to.exist;
|
|
9
12
|
});
|
|
13
|
+
|
|
14
|
+
it('hides non-matching items and empty panels', async () => {
|
|
15
|
+
const el = await fixture<ZnFilterContainer>(html`
|
|
16
|
+
<zn-filter-container>
|
|
17
|
+
<zn-panel id="fruit"><div data-filter="Apple"></div><div data-filter="Banana"></div></zn-panel>
|
|
18
|
+
<zn-panel id="veg"><div data-filter="Carrot"></div></zn-panel>
|
|
19
|
+
</zn-filter-container>
|
|
20
|
+
`);
|
|
21
|
+
|
|
22
|
+
el.handleSearchChange({target: {value: 'app'}} as unknown as Event);
|
|
23
|
+
await nextFrame();
|
|
24
|
+
|
|
25
|
+
expect(el.querySelector<HTMLElement>('[data-filter="Apple"]')!.style.display).to.equal('');
|
|
26
|
+
expect(el.querySelector<HTMLElement>('[data-filter="Banana"]')!.style.display).to.equal('none');
|
|
27
|
+
expect(el.querySelector<HTMLElement>('#fruit')!.style.display).to.equal('');
|
|
28
|
+
expect(el.querySelector<HTMLElement>('#veg')!.style.display).to.equal('none');
|
|
29
|
+
|
|
30
|
+
el.handleSearchChange({target: {value: ''}} as unknown as Event);
|
|
31
|
+
await nextFrame();
|
|
32
|
+
|
|
33
|
+
expect(el.querySelector<HTMLElement>('#veg')!.style.display).to.equal('');
|
|
34
|
+
});
|
|
10
35
|
});
|