dees-catalog/ts_web/elements/dees-chips.ts

151 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-10-08 14:07:40 +00:00
import {
customElement,
html,
DeesElement,
property,
2023-08-07 18:02:18 +00:00
type TemplateResult,
2021-10-08 14:07:40 +00:00
cssManager,
css,
2023-08-07 23:10:02 +00:00
type CSSResult,
2021-10-08 14:07:40 +00:00
unsafeCSS,
2023-08-07 17:13:29 +00:00
} from '@design.estate/dees-element';
2021-10-08 14:07:40 +00:00
2023-08-07 17:13:29 +00:00
import * as domtools from '@design.estate/dees-domtools';
2023-10-23 19:23:18 +00:00
import { demoFunc } from './dees-chips.demo.js';
2021-10-08 14:07:40 +00:00
declare global {
interface HTMLElementTagNameMap {
'dees-chips': DeesChips;
}
}
2023-10-23 19:23:18 +00:00
type Tag = { key: string, value: string };
2021-10-08 14:07:40 +00:00
@customElement('dees-chips')
export class DeesChips extends DeesElement {
2023-10-23 19:23:18 +00:00
public static demo = demoFunc;
2021-10-08 14:07:40 +00:00
@property()
2023-10-23 19:23:18 +00:00
public selectionMode: 'none' | 'single' | 'multiple' = 'single';
2021-10-08 14:07:40 +00:00
@property({
type: Array
})
2023-10-23 19:23:18 +00:00
public selectableChips: Tag[] = [];
2021-10-08 14:07:40 +00:00
@property()
2023-10-23 19:23:18 +00:00
public selectedChip: Tag = null;
2021-10-08 14:07:40 +00:00
@property({
type: Array
})
2023-10-23 19:23:18 +00:00
public selectedChips: Tag[] = [];
2021-10-08 14:07:40 +00:00
constructor() {
super();
}
public static styles = [
cssManager.defaultStyles,
css`
:host {
display: block;
box-sizing: border-box;
}
.mainbox {
2023-10-23 19:23:18 +00:00
user-select: none;
2021-10-08 14:07:40 +00:00
}
.chip {
2023-10-23 19:23:18 +00:00
border-top: ${cssManager.bdTheme('1px solid #CCC', '1px solid #444')};
background: #333333;
display: inline-flex;
height: 20px;
line-height: 20px;
padding: 0px 8px;
font-size: 12px;
2021-10-08 14:07:40 +00:00
color: #fff;
2022-05-30 21:15:32 +00:00
border-radius: 40px;
margin-right: 4px;
margin-bottom: 8px;
2023-10-23 19:23:18 +00:00
position: relative;
overflow: hidden;
2021-10-08 14:07:40 +00:00
}
.chip:hover {
background: #666666;
cursor: pointer;
}
.chip.selected {
background: #00A3FF;
}
2023-10-23 19:23:18 +00:00
.chipKey {
background: rgba(0,0,0,0.3);
height: 100%;
display: inline-block;
margin-left: -8px;
padding-left: 8px;
padding-right: 8px;
margin-right: 8px;
}
2021-10-08 14:07:40 +00:00
`,
];
public render(): TemplateResult {
return html`
<div class="mainbox">
2023-10-23 19:23:18 +00:00
${this.selectableChips.map(chip => html`
<div @click=${() => this.selectChip(chip)} class="chip ${this.isSelected(chip) ? 'selected' : ''}">
${chip.key ? html`<div class="chipKey">${chip.key}</div>` : html``}${chip.value}
2021-10-08 14:07:40 +00:00
</div>
`)}
</div>
`;
}
public async firstUpdated() {
if (!this.textContent) {
this.textContent = 'Button';
this.performUpdate();
}
}
2023-10-23 19:23:18 +00:00
private isSelected(chip: Tag): boolean {
if (this.selectionMode === 'single') {
return this.selectedChip?.key === chip.key;
} else {
return this.selectedChips.some(selected => selected.key === chip.key);
}
}
public async selectChip(chip: Tag) {
if (this.selectionMode === 'none') {
return;
}
2021-10-08 14:07:40 +00:00
if (this.selectionMode === 'single') {
2023-10-23 19:23:18 +00:00
if (this.isSelected(chip)) {
2021-10-08 14:07:40 +00:00
this.selectedChip = null;
this.selectedChips = [];
} else {
2023-10-23 19:23:18 +00:00
this.selectedChip = chip;
this.selectedChips = [chip];
2021-10-08 14:07:40 +00:00
}
} else if(this.selectionMode === 'multiple') {
2023-10-23 19:23:18 +00:00
if (this.isSelected(chip)) {
this.selectedChips = this.selectedChips.filter(selected => selected.key !== chip.key);
2021-10-08 14:07:40 +00:00
} else {
2023-10-23 19:23:18 +00:00
this.selectedChips = [...this.selectedChips, chip];
2021-10-08 14:07:40 +00:00
}
this.requestUpdate();
}
console.log(this.selectedChips);
}
}