Skip to content

Commit c2698a8

Browse files
authored
[website] update components demo (#29)
1 parent 8a5b1f5 commit c2698a8

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

website/src/examples/components.js

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,56 +8,47 @@ const fixedColumns = columns.map((column, columnIndex) => {
88
return { ...column, frozen }
99
})
1010

11-
const expandColumnKey = 'column-1'
11+
fixedColumns[0].format = 'checkbox'
12+
fixedColumns[1].format = 'contact'
1213

13-
// add some sub items
14-
data.forEach((rowData, rowIndex) => {
15-
const cellData = rowData[expandColumnKey]
16-
rowData[expandColumnKey] = `Group ${rowIndex}`
17-
for (let i = 0; i < 3; i++) {
18-
const subData = {
19-
...rowData,
20-
id: `${rowData.id}-sub-${i}`,
21-
parentId: rowData.id,
22-
[expandColumnKey]: `Sub Group ${i}`,
23-
}
24-
data.push(subData)
14+
const Contact = styled.div`
15+
font-weight: 700;
16+
color: orange;
17+
`
2518

26-
const subSubData = {
27-
...subData,
28-
id: `${subData.id}-sub-sub-${i}`,
29-
parentId: subData.id,
30-
[expandColumnKey]: cellData,
31-
}
32-
data.push(subSubData)
33-
}
34-
})
19+
const stringRenderer = ({ className, cellData }) => (
20+
<div className={className}>{cellData}</div>
21+
)
22+
const checkboxRenderer = ({ rowIndex }) => (
23+
<input type="checkbox" checked={rowIndex % 2 === 0} />
24+
)
25+
const contactRenderer = ({ cellData }) => <Contact>{cellData}</Contact>
3526

36-
const treeData = unflatten(data)
27+
const renderers = {
28+
string: stringRenderer,
29+
checkbox: checkboxRenderer,
30+
contact: contactRenderer,
31+
}
32+
33+
const Cell = cellProps => {
34+
const format = cellProps.column.format || 'string'
35+
const renderer = renderers[format] || renderers.string
3736

38-
const GroupCell = ({ cellData, rowData, column, className }) => {
39-
if (
40-
rowData.children &&
41-
rowData.children.length &&
42-
column.key !== expandColumnKey
43-
)
44-
return null
45-
return <div className={className}>{cellData}</div>
37+
return renderer(cellProps)
4638
}
4739

4840
const components = {
49-
TableCell: GroupCell,
41+
TableCell: Cell,
5042
}
5143

52-
const rowStyle = ({ rowData }) =>
53-
rowData.children && rowData.children.length && { backgroundColor: '#f7f9fa' }
44+
const expandColumnKey = 'column-1'
45+
const treeData = unflatten(data)
5446

5547
export default () => (
5648
<Table
5749
fixed
5850
columns={fixedColumns}
5951
data={treeData}
60-
rowStyle={rowStyle}
6152
expandColumnKey={expandColumnKey}
6253
onExpandedRowsChange={action('onExpandedRowsChange')}
6354
components={components}

0 commit comments

Comments
 (0)