@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubex/zinc",
3
- "version": "1.1.158",
3
+ "version": "1.1.159",
4
4
  "description": "A collection of web components for building web applications based off of @shoelace-style/Shoelace",
5
5
  "keywords": [
6
6
  "web components",
@@ -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
- // get all element that have data-'this.attr' attribute
31
- const filterableElements = this.querySelectorAll(`[data-${this.attr}]`);
32
- const input = event.target as HTMLInputElement;
33
- const searchTerm = input.value.toLowerCase();
32
+ const searchTerm = (event.target as HTMLInputElement).value.toLowerCase();
33
+ cancelAnimationFrame(this._filterFrame);
34
+ this._filterFrame = requestAnimationFrame(() => this.applyFilter(searchTerm));
35
+ }
34
36
 
35
- filterableElements.forEach((el) => {
36
- const filterValue = el.getAttribute(`data-${this.attr}`)?.toLowerCase() || '';
37
- if (filterValue.includes(searchTerm)) {
38
- (el as HTMLElement).style.display = '';
39
- } else {
40
- (el as HTMLElement).style.display = 'none';
41
- }
37
+ disconnectedCallback() {
38
+ super.disconnectedCallback();
39
+ cancelAnimationFrame(this._filterFrame);
40
+ }
42
41
 
43
- const panel = el.closest('zn-panel');
44
- if (panel) {
45
- const panelItems = panel.querySelectorAll(`[data-${this.attr}]`);
46
- let anyVisible = false;
47
- panelItems.forEach((item) => {
48
- if ((item as HTMLElement).style.display !== 'none') {
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
- if (anyVisible) {
54
- (panel as HTMLElement).style.display = '';
55
- } else {
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 accordionItem = el.closest('zn-accordion');
61
- if (accordionItem) {
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
- if (anyVisible) {
71
- (accordionItem as HTMLElement).style.display = '';
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
- if (searchTerm === '') {
77
- filterableElements.forEach((el) => {
78
- (el as HTMLElement).style.display = '';
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
  });