-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdb-sidenavi.tsx
79 lines (71 loc) · 1.97 KB
/
db-sidenavi.tsx
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
69
70
71
72
73
74
75
76
77
78
79
import { Component, h, Element, Prop } from '@stencil/core';
import {
addIconVariantToAllDbLinksRecursive,
parseData
} from '../../utils/utils';
import { DbLinkType } from '../db-link/db-link-type';
const getCompDataHtml = (compData: DbLinkType[]) => {
if (!compData) return '';
return compData
.map(
(data) =>
`<li><db-link href="${data.href}" target="${
data.target
}" icon-variant="${data.icon ? '32-outline' : false}" icon="${
data.icon
}" current="${data.ariaCurrent}">${data.label}</db-link></li>`
)
.join('\n');
};
@Component({
tag: 'db-sidenavi',
styleUrl: 'db-sidenavi.scss'
})
export class DbSidenavi {
/**
* The data attribute can be used to generate sidenav by data.
*/
@Prop({ reflect: true }) data?: string;
private compData: DbLinkType[];
private hasItemsWrapper: boolean;
get children(): Element[] {
return this._children;
}
set children(value: Element[]) {
this._children = value;
}
private _children: Element[];
@Element() host: HTMLDbSidenaviElement;
componentWillLoad() {
if (this.data) {
this.compData = parseData(this.data);
} else {
addIconVariantToAllDbLinksRecursive(this.host, '32-outline');
this._children = Array.from(this.host?.children);
if (this.children.find((child) => child.tagName.toLowerCase() === 'li')) {
this.hasItemsWrapper = true;
} else {
this.host.innerHTML = '';
}
}
}
render() {
return (
<nav class="cmp-sidenavi">
{this.compData && <ol innerHTML={getCompDataHtml(this.compData)} />}
{!this.compData && (
<ol>
{!this.hasItemsWrapper &&
this._children?.map((child, index) => (
<li
key={`sidenavi-item-${index}`}
innerHTML={child.outerHTML}
/>
))}
{this.hasItemsWrapper && <slot />}
</ol>
)}
</nav>
);
}
}