-
Notifications
You must be signed in to change notification settings - Fork 5
/
cosmoz-omnitable-column.js
100 lines (91 loc) · 2.71 KB
/
cosmoz-omnitable-column.js
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
/* eslint-disable no-return-assign */
import '@neovici/cosmoz-input';
import './ui-helpers/cosmoz-clear-button';
import {
applySingleFilter,
columnMixin,
getString,
} from './cosmoz-omnitable-column-mixin';
import { PolymerElement } from '@polymer/polymer/polymer-element';
import { html } from 'lit-html';
const onChange = (setState) => (event) =>
setState((state) => {
// skip the event emitted during cosmoz-input initialization
if (state.inputValue === undefined && event.target.value === '') {
return state;
}
clearTimeout(state.t);
const t = setTimeout(
() => setState((state) => ({ ...state, filter: state.inputValue })),
1000,
);
return { ...state, inputValue: event.target.value, t };
}),
onBlur = (setState) => () =>
setState((state) => ({ ...state, filter: state.inputValue })),
onKeyDown = (setState) => (event) => {
if (event.keyCode === 13) {
event.preventDefault();
setState((state) => ({ ...state, filter: state.inputValue }));
}
},
onFocus = (setState) => (event) =>
setState((state) => ({ ...state, headerFocused: event.detail.value })),
resetFilter = (setState) => () =>
setState((state) => ({ ...state, filter: null, inputValue: null })),
hasFilter = (filter) => filter != null && filter !== '';
/**
* @polymer
* @customElement
* @appliesMixin columnMixin
*/
class OmnitableColumn extends columnMixin(PolymerElement) {
static get properties() {
return {
minWidth: { type: String, value: '55px' },
editMinWidth: { type: String, value: '55px' },
inputValue: { type: Object, notify: true },
};
}
getFilterFn(column, filter) {
if (filter == null || filter === '') {
return;
}
return applySingleFilter(column, filter);
}
renderCell(column, { item }) {
return html`<span class="default-column">${getString(column, item)}</span>`;
}
renderEditCell(column, { item }, onItemChange) {
const onChange = (event) => onItemChange(event.target.value);
return html`<cosmoz-input
no-label-float
type="text"
@change=${onChange}
.value=${getString(column, item)}
></cosmoz-input>`;
}
renderHeader(column, { filter, inputValue, headerFocused }, setState) {
return html`<cosmoz-input
label=${column.title}
.value=${inputValue ?? filter}
@value-changed=${onChange(setState)}
focused=${headerFocused}
@focused-changed=${onFocus(setState)}
@keydown=${onKeyDown(setState)}
@blur=${onBlur(setState)}
>
<cosmoz-clear-button
suffix
slot="suffix"
?visible=${hasFilter(filter)}
light
@click=${resetFilter(setState)}
></cosmoz-clear-button>
</cosmoz-input>`;
}
legacyFilterToState(filter) {
return { filter, inputValue: filter };
}
}
customElements.define('cosmoz-omnitable-column', OmnitableColumn);