2022-01-06 15:45:35 +01:00
|
|
|
import type {Cash} from 'cash-dom/dist/cash';
|
2022-01-27 10:10:45 +01:00
|
|
|
import {inRange} from 'lodash-es';
|
2022-01-06 15:45:35 +01:00
|
|
|
import type {
|
|
|
|
Vnode,
|
|
|
|
VnodeDOM
|
|
|
|
} from 'mithril';
|
2021-11-23 21:24:29 +01:00
|
|
|
|
2022-01-06 15:45:35 +01:00
|
|
|
import Component from '../Component';
|
2021-09-07 13:28:20 +02:00
|
|
|
|
2022-01-06 15:45:35 +01:00
|
|
|
declare global {
|
|
|
|
namespace JSX {
|
|
|
|
interface IntrinsicElements {
|
|
|
|
TableCell: TableCell;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-14 10:27:19 +01:00
|
|
|
|
2022-01-06 15:45:35 +01:00
|
|
|
type Attributes = {type?: string};
|
|
|
|
|
|
|
|
export default class TableCell extends Component<Attributes> {
|
|
|
|
view(vnode: Vnode) {
|
2021-11-22 19:22:54 +01:00
|
|
|
this.attrs.addClassNames('mdc-data-table__cell', {
|
2022-01-06 15:45:35 +01:00
|
|
|
[`mdc-data-table__cell--${this.attrs.get('type') as string}`]: this.attrs.has(
|
|
|
|
'type'
|
|
|
|
)
|
2021-11-22 19:22:54 +01:00
|
|
|
});
|
|
|
|
|
2022-01-06 15:45:35 +01:00
|
|
|
if (
|
|
|
|
(!Array.isArray(vnode.children) || vnode.children.length === 0)
|
|
|
|
&& this.attrs.get('type') === 'checkbox'
|
|
|
|
) {
|
|
|
|
vnode.children = [<mwc-checkbox key="checkbox" className="mdc-data-table__row-checkbox" />];
|
2021-11-22 19:22:54 +01:00
|
|
|
}
|
|
|
|
|
2021-09-07 13:28:20 +02:00
|
|
|
return <td {...this.attrs.all()}>{vnode.children}</td>;
|
|
|
|
}
|
2021-11-23 21:24:29 +01:00
|
|
|
|
2022-01-06 15:45:35 +01:00
|
|
|
oncreate(vnode: VnodeDOM<Attributes>) {
|
2021-11-23 21:24:29 +01:00
|
|
|
super.oncreate(vnode);
|
|
|
|
|
|
|
|
const checkboxes = (): Cash => $(this.element)
|
|
|
|
.closest('.mdc-data-table')
|
|
|
|
.find('tbody tr[checkable] mwc-checkbox');
|
|
|
|
|
|
|
|
const cell: Cash = $(this.element);
|
2021-12-14 10:27:19 +01:00
|
|
|
cell.children('mwc-checkbox').on('change', () => {
|
2021-11-23 21:24:29 +01:00
|
|
|
const row = cell.parent();
|
|
|
|
row.toggleClass('mdc-data-table__row--selected');
|
2022-01-06 15:45:35 +01:00
|
|
|
const headerCheckbox = cell
|
|
|
|
.closest('.mdc-data-table')
|
|
|
|
.find('thead th mwc-checkbox');
|
2021-11-23 21:24:29 +01:00
|
|
|
const checks = checkboxes();
|
|
|
|
const checked = checks.filter('[checked]');
|
|
|
|
|
|
|
|
if (inRange(checked.length, 1, checks.length)) {
|
|
|
|
headerCheckbox.prop('indeterminate', true);
|
|
|
|
headerCheckbox.prop('checked', false);
|
|
|
|
} else {
|
|
|
|
headerCheckbox.prop('checked', checks.length === checked.length);
|
|
|
|
headerCheckbox.prop('indeterminate', false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-09-07 13:28:20 +02:00
|
|
|
}
|