forked from OpenEnergyTools/filterable-lists
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase-list.ts
More file actions
68 lines (53 loc) · 1.89 KB
/
Copy pathbase-list.ts
File metadata and controls
68 lines (53 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* eslint-disable @typescript-eslint/no-explicit-any */
import { html, LitElement, TemplateResult } from 'lit';
import { property, query, state } from 'lit/decorators.js';
import { ScopedElementsMixin } from '@open-wc/scoped-elements/lit-element.js';
import { TextField } from '@scopedelement/material-web/textfield/internal/text-field';
function searchRegex(filter?: string): RegExp {
if (!filter) return /.*/i;
const terms: string[] =
filter
.replace(/[.+^${}()|[\]\\]/g, '\\$&')
.trim()
.match(/(?:[^\s"']+|['"][^'"]*["'])+/g) ?? [];
const expandedTerms = terms.map(term =>
term.replace(/\*/g, '.*').replace(/\?/g, '.{1}').replace(/"|'/g, '')
);
const regexString = expandedTerms.map(term => `(?=.*${term})`);
return new RegExp(`${regexString.join('')}.*`, 'i');
}
function debounce(callback: any, delay = 100) {
let timeout: any;
return (...args: any) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
callback(...args);
}, delay);
};
}
// Base class for all filterable list components
export class FilterListBase extends ScopedElementsMixin(LitElement) {
/** Whether list items can be filtered on `headline` and `supportingText` */
@property({ type: Boolean })
filterable = false;
/** Placeholder for search input field */
@property({ type: String })
searchhelper = 'search';
@state()
protected searchRegex: RegExp = /.*/i;
@query('md-outlined-text-field')
protected searchInput?: TextField;
protected onFilter(): void {
this.searchRegex = searchRegex(this.searchInput?.value);
}
protected renderSearchField(): TemplateResult {
return this.filterable
? html`<md-outlined-text-field
placeholder="${this.searchhelper}"
@input="${debounce(() => this.onFilter())}"
>
<md-icon slot="leading-icon">search</md-icon></md-outlined-text-field
>`
: html``;
}
}