-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathRoot.svelte
More file actions
139 lines (125 loc) · 4.49 KB
/
Root.svelte
File metadata and controls
139 lines (125 loc) · 4.49 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<script lang="ts">
import type { Column, RootProp } from './index.js';
import Row from './row/Base.svelte';
export let columns: Array<Column> | number;
export let allowSelection: boolean = false;
export let selectedRows: Array<string> = [];
export let maxRows: number = Infinity;
export let stickyHeader: boolean = false;
export let cellHeight: string = '40px';
let availableIds: Set<string> = new Set();
function createGridTemplateColumns(cols: typeof columns) {
if (typeof cols === 'number') {
return `repeat(${cols}, 1fr)`;
}
const columns = cols.filter((column) => column.hide !== true);
const hasOnlyMaxWidth = columns.every(
(column) => typeof column.width === 'number' || (column.width && 'max' in column.width)
);
return columns.reduce(
(acc, column) => {
if (column.width === undefined) return `${acc} 1fr`;
if (typeof column.width === 'number') {
return `${acc} ${column.width}px`;
}
if ('min' in column.width && 'max' in column.width) {
if (hasOnlyMaxWidth) {
return `${acc} minmax(${column.width.min}px, 1fr)`;
}
return `${acc} minmax(${column.width.min}px, ${column.width.max}px)`;
}
if ('min' in column.width) {
return `${acc} minmax(${column.width.min}px, 1fr)`;
}
return acc;
},
allowSelection ? ` ${cellHeight}px` : '' // Default width for selection column
);
}
function groupById(cols: typeof columns): RootProp['columns'] {
if (typeof cols === 'number') {
return {};
}
return cols.reduce<Record<Column['id'], Column>>((acc, column) => {
acc[column.id] = column;
return acc;
}, {});
}
function toggleAll() {
if (allRowsSelected) {
selectedRows = selectedRows.filter((row) => !availableIds.has(row));
} else {
selectedRows = [
...selectedRows,
...[...availableIds].filter((row) => !selectedRows.includes(row))
];
}
}
function toggle(id: string) {
if (selectedRows.includes(id)) {
selectedRows = selectedRows.filter((row) => row !== id);
} else {
selectedRows = [...selectedRows, id];
}
}
function addAvailableId(id: string) {
availableIds.add(id);
availableIds = availableIds;
}
function removeAvailableId(id: string) {
availableIds.delete(id);
availableIds = availableIds;
}
function calculateMaxHeight(maxRows: number, cellHeight: string, hasHeader: boolean) {
if (maxRows === Infinity) return undefined;
const totalRows = hasHeader ? maxRows + 1 : maxRows;
return `calc(${totalRows} * ${cellHeight})`;
}
$: someRowsSelected =
availableIds.size > 0 &&
selectedRows.length > 0 &&
selectedRows.some((row) => availableIds.has(row));
$: allRowsSelected =
availableIds.size > 0 && [...availableIds].every((row) => selectedRows.includes(row));
$: root = {
allowSelection,
selectedRows,
columns: groupById(columns),
toggleAll,
toggle,
selectedSome: someRowsSelected,
selectedNone: !someRowsSelected,
selectedAll: allRowsSelected,
addAvailableId,
removeAvailableId,
cellHeight,
hasHeader: $$slots.header !== undefined
} as RootProp;
$: maxHeight = calculateMaxHeight(maxRows, cellHeight, root.hasHeader);
</script>
<div class="root" style:max-height={maxHeight}>
<div role="table" style:--grid-template-columns={createGridTemplateColumns(columns)}>
{#if root.hasHeader}
<Row type="header" {root} sticky={stickyHeader}>
<slot name="header" {root} />
</Row>
{/if}
<slot {root} />
</div>
</div>
<style lang="scss">
.root {
overflow: auto;
border: 1px solid var(--border-neutral);
border-radius: var(--border-radius-s);
background: var(--bgcolor-neutral-primary);
::-webkit-scrollbar {
display: none;
}
[role='table'] {
display: grid;
grid-template-columns: var(--grid-template-columns);
width: 100%;
}
}
</style>