@spectric/ui 0.0.20 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,17 +2,21 @@ import { html } from "lit";
2
2
  import "../pagination";
3
3
  import { customElement, property, state } from "lit/decorators.js";
4
4
  import {
5
- HTMLElementTagWithEvents,
6
- ReactElementWithPropsAndEvents,
5
+ HTMLElementTagWithEvents,
6
+ ReactElementWithPropsAndEvents,
7
7
  } from "../types";
8
8
  import "./table.css";
9
9
  import "./header.css";
10
10
  export const TableHeaderElementTag = "spectric-table-header";
11
- import { ColumnSettings, TableSortDirection } from "./table";
11
+ import {
12
+ ColumnSettings,
13
+ SpectricTableElement,
14
+ TableSortDirection,
15
+ } from "./table";
12
16
  import { DisposableElement } from "../../classes";
13
17
 
14
18
  interface HeaderProps<T> {
15
- columns: ColumnSettings<T>[];
19
+ columns: ColumnSettings<T>[];
16
20
  }
17
21
 
18
22
  /**
@@ -20,169 +24,175 @@ interface HeaderProps<T> {
20
24
  */
21
25
  @customElement(TableHeaderElementTag)
22
26
  export class TableHeaderElement<T>
23
- extends DisposableElement
24
- implements HeaderProps<T> {
25
- @property({ type: Array, attribute: false })
26
- columns: ColumnSettings<T>[] = [];
27
- @state()
28
- private _resizeStart?: { event: MouseEvent; column: ColumnSettings<T> };
29
- constructor() {
30
- super();
31
- this.addDisposableListener(
32
- () => document.body,
33
- "mouseup",
34
- this._handleResizeEnd
35
- );
27
+ extends DisposableElement
28
+ implements HeaderProps<T>
29
+ {
30
+ @property({ type: Array, attribute: false })
31
+ columns: ColumnSettings<T>[] = [];
32
+ @state()
33
+ private _resizeStart?: { event: MouseEvent; column: ColumnSettings<T> };
34
+ @property({ type: Object, attribute: false })
35
+ table!: SpectricTableElement<T>;
36
+ constructor() {
37
+ super();
38
+ this.addDisposableListener(
39
+ () => document.body,
40
+ "mouseup",
41
+ this._handleResizeEnd
42
+ );
43
+ }
44
+ protected createRenderRoot(): HTMLElement | DocumentFragment {
45
+ return this;
46
+ }
47
+ _handleSortChange = (column: ColumnSettings<T>) => {
48
+ if (this._resizeStart) {
49
+ return;
36
50
  }
37
- protected createRenderRoot(): HTMLElement | DocumentFragment {
38
- return this;
51
+ column = JSON.parse(JSON.stringify(column)); // Clone the column to not mutate the original object
52
+ if (!column.sortable) {
53
+ return;
39
54
  }
40
- _handleSortChange = (column: ColumnSettings<T>) => {
41
- if (this._resizeStart) {
42
- return;
43
- }
44
- column = JSON.parse(JSON.stringify(column)); // Clone the column to not mutate the original object
45
- if (!column.sortable) {
46
- return;
47
- }
48
- if (
49
- column.sortDirection === TableSortDirection.none ||
50
- column.sortDirection === undefined
51
- ) {
52
- column.sortDirection = TableSortDirection.ascending;
53
- } else if (column.sortDirection === TableSortDirection.ascending) {
54
- column.sortDirection = TableSortDirection.descending;
55
- } else if (column.sortDirection === TableSortDirection.descending) {
56
- column.sortDirection = TableSortDirection.none;
57
- }
58
- this.dispatchEvent(
59
- new CustomEvent<ColumnSettings<T>>("sortChange", {
60
- composed: true,
61
- bubbles: true,
62
- detail: column,
63
- })
64
- );
65
- };
66
- _handleResizeStart = (event: MouseEvent, column: ColumnSettings<T>) => {
67
- this._resizeStart = { event, column };
68
- };
69
- _handleResizeEnd = (e: MouseEvent) => {
70
- if (!this._resizeStart) {
71
- return;
72
- }
73
- let delta = e.pageX - this._resizeStart?.event.pageX;
74
- if (this._resizeStart.column.width) {
75
- this._resizeStart.column.width = this._resizeStart.column.width + delta;
76
- } else {
77
- let cell = (this._resizeStart.event.target as HTMLDivElement).closest(
78
- "td"
79
- ) as HTMLTableCellElement;
80
- this._resizeStart.column.width =
81
- cell.getBoundingClientRect().width + delta;
55
+ if (
56
+ column.sortDirection === TableSortDirection.none ||
57
+ column.sortDirection === undefined
58
+ ) {
59
+ column.sortDirection = TableSortDirection.ascending;
60
+ } else if (column.sortDirection === TableSortDirection.ascending) {
61
+ column.sortDirection = TableSortDirection.descending;
62
+ } else if (column.sortDirection === TableSortDirection.descending) {
63
+ column.sortDirection = TableSortDirection.none;
64
+ }
65
+ this.dispatchEvent(
66
+ new CustomEvent<ColumnSettings<T>>("sortChange", {
67
+ composed: true,
68
+ bubbles: true,
69
+ detail: column,
70
+ })
71
+ );
72
+ };
73
+ _handleResizeStart = (event: MouseEvent, column: ColumnSettings<T>) => {
74
+ this._resizeStart = { event, column };
75
+ };
76
+ _handleResizeEnd = (e: MouseEvent) => {
77
+ if (!this._resizeStart) {
78
+ return;
79
+ }
80
+ let delta = e.pageX - this._resizeStart?.event.pageX;
81
+ if (this._resizeStart.column.width) {
82
+ this._resizeStart.column.width = this._resizeStart.column.width + delta;
83
+ } else {
84
+ let cell = (this._resizeStart.event.target as HTMLDivElement).closest(
85
+ "td"
86
+ ) as HTMLTableCellElement;
87
+ this._resizeStart.column.width =
88
+ cell.getBoundingClientRect().width + delta;
89
+ }
90
+ this.dispatchEvent(
91
+ new CustomEvent("columnResize", { detail: this._resizeStart.column })
92
+ );
93
+ //We wrap this to ensure any sort events get fired before we are done FIXME?
94
+ requestAnimationFrame(() => {
95
+ this._resizeStart = undefined;
96
+ //Clear any text that got selected during column resize
97
+ if (window.getSelection) {
98
+ let selection = window.getSelection();
99
+ if (selection) {
100
+ selection.removeAllRanges();
82
101
  }
83
- this.dispatchEvent(
84
- new CustomEvent("columnResize", { detail: this._resizeStart.column })
85
- );
86
- //We wrap this to ensure any sort events get fired before we are done FIXME?
87
- requestAnimationFrame(() => {
88
- this._resizeStart = undefined;
89
- //Clear any text that got selected during column resize
90
- if (window.getSelection) {
91
- let selection = window.getSelection();
92
- if (selection) {
93
- selection.removeAllRanges();
94
- }
95
- }
96
- })
97
- };
98
- protected render(): unknown {
99
- return html`
102
+ }
103
+ });
104
+ };
105
+ protected render(): unknown {
106
+ return html`
100
107
  <tr>
101
- ${this.columns.map(column => this.renderCell(column))}
108
+ ${this.columns.map((column) => this.renderCell(column))}
102
109
  </tr>
103
110
  `;
111
+ }
112
+ private renderCell(column: ColumnSettings<T>) {
113
+ let classes = ["header-contents"];
114
+ if (column.filterable) {
115
+ //classes.push("filterable")
104
116
  }
105
- private renderCell(column: ColumnSettings<T>) {
106
- let classes = ["header-contents"];
107
- if (column.filterable) {
108
- //classes.push("filterable")
109
- }
110
- if (column.sortable) {
111
- classes.push("sortable");
112
- }
113
- if (this._resizeStart) {
114
- classes.push("resizing");
115
- }
116
- let columnWidth = column.width
117
- ? `min-width:${column.width}px;max-width:${column.width}px;`
118
- : "";
119
- let sortDirection =
120
- column.sortDirection === TableSortDirection.ascending
121
- ? `🠉`
122
- : column.sortDirection == TableSortDirection.descending
123
- ? `🠋`
124
- : ``;
125
- let sortClass = column.sortDirection || TableSortDirection.none;
126
- let resizable =
127
- column.allowResize || column.allowResize === undefined;
128
- let resizeHandle = resizable
129
- ? html`<div
130
- class="header-resize-handle"
131
- @mousedown=${(e: MouseEvent) => {
132
- this._handleResizeStart(e, column);
133
- }}
134
- @mouseup=${this._handleResizeEnd}
135
- ></div>`
136
- : null
137
- return html` <td
138
- @click=${() => this._handleSortChange(column)}
139
- style="${columnWidth}"
140
- >
141
- ${resizeHandle}
142
- <div class=${classes.join(" ")}>
143
- ${column.title || column.key}
144
- <span class="sort-direction ${sortClass}">${sortDirection}</span>
145
- </div>
146
- </td>`;
117
+ if (column.sortable) {
118
+ classes.push("sortable");
119
+ }
120
+ if (this._resizeStart) {
121
+ classes.push("resizing");
147
122
  }
123
+ let columnWidth = column.width
124
+ ? `min-width:${column.width}px;max-width:${column.width}px;`
125
+ : "";
126
+ let sortDirection =
127
+ column.sortDirection === TableSortDirection.ascending
128
+ ? `🠉`
129
+ : column.sortDirection == TableSortDirection.descending
130
+ ? `🠋`
131
+ : ``;
132
+ let sortClass = column.sortDirection || TableSortDirection.none;
133
+ let resizable = column.allowResize || column.allowResize === undefined;
134
+ let resizeHandle = resizable
135
+ ? html`<div
136
+ class="header-resize-handle"
137
+ @mousedown=${(e: MouseEvent) => {
138
+ this._handleResizeStart(e, column);
139
+ }}
140
+ @mouseup=${this._handleResizeEnd}
141
+ ></div>`
142
+ : null;
143
+ let title = column.title || column.key;
144
+ if (typeof title == "function") {
145
+ title = title(this.table) || undefined;
146
+ }
147
+ return html` <td
148
+ @click=${() => this._handleSortChange(column)}
149
+ style="${columnWidth}"
150
+ >
151
+ ${resizeHandle}
152
+ <div class=${classes.join(" ")}>
153
+ ${title}
154
+ <span class="sort-direction ${sortClass}">${sortDirection}</span>
155
+ </div>
156
+ </td>`;
157
+ }
148
158
  }
149
159
 
150
160
  interface TableHeaderEvents {
151
- sortChange: (event: CustomEvent<ColumnSettings<any>>) => void; //TODO sort events
152
- columnResize: (event: CustomEvent<ColumnSettings<any>>) => void;
161
+ sortChange: (event: CustomEvent<ColumnSettings<any>>) => void; //TODO sort events
162
+ columnResize: (event: CustomEvent<ColumnSettings<any>>) => void;
153
163
  }
154
164
 
155
165
  declare global {
156
- interface HTMLElementTagNameMap {
157
- [TableHeaderElementTag]: HTMLElementTagWithEvents<
158
- TableHeaderElement<any>,
159
- TableHeaderEvents
160
- >;
166
+ interface HTMLElementTagNameMap {
167
+ [TableHeaderElementTag]: HTMLElementTagWithEvents<
168
+ TableHeaderElement<any>,
169
+ TableHeaderEvents
170
+ >;
171
+ }
172
+ namespace JSX {
173
+ interface IntrinsicElements {
174
+ /**
175
+ * @see {@link DialogElement}
176
+ */
177
+ [TableHeaderElementTag]: ReactElementWithPropsAndEvents<
178
+ TableHeaderElement<any>,
179
+ HeaderProps<any>,
180
+ TableHeaderEvents
181
+ >;
161
182
  }
183
+ }
184
+ namespace React {
162
185
  namespace JSX {
163
- interface IntrinsicElements {
164
- /**
165
- * @see {@link DialogElement}
166
- */
167
- [TableHeaderElementTag]: ReactElementWithPropsAndEvents<
168
- TableHeaderElement<any>,
169
- HeaderProps<any>,
170
- TableHeaderEvents
171
- >;
172
- }
173
- }
174
- namespace React {
175
- namespace JSX {
176
- interface IntrinsicElements {
177
- /**
178
- * @see {@link DialogElement}
179
- */
180
- [TableHeaderElementTag]: ReactElementWithPropsAndEvents<
181
- TableHeaderElement<any>,
182
- HeaderProps<any>,
183
- TableHeaderEvents
184
- >;
185
- }
186
- }
186
+ interface IntrinsicElements {
187
+ /**
188
+ * @see {@link DialogElement}
189
+ */
190
+ [TableHeaderElementTag]: ReactElementWithPropsAndEvents<
191
+ TableHeaderElement<any>,
192
+ HeaderProps<any>,
193
+ TableHeaderEvents
194
+ >;
195
+ }
187
196
  }
197
+ }
188
198
  }