Skip to content

Commit 589c5d7

Browse files
authored
refactor: extract grid utility columns logic to separate mixins (#6610)
1 parent 91ea11e commit 589c5d7

14 files changed

Lines changed: 515 additions & 420 deletions
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 GridFilterColumnMixin<T extends Constructor<HTMLElement>>(
9+
superclass: T,
10+
): Constructor<GridFilterColumnMixinClass> & T;
11+
12+
export declare class GridFilterColumnMixinClass {
13+
/**
14+
* Text to display as the label of the column filter text-field.
15+
*/
16+
header: string | null | undefined;
17+
18+
/**
19+
* JS Path of the property in the item used for filtering the data.
20+
*/
21+
path: string | null | undefined;
22+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
* @polymerMixin
9+
*/
10+
export const GridFilterColumnMixin = (superClass) =>
11+
class extends superClass {
12+
static get properties() {
13+
return {
14+
/**
15+
* JS Path of the property in the item used for filtering the data.
16+
*/
17+
path: String,
18+
19+
/**
20+
* Text to display as the label of the column filter text-field.
21+
*/
22+
header: String,
23+
};
24+
}
25+
26+
static get observers() {
27+
return ['_onHeaderRendererOrBindingChanged(_headerRenderer, _headerCell, path, header, _filterValue)'];
28+
}
29+
30+
constructor() {
31+
super();
32+
33+
this.__boundOnFilterValueChanged = this.__onFilterValueChanged.bind(this);
34+
}
35+
36+
/**
37+
* Renders the grid filter with the custom text field to the header cell.
38+
*
39+
* @override
40+
*/
41+
_defaultHeaderRenderer(root, _column) {
42+
let filter = root.firstElementChild;
43+
let textField = filter ? filter.firstElementChild : undefined;
44+
45+
if (!filter) {
46+
filter = document.createElement('vaadin-grid-filter');
47+
textField = document.createElement('vaadin-text-field');
48+
textField.setAttribute('theme', 'small');
49+
textField.setAttribute('style', 'max-width: 100%;');
50+
textField.setAttribute('focus-target', '');
51+
textField.addEventListener('value-changed', this.__boundOnFilterValueChanged);
52+
filter.appendChild(textField);
53+
root.appendChild(filter);
54+
}
55+
56+
filter.path = this.path;
57+
filter.value = this._filterValue;
58+
59+
textField.__rendererValue = this._filterValue;
60+
textField.value = this._filterValue;
61+
textField.label = this.__getHeader(this.header, this.path);
62+
}
63+
64+
/**
65+
* The filter column doesn't allow to use a custom header renderer
66+
* to override the header cell content.
67+
* It always renders the grid filter to the header cell.
68+
*
69+
* @override
70+
*/
71+
_computeHeaderRenderer() {
72+
return this._defaultHeaderRenderer;
73+
}
74+
75+
/**
76+
* Updates the internal filter value once the filter text field is changed.
77+
* The listener handles only user-fired events.
78+
*
79+
* @private
80+
*/
81+
__onFilterValueChanged(e) {
82+
// Skip if the value is changed by the renderer.
83+
if (e.detail.value === e.target.__rendererValue) {
84+
return;
85+
}
86+
87+
this._filterValue = e.detail.value;
88+
}
89+
90+
/** @private */
91+
__getHeader(header, path) {
92+
if (header) {
93+
return header;
94+
}
95+
96+
if (path) {
97+
return this._generateHeader(path);
98+
}
99+
}
100+
};

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import type { GridDefaultItem } from './vaadin-grid.js';
7-
import { GridColumn } from './vaadin-grid-column.js';
7+
import type { GridColumn, GridColumnMixin } from './vaadin-grid-column.js';
8+
import type { GridFilterColumnMixinClass } from './vaadin-grid-filter-column-mixin.js';
9+
10+
export * from './vaadin-grid-filter-column-mixin.js';
811

912
/**
1013
* `<vaadin-grid-filter-column>` is a helper element for the `<vaadin-grid>`
@@ -19,17 +22,12 @@ import { GridColumn } from './vaadin-grid-column.js';
1922
* ...
2023
* ```
2124
*/
22-
declare class GridFilterColumn<TItem = GridDefaultItem> extends GridColumn<TItem> {
23-
/**
24-
* Text to display as the label of the column filter text-field.
25-
*/
26-
header: string | null | undefined;
25+
declare class GridFilterColumn<TItem = GridDefaultItem> extends HTMLElement {}
2726

28-
/**
29-
* JS Path of the property in the item used for filtering the data.
30-
*/
31-
path: string | null | undefined;
32-
}
27+
interface GridFilterColumn<TItem = GridDefaultItem>
28+
extends GridFilterColumnMixinClass,
29+
GridColumnMixin<TItem, GridColumn<TItem>>,
30+
GridColumn<TItem> {}
3331

3432
declare global {
3533
interface HTMLElementTagNameMap {

‎packages/grid/src/vaadin-grid-filter-column.js‎

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import './vaadin-grid-filter.js';
77
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
88
import { GridColumn } from './vaadin-grid-column.js';
9+
import { GridFilterColumnMixin } from './vaadin-grid-filter-column-mixin.js';
910

1011
/**
1112
* `<vaadin-grid-filter-column>` is a helper element for the `<vaadin-grid>`
@@ -22,100 +23,12 @@ import { GridColumn } from './vaadin-grid-column.js';
2223
*
2324
* @customElement
2425
* @extends GridColumn
26+
* @mixes GridFilterColumnMixin
2527
*/
26-
class GridFilterColumn extends GridColumn {
28+
class GridFilterColumn extends GridFilterColumnMixin(GridColumn) {
2729
static get is() {
2830
return 'vaadin-grid-filter-column';
2931
}
30-
31-
static get properties() {
32-
return {
33-
/**
34-
* JS Path of the property in the item used for filtering the data.
35-
*/
36-
path: String,
37-
38-
/**
39-
* Text to display as the label of the column filter text-field.
40-
*/
41-
header: String,
42-
};
43-
}
44-
45-
static get observers() {
46-
return ['_onHeaderRendererOrBindingChanged(_headerRenderer, _headerCell, path, header, _filterValue)'];
47-
}
48-
49-
constructor() {
50-
super();
51-
52-
this.__boundOnFilterValueChanged = this.__onFilterValueChanged.bind(this);
53-
}
54-
55-
/**
56-
* Renders the grid filter with the custom text field to the header cell.
57-
*
58-
* @override
59-
*/
60-
_defaultHeaderRenderer(root, _column) {
61-
let filter = root.firstElementChild;
62-
let textField = filter ? filter.firstElementChild : undefined;
63-
64-
if (!filter) {
65-
filter = document.createElement('vaadin-grid-filter');
66-
textField = document.createElement('vaadin-text-field');
67-
textField.setAttribute('theme', 'small');
68-
textField.setAttribute('style', 'max-width: 100%;');
69-
textField.setAttribute('focus-target', '');
70-
textField.addEventListener('value-changed', this.__boundOnFilterValueChanged);
71-
filter.appendChild(textField);
72-
root.appendChild(filter);
73-
}
74-
75-
filter.path = this.path;
76-
filter.value = this._filterValue;
77-
78-
textField.__rendererValue = this._filterValue;
79-
textField.value = this._filterValue;
80-
textField.label = this.__getHeader(this.header, this.path);
81-
}
82-
83-
/**
84-
* The filter column doesn't allow to use a custom header renderer
85-
* to override the header cell content.
86-
* It always renders the grid filter to the header cell.
87-
*
88-
* @override
89-
*/
90-
_computeHeaderRenderer() {
91-
return this._defaultHeaderRenderer;
92-
}
93-
94-
/**
95-
* Updates the internal filter value once the filter text field is changed.
96-
* The listener handles only user-fired events.
97-
*
98-
* @private
99-
*/
100-
__onFilterValueChanged(e) {
101-
// Skip if the value is changed by the renderer.
102-
if (e.detail.value === e.target.__rendererValue) {
103-
return;
104-
}
105-
106-
this._filterValue = e.detail.value;
107-
}
108-
109-
/** @private */
110-
__getHeader(header, path) {
111-
if (header) {
112-
return header;
113-
}
114-
115-
if (path) {
116-
return this._generateHeader(path);
117-
}
118-
}
11932
}
12033

12134
defineCustomElement(GridFilterColumn);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
import { GridSelectionColumnBaseMixinClass } from './vaadin-grid-selection-column-base-mixin.js';
8+
9+
/**
10+
* Fired when the `selectAll` property changes.
11+
*/
12+
export type GridSelectionColumnSelectAllChangedEvent = CustomEvent<{ value: boolean }>;
13+
14+
export interface GridSelectionColumnCustomEventMap {
15+
'select-all-changed': GridSelectionColumnSelectAllChangedEvent;
16+
}
17+
18+
export interface GridSelectionColumnEventMap extends HTMLElementEventMap, GridSelectionColumnCustomEventMap {}
19+
20+
export declare function GridSelectionColumnMixin<TItem, T extends Constructor<HTMLElement>>(
21+
superclass: T,
22+
): Constructor<GridSelectionColumnMixinClass<TItem>> & T;
23+
24+
export declare class GridSelectionColumnMixinClass<TItem> extends GridSelectionColumnBaseMixinClass<TItem> {}

0 commit comments

Comments
 (0)