/
chip.ts
166 lines (150 loc) · 4.86 KB
/
chip.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { LitElement, html, nothing } from 'lit';
import { property } from 'lit/decorators.js';
import { Ref, createRef, ref } from 'lit/directives/ref.js';
import { styles } from './themes/chip.base.css.js';
import { styles as shared } from './themes/shared/chip.common.css.js';
import { all } from './themes/themes.js';
import { themes } from '../../theming/theming-decorator.js';
import { addKeybindings } from '../common/controllers/key-bindings.js';
import { blazorTwoWayBind } from '../common/decorators/blazorTwoWayBind.js';
import { registerComponent } from '../common/definitions/register.js';
import { Constructor } from '../common/mixins/constructor.js';
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
import { SizableMixin } from '../common/mixins/sizable.js';
import IgcIconComponent from '../icon/icon.js';
export interface IgcChipEventMap {
igcRemove: CustomEvent<boolean>;
igcSelect: CustomEvent<boolean>;
}
/**
* Chips help people enter information, make selections, filter content, or trigger actions.
*
* @element igc-chip
*
* @slot - Renders the chip data.
* @slot prefix - Renders content before the data of the chip.
* @slot suffix - Renders content after the data of the chip.
*
* @fires igcRemove - Emits an event when the chip component is removed. Returns the removed chip component.
* @fires igcSelect - Emits event when the chip component is selected/deselected and any related animations and transitions also end.
*
* @csspart base - The base wrapper of the chip.
* @csspart prefix - The prefix container of the chip.
* @csspart suffix - The suffix container of the chip.
*/
@themes(all)
export default class IgcChipComponent extends SizableMixin(
EventEmitterMixin<IgcChipEventMap, Constructor<LitElement>>(LitElement)
) {
public static readonly tagName = 'igc-chip';
public static styles = [styles, shared];
/* blazorSuppress */
public static register() {
registerComponent(this, IgcIconComponent);
}
private _removePartRef: Ref<HTMLSlotElement> = createRef();
/**
* Sets the disabled state for the chip.
* @attr
*/
@property({ type: Boolean, reflect: true })
public disabled = false;
/**
* Defines if the chip is removable or not.
* @attr
*/
@property({ type: Boolean, reflect: true })
public removable = false;
/**
* Defines if the chip is selectable or not.
* @attr
*/
@property({ type: Boolean, reflect: true })
public selectable = false;
/**
* Defines if the chip is selected or not.
* @attr
*/
@property({ type: Boolean, reflect: true })
@blazorTwoWayBind('igcSelect', 'detail')
public selected = false;
/**
* A property that sets the color variant of the chip component.
* @attr
*/
@property({ reflect: true })
public variant!: 'primary' | 'success' | 'danger' | 'warning' | 'info';
constructor() {
super();
this.size = 'medium';
addKeybindings(this, {
ref: this._removePartRef,
bindingDefaults: { triggers: ['keyup'] },
}).setActivateHandler(this.handleRemove);
}
protected handleSelect() {
if (this.selectable) {
this.selected = !this.selected;
this.emitEvent('igcSelect', { detail: this.selected });
}
}
protected handleRemove(e: Event) {
this.emitEvent('igcRemove');
e.stopPropagation();
}
protected override render() {
return html`
<button
part="base"
.disabled="${this.disabled}"
aria-selected="${this.selected ? 'true' : 'false'}"
aria-disabled="${this.disabled ? 'true' : 'false'}"
@click=${this.handleSelect}
>
<span part="prefix">
${this.selectable && this.selected
? html`<slot @slotchange=${this.slotChanges} name="select">
<igc-icon
size=${this.size}
name="chip_select"
collection="internal"
></igc-icon>
</slot>`
: nothing}
<slot name="start"></slot>
<slot name="prefix"></slot>
</span>
<slot></slot>
<span part="suffix">
<slot name="end"></slot>
<slot name="suffix"></slot>
${this.removable && !this.disabled
? html`<slot
${ref(this._removePartRef)}
@slotchange=${this.slotChanges}
@click=${this.handleRemove}
name="remove"
>
<igc-icon
size=${this.size}
name="chip_cancel"
collection="internal"
tabindex="0"
role="button"
aria-label="remove"
></igc-icon>
</slot>`
: nothing}
</span>
</button>
`;
}
protected slotChanges() {
this.requestUpdate();
}
}
declare global {
interface HTMLElementTagNameMap {
'igc-chip': IgcChipComponent;
}
}