-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathTable.test.tsx
147 lines (126 loc) · 3.1 KB
/
Table.test.tsx
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
140
141
142
143
144
145
146
147
import React from 'react';
import Table, { TableInstance } from '../src/Table';
import Cell from '../src/Cell';
import Column from '../src/Column';
import HeaderCell from '../src/HeaderCell';
import { expectType } from 'ts-expect';
interface ItemDataType<T = number | string> extends Record<string, any> {
label?: React.ReactNode;
value?: T;
groupBy?: string;
parent?: ItemDataType<T>;
children?: ItemDataType<T>[];
loading?: boolean;
}
type Row = {
id: number;
name: string;
};
const data: Row[] = [
{ id: 1, name: 'First' },
{ id: 2, name: 'Second' }
];
<Table
data={data}
rowHeight={row => {
expectType<Row | undefined>(row);
return 44;
}}
rowClassName={row => {
expectType<Row>(row);
return '';
}}
renderTreeToggle={(button, row) => {
expectType<Row | undefined>(row);
return null;
}}
renderRowExpanded={row => {
expectType<Row | undefined>(row);
return null;
}}
renderRow={(node, row) => {
expectType<Row | undefined>(row);
return null;
}}
onRowClick={row => {
expectType<Row>(row);
}}
onRowContextMenu={row => {
expectType<Row>(row);
}}
onExpandChange={(expanded, row) => {
expectType<Row>(row);
}}
/>;
// It should be possible to call instance methods via ref
const ref = React.createRef<TableInstance<Row, string>>();
<Table ref={ref} />;
<Table ref={ref}>
{({ Column, HeaderCell, Cell }) => (
<>
<Column>
<HeaderCell>Id</HeaderCell>
<Cell>{({ id }) => id}</Cell>
</Column>
</>
)}
</Table>;
ref.current?.body;
ref.current?.root;
ref.current?.scrollLeft(100);
ref.current?.scrollTop(100);
interface InventoryItem {
id: string;
name: string;
}
const table = React.createRef<TableInstance<InventoryItem, string>>();
<Table<InventoryItem, string> ref={table}>
{({ Column, HeaderCell, Cell, ColumnGroup }) => (
<>
<Column>
<HeaderCell>Name</HeaderCell>
<Cell>{row => row.name}</Cell>
</Column>
<Column>
<HeaderCell>Type</HeaderCell>
{/** @ts-expect-error Property 'type' does not exist on type 'InventoryItem' */}
<Cell>{row => row.type}</Cell>
</Column>
<ColumnGroup>
<Column>
<HeaderCell>Id</HeaderCell>
<Cell>{row => row.id}</Cell>
</Column>
<Column>
<HeaderCell>Name</HeaderCell>
<Cell>{row => row.name}</Cell>
</Column>
</ColumnGroup>
</>
)}
</Table>;
interface ImageCellProps<TKey extends string, TRow extends Record<TKey, string>> {
rowData: TRow;
dataKey: TKey;
// ... any other props
}
export const ImageCell = <TKey extends string, TRow extends Record<TKey, string>>({
rowData,
dataKey,
...rest
}: ImageCellProps<TKey, TRow>) => (
<Cell<TRow, TKey> {...rest}>
<img src={rowData[dataKey]} width="50" />
</Cell>
);
const rows: ItemDataType[] = [
{ id: 1, name: 'First' },
{ id: 2, name: 'Second' }
];
// test case for https://github.com/rsuite/rsuite-table/issues/422
<Table data={rows}>
<Column width={50}>
<HeaderCell>Id</HeaderCell>
<Cell>{rowData => rowData.id}</Cell>
</Column>
</Table>;