-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathrender-cell.tsx
More file actions
133 lines (115 loc) · 3.54 KB
/
render-cell.tsx
File metadata and controls
133 lines (115 loc) · 3.54 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
import type { FC } from 'react';
import React from 'react';
import { createReactRenderElement, KolButton, KolInputText, KolTableStateful } from '@public-ui/react-v19';
import { getRoot } from '../../shares/react-roots';
import { SampleDescription } from '../SampleDescription';
import { DATE_FORMATTER } from './formatter';
import type { IconsPropType, KoliBriTableCell, KoliBriTableHeaders } from '@public-ui/components';
import { useToasterService } from '../../hooks/useToasterService';
type Data = {
order: number;
date: Date;
shipped: boolean;
};
/**
* Generates n data entries with random dates and shipping status
* @param n - Number of data entries to generate
* @returns Array of Data objects
*/
function generateDataEntries(n: number): Data[] {
const entries: Data[] = [];
const startDate = new Date('1970-01-01').getTime();
const endDate = new Date('2025-12-31').getTime();
for (let i = 0; i < n; i++) {
const randomTimestamp = startDate + Math.random() * (endDate - startDate);
entries.push({
order: i,
shipped: Math.random() > 0.5,
date: new Date(randomTimestamp),
});
}
return entries;
}
const DATA: Data[] = generateDataEntries(100);
function KolButtonWrapper({ label, icons }: { label: string; icons: IconsPropType }) {
const { dummyClickEventHandler } = useToasterService();
const dummyEventHandler = {
onClick: dummyClickEventHandler,
};
return <KolButton _label={label} _icons={icons} _on={dummyEventHandler} />;
}
const HEADERS: KoliBriTableHeaders = {
horizontal: [
[
{
label: '#',
key: 'order',
textAlign: 'center',
width: 100,
/* Example 1: Use render return value to format data */
render: (_el, cell: KoliBriTableCell) => {
const { label } = cell as { label: string };
return `Index: ${label}`;
},
},
{
label: 'Status',
key: 'shipped',
textAlign: 'center',
width: 100,
/* Example 2: Simple render function using textContent */
render: (el, cell: KoliBriTableCell) => {
const { label } = cell as { label: string };
const element = el as HTMLElement;
if (label) {
element.textContent = `Order has been dispatched 🚚`;
} else {
element.textContent = `Order pending 📦`;
}
},
},
{
label: 'Date (string)',
key: 'date',
width: 200,
textAlign: 'center',
/* Example 3: Render function using innerHTML. ⚠️Make sure to sanitize data to avoid XSS. */
render: (el, cell: KoliBriTableCell) => {
const { label } = cell as { label: string };
(el as HTMLElement).innerHTML = `<strong>${DATE_FORMATTER.format(label as unknown as Date)}</strong>`;
},
compareFn: (data0, data1) => (data0 as Data).date.getTime() - (data1 as Data).date.getTime(),
},
{
label: 'Action (react)',
key: 'action',
width: 200,
/* Example 4: Render function using React */
render: (el) => {
getRoot(createReactRenderElement(el)).render(
<div
style={{
display: `grid`,
gridAutoFlow: `column`,
alignItems: `end`,
gap: `1rem`,
maxWidth: `400px`,
}}
>
<KolInputText _label="Input" />
<KolButtonWrapper label="Save" icons={{ left: 'kolicon-check' }} />
</div>,
);
},
},
],
],
};
export const TableRenderCell: FC = () => (
<>
<SampleDescription>
<p>This sample shows KolTableStateful using React render functions for the cell contents.</p>
</SampleDescription>
<KolTableStateful _label="Sort by date column" _data={DATA} _headers={HEADERS} className="w-full" _hasSettingsMenu />
</>
);