Skip to content

Commit 7c08349

Browse files
authored
refactor: extract grid selection column base mixin (#6143)
* refactor: extract GridSelectionColumnBaseMixin * Update docs and Typescript definitions * Remove mixes annotation from TS definition * Add protected methods to TS definition
1 parent d69754a commit 7c08349

5 files changed

Lines changed: 292 additions & 134 deletions
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2016 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import type { Constructor } from '@open-wc/dedupe-mixin';
7+
8+
export declare function GridSelectionColumnBaseMixin<TItem, T extends Constructor<HTMLElement>>(
9+
base: T,
10+
): Constructor<GridSelectionColumnBaseMixinClass<TItem>> & T;
11+
12+
/**
13+
* A mixin that provides basic functionality for the
14+
* `<vaadin-grid-selection-column>`. This includes properties, cell rendering,
15+
* and overridable methods for handling changes to the selection state.
16+
*
17+
* **NOTE**: This mixin is re-used by the Flow component, and as such must not
18+
* implement any selection state updates for the column element or the grid.
19+
* Web component-specific selection state updates must be implemented in the
20+
* `<vaadin-grid-selection-column>` itself, by overriding the protected methods
21+
* provided by this mixin.
22+
*
23+
* @polymerMixin
24+
*/
25+
export declare class GridSelectionColumnBaseMixinClass<TItem> {
26+
/**
27+
* When true, all the items are selected.
28+
* @attr {boolean} select-all
29+
*/
30+
selectAll: boolean;
31+
32+
/**
33+
* When true, the active gets automatically selected.
34+
* @attr {boolean} auto-select
35+
*/
36+
autoSelect: boolean;
37+
38+
/**
39+
* Override to handle the user selecting all items.
40+
*/
41+
protected _selectAll(): void;
42+
43+
/**
44+
* Override to handle the user deselecting all items.
45+
*/
46+
protected _deselectAll(): void;
47+
48+
/**
49+
* Override to handle the user selecting an item.
50+
*/
51+
protected _selectItem(item: TItem): void;
52+
53+
/**
54+
* Override to handle the user deselecting an item.
55+
*/
56+
protected _deselectItem(item: TItem): void;
57+
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2016 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
7+
/**
8+
* A mixin that provides basic functionality for the
9+
* `<vaadin-grid-selection-column>`. This includes properties, cell rendering,
10+
* and overridable methods for handling changes to the selection state.
11+
*
12+
* **NOTE**: This mixin is re-used by the Flow component, and as such must not
13+
* implement any selection state updates for the column element or the grid.
14+
* Web component-specific selection state updates must be implemented in the
15+
* `<vaadin-grid-selection-column>` itself, by overriding the protected methods
16+
* provided by this mixin.
17+
*
18+
* @polymerMixin
19+
*/
20+
export const GridSelectionColumnBaseMixin = (superClass) =>
21+
class GridSelectionColumnBaseMixin extends superClass {
22+
static get properties() {
23+
return {
24+
/**
25+
* Width of the cells for this column.
26+
*/
27+
width: {
28+
type: String,
29+
value: '58px',
30+
},
31+
32+
/**
33+
* Flex grow ratio for the cell widths. When set to 0, cell width is fixed.
34+
* @attr {number} flex-grow
35+
* @type {number}
36+
*/
37+
flexGrow: {
38+
type: Number,
39+
value: 0,
40+
},
41+
42+
/**
43+
* When true, all the items are selected.
44+
* @attr {boolean} select-all
45+
* @type {boolean}
46+
*/
47+
selectAll: {
48+
type: Boolean,
49+
value: false,
50+
notify: true,
51+
},
52+
53+
/**
54+
* When true, the active gets automatically selected.
55+
* @attr {boolean} auto-select
56+
* @type {boolean}
57+
*/
58+
autoSelect: {
59+
type: Boolean,
60+
value: false,
61+
},
62+
63+
/** @protected */
64+
_indeterminate: Boolean,
65+
66+
/** @protected */
67+
_selectAllHidden: Boolean,
68+
};
69+
}
70+
71+
static get observers() {
72+
return [
73+
'_onHeaderRendererOrBindingChanged(_headerRenderer, _headerCell, path, header, selectAll, _indeterminate, _selectAllHidden)',
74+
];
75+
}
76+
77+
/**
78+
* Renders the Select All checkbox to the header cell.
79+
*
80+
* @override
81+
*/
82+
_defaultHeaderRenderer(root, _column) {
83+
let checkbox = root.firstElementChild;
84+
if (!checkbox) {
85+
checkbox = document.createElement('vaadin-checkbox');
86+
checkbox.setAttribute('aria-label', 'Select All');
87+
checkbox.classList.add('vaadin-grid-select-all-checkbox');
88+
root.appendChild(checkbox);
89+
// Add listener after appending, so we can skip the initial change event
90+
checkbox.addEventListener('checked-changed', this.__onSelectAllCheckedChanged.bind(this));
91+
}
92+
93+
const checked = this.__isChecked(this.selectAll, this._indeterminate);
94+
checkbox.__rendererChecked = checked;
95+
checkbox.checked = checked;
96+
checkbox.hidden = this._selectAllHidden;
97+
checkbox.indeterminate = this._indeterminate;
98+
}
99+
100+
/**
101+
* Renders the Select Row checkbox to the body cell.
102+
*
103+
* @override
104+
*/
105+
_defaultRenderer(root, _column, { item, selected }) {
106+
let checkbox = root.firstElementChild;
107+
if (!checkbox) {
108+
checkbox = document.createElement('vaadin-checkbox');
109+
checkbox.setAttribute('aria-label', 'Select Row');
110+
root.appendChild(checkbox);
111+
// Add listener after appending, so we can skip the initial change event
112+
checkbox.addEventListener('checked-changed', this.__onSelectRowCheckedChanged.bind(this));
113+
}
114+
115+
checkbox.__item = item;
116+
checkbox.__rendererChecked = selected;
117+
checkbox.checked = selected;
118+
}
119+
120+
/**
121+
* Updates the select all state when the Select All checkbox is switched.
122+
* The listener handles only user-fired events.
123+
*
124+
* @private
125+
*/
126+
__onSelectAllCheckedChanged(e) {
127+
// Skip if the state is changed by the renderer.
128+
if (e.target.checked === e.target.__rendererChecked) {
129+
return;
130+
}
131+
132+
if (this._indeterminate || e.target.checked) {
133+
this._selectAll();
134+
} else {
135+
this._deselectAll();
136+
}
137+
}
138+
139+
/**
140+
* Selects or deselects the row when the Select Row checkbox is switched.
141+
* The listener handles only user-fired events.
142+
*
143+
* @private
144+
*/
145+
__onSelectRowCheckedChanged(e) {
146+
// Skip if the state is changed by the renderer.
147+
if (e.target.checked === e.target.__rendererChecked) {
148+
return;
149+
}
150+
151+
if (e.target.checked) {
152+
this._selectItem(e.target.__item);
153+
} else {
154+
this._deselectItem(e.target.__item);
155+
}
156+
}
157+
158+
/**
159+
* Override to handle the user selecting all items.
160+
* @protected
161+
*/
162+
_selectAll() {}
163+
164+
/**
165+
* Override to handle the user deselecting all items.
166+
* @protected
167+
*/
168+
_deselectAll() {}
169+
170+
/**
171+
* Override to handle the user selecting an item.
172+
* @param {Object} item the item to select
173+
* @protected
174+
*/
175+
_selectItem(item) {}
176+
177+
/**
178+
* Override to handle the user deselecting an item.
179+
* @param {Object} item the item to deselect
180+
* @protected
181+
*/
182+
_deselectItem(item) {}
183+
184+
/**
185+
* IOS needs indeterminate + checked at the same time
186+
* @private
187+
*/
188+
__isChecked(selectAll, indeterminate) {
189+
return indeterminate || selectAll;
190+
}
191+
};

‎packages/grid/src/vaadin-grid-selection-column.d.ts‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
import type { GridDefaultItem } from './vaadin-grid.js';
77
import { GridColumn } from './vaadin-grid-column.js';
8+
import type { GridSelectionColumnBaseMixinClass } from './vaadin-grid-selection-column-base-mixin.js';
89

910
/**
1011
* Fired when the `selectAll` property changes.
@@ -42,18 +43,6 @@ export interface GridSelectionColumnEventMap extends HTMLElementEventMap, GridSe
4243
* @fires {CustomEvent} select-all-changed - Fired when the `selectAll` property changes.
4344
*/
4445
declare class GridSelectionColumn<TItem = GridDefaultItem> extends GridColumn<TItem> {
45-
/**
46-
* When true, all the items are selected.
47-
* @attr {boolean} select-all
48-
*/
49-
selectAll: boolean;
50-
51-
/**
52-
* When true, the active gets automatically selected.
53-
* @attr {boolean} auto-select
54-
*/
55-
autoSelect: boolean;
56-
5746
addEventListener<K extends keyof GridSelectionColumnEventMap>(
5847
type: K,
5948
listener: (this: GridSelectionColumn<TItem>, ev: GridSelectionColumnEventMap[K]) => void,
@@ -67,6 +56,8 @@ declare class GridSelectionColumn<TItem = GridDefaultItem> extends GridColumn<TI
6756
): void;
6857
}
6958

59+
interface GridSelectionColumn<TItem = GridDefaultItem> extends GridSelectionColumnBaseMixinClass<TItem> {}
60+
7061
declare global {
7162
interface HTMLElementTagNameMap {
7263
'vaadin-grid-selection-column': GridSelectionColumn<GridDefaultItem>;

0 commit comments

Comments
 (0)