This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
/
table-header-cell.ts
194 lines (174 loc) · 5.57 KB
/
table-header-cell.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* @license
*
* Copyright IBM Corp. 2019
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
import settings from 'carbon-components/es/globals/js/settings';
import { html, property, customElement, LitElement } from 'lit-element';
import Arrows16 from '@carbon/icons/lib/arrows/16';
import ArrowDown16 from '@carbon/icons/lib/arrow--down/16';
import FocusMixin from '../../globals/mixins/focus';
import styles from './data-table.scss';
const { prefix } = settings;
/**
* Table sort state.
*/
export enum TABLE_SORT_DIRECTION {
/**
* Not sorted.
*/
NONE = 'none',
/**
* Sorted ascendingly.
*/
ASCENDING = 'ascending',
/**
* Sorted descendingly.
*/
DESCENDING = 'descending',
}
/**
* Table sort cycle.
*/
export enum TABLE_SORT_CYCLE {
BI_STATES_FROM_ASCENDING = 'bi-states-from-ascending',
BI_STATES_FROM_DESCENDING = 'bi-states-from-descending',
TRI_STATES_FROM_ASCENDING = 'tri-states-from-ascending',
TRI_STATES_FROM_DESCENDING = 'tri-states-from-descending',
}
/**
* Mapping of table sort cycles to table sort states.
*/
export const TABLE_SORT_CYCLES = {
[TABLE_SORT_CYCLE.BI_STATES_FROM_ASCENDING]: [TABLE_SORT_DIRECTION.ASCENDING, TABLE_SORT_DIRECTION.DESCENDING],
[TABLE_SORT_CYCLE.BI_STATES_FROM_DESCENDING]: [TABLE_SORT_DIRECTION.DESCENDING, TABLE_SORT_DIRECTION.ASCENDING],
[TABLE_SORT_CYCLE.TRI_STATES_FROM_ASCENDING]: [
TABLE_SORT_DIRECTION.NONE,
TABLE_SORT_DIRECTION.ASCENDING,
TABLE_SORT_DIRECTION.DESCENDING,
],
[TABLE_SORT_CYCLE.TRI_STATES_FROM_DESCENDING]: [
TABLE_SORT_DIRECTION.NONE,
TABLE_SORT_DIRECTION.DESCENDING,
TABLE_SORT_DIRECTION.ASCENDING,
],
};
/**
* Data table header cell.
*/
@customElement(`${prefix}-table-header-cell`)
class BXTableHeaderCell extends FocusMixin(LitElement) {
/**
* Handles `click` event on the sort button.
* @param event The event.
*/
private _handleClickSortButton() {
const nextSortDirection = this._getNextSort();
const init = {
bubbles: true,
cancelable: true,
composed: true,
detail: {
oldSortDirection: this.sortDirection,
sortDirection: nextSortDirection,
},
};
const constructor = this.constructor as typeof BXTableHeaderCell;
if (this.dispatchEvent(new CustomEvent(constructor.eventBeforeSort, init))) {
this.sortActive = true;
this.sortDirection = nextSortDirection;
}
}
/**
* Handles `slotchange` event.
* @param event The event.
*/
private _handleSlotChange() {
this.requestUpdate();
}
/**
* @returns The next sort direction.
*/
private _getNextSort() {
const { sortCycle = TABLE_SORT_CYCLE.TRI_STATES_FROM_ASCENDING, sortDirection } = this;
if (!sortDirection) {
throw new TypeError(
'Table sort direction is not defined. ' +
'Likely that `_getNextSort()` is called with non-sorted table column, which should not happen in regular condition.'
);
}
const directions = (this.constructor as typeof BXTableHeaderCell).TABLE_SORT_CYCLES[sortCycle];
const index = directions.indexOf(sortDirection as TABLE_SORT_DIRECTION);
if (index < 0) {
if (sortDirection === TABLE_SORT_DIRECTION.NONE) {
// If the current sort direction is `none` in bi-state sort cycle, returns the first one in the cycle
return directions[0];
}
throw new RangeError(`The given sort state (${sortDirection}) is not found in the given table sort cycle: ${sortCycle}`);
}
return directions[(index + 1) % directions.length];
}
/**
* `true` if this table header cell is of a primary sorting column. Corresponds to `sort-active` attribute.
*/
@property({ type: Boolean, reflect: true, attribute: 'sort-active' })
sortActive = false;
/**
* The table sort cycle in use. Corresponds to `sort-cycle` attribute.
*/
@property({ reflect: true, attribute: 'sort-cycle' })
sortCycle?: TABLE_SORT_CYCLE;
/**
* The table sort direction. Corresponds to `sort-direction` attribute.
* If present, this table header cell will have a sorting UI.
*/
@property({ reflect: true, attribute: 'sort-direction' })
sortDirection?: TABLE_SORT_DIRECTION;
createRenderRoot() {
return this.attachShadow({ mode: 'open', delegatesFocus: true });
}
connectedCallback() {
if (!this.hasAttribute('role')) {
this.setAttribute('role', 'columnheader');
}
super.connectedCallback();
}
render() {
const { sortDirection } = this;
if (sortDirection) {
const sortIcon =
sortDirection === TABLE_SORT_DIRECTION.NONE
? Arrows16({
class: `${prefix}--table-sort__icon-unsorted`,
})
: ArrowDown16({
class: `${prefix}--table-sort__icon`,
});
return html`
<button class="${prefix}--table-sort" title="${this.textContent}" @click=${this._handleClickSortButton}>
<span class="${prefix}--table-header-label"><slot @slotchange=${this._handleSlotChange}></slot></span>
${sortIcon}
</button>
`;
}
return html`
<slot></slot>
`;
}
/**
* The name of the custom event fired before a new sort direction is set upon a user gesture.
* Cancellation of this event stops the user-initiated change in sort direction.
*/
static get eventBeforeSort() {
return `${prefix}-table-header-cell-sort`;
}
static styles = styles;
/**
* Mapping of table sort cycles to table sort states.
*/
static TABLE_SORT_CYCLES = TABLE_SORT_CYCLES;
}
export default BXTableHeaderCell;