123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- import React, { useState, useMemo } from 'react';
- import { Table, Button } from '@douyinfe/semi-ui/';
- import {IconArrowUp, IconArrowDown} from '@douyinfe/semi-icons';
- /**
- * Aligned to Ant Design APIs for migration, see [antd-demo](https://codesandbox.io/s/great-mcclintock-14fl7).
- */
- const Demo = () => {
- const childrenRecordName = 'children';
- const rowKey = 'key';
- const [expandedRowKeys, setExpandedRowKeys] = useState([1]);
- const [data, setData] = useState([
- {
- key: 1,
- name: 'John Brown sr.',
- age: 60,
- address: 'New York No. 1 Lake Park',
- children: [
- {
- key: 11,
- name: 'John Brown',
- age: 42,
- address: 'New York No. 2 Lake Park',
- },
- {
- key: 12,
- name: 'John Brown jr.',
- age: 30,
- address: 'New York No. 3 Lake Park',
- children: [
- {
- key: 121,
- name: 'Jimmy Brown',
- age: 16,
- address: 'New York No. 3 Lake Park',
- },
- ],
- },
- {
- key: 13,
- name: 'Jim Green sr.',
- age: 72,
- address: 'London No. 1 Lake Park',
- children: [
- {
- key: 131,
- name: 'Jim Green',
- age: 42,
- address: 'London No. 2 Lake Park',
- children: [
- {
- key: 1311,
- name: 'Jim Green jr.',
- age: 25,
- address: 'London No. 3 Lake Park',
- },
- {
- key: 1312,
- name: 'Jimmy Green sr.',
- age: 18,
- address: 'London No. 4 Lake Park',
- },
- ],
- },
- ],
- },
- ],
- },
- {
- key: 2,
- name: 'Joe Black',
- age: 32,
- address: 'Sidney No. 1 Lake Park',
- },
- ]);
- const switchRecord = (key1, key2) => {
- const newData = [...data];
- if (key1 != null && key2 != null) {
- const item1 = findRecordByKey(key1, newData);
- const item2 = findRecordByKey(key2, newData);
- // you have to copy item1 and item2 first
- const copiedItem1 = { ...item1 };
- const copiedItem2 = { ...item2 };
- coverRecord(item1, copiedItem2);
- coverRecord(item2, copiedItem1);
- setData(newData);
- }
- };
- const findRecordByKey = (key, data) => {
- if (Array.isArray(data) && data.length && key != null) {
- for (let item of data) {
- if (item[rowKey] === key) {
- return item;
- }
- const children = item[childrenRecordName];
- if (Array.isArray(children) && children.length) {
- const item = findRecordByKey(key, children);
- if (item != null) {
- return item;
- }
- }
- }
- }
- };
- const coverRecord = (obj, srcObj) => {
- if (obj && typeof obj === 'object' && srcObj && typeof srcObj === 'object') {
- const srcKeys = Object.keys(srcObj);
- const copied = { ...srcObj };
- Object.assign(obj, copied);
- Object.keys(obj).forEach(key => {
- if (!srcKeys.includes(key)) {
- delete obj[key];
- }
- });
- }
- return obj;
- };
- const getSameLevelRecords = (key, data = []) => {
- if (key != null && Array.isArray(data) && data.length) {
- if (data.find(item => item[rowKey] === key)) {
- return data;
- }
- for (let item of data) {
- const records = getSameLevelRecords(key, item[childrenRecordName]);
- if (records.length) {
- return records;
- }
- }
- }
- return [];
- };
- const columns = [
- {
- title: 'Name',
- dataIndex: 'name',
- key: 'name',
- width: 300,
- fixed: 'left',
- },
- {
- title: 'Age',
- dataIndex: 'age',
- key: 'age',
- width: 150,
- filterChildrenRecord: true,
- filters: [
- { text: 'age < 30', value: 30 },
- { text: 'age < 20', value: 20 },
- { text: 'age < 10', value: 10 },
- ],
- onFilter: (filteredValue, record) => {
- // console.log(`filteredValue: `, filteredValue, ` record: `, record);
- return record.age < filteredValue;
- },
- sorter: (v1, v2) => {
- return v1.age < v2.age ? -1 : 1;
- },
- sortChildrenRecord: true,
- },
- {
- title: 'Address',
- dataIndex: 'address',
- key: 'address',
- width: 300,
- },
- {
- key: 'operation',
- render: record => {
- const records = getSameLevelRecords(record[rowKey], data);
- const index = records.findIndex(item => item[rowKey] === record[rowKey]);
- const upProps = {};
- const downProps = {};
- if (index > 0) {
- const upRow = records[index - 1];
- upProps.onClick = () => switchRecord(record[rowKey], upRow[rowKey]);
- } else {
- upProps.disabled = true;
- }
- if (index < records.length - 1) {
- const downRow = records[index + 1];
- downProps.onClick = () => switchRecord(record[rowKey], downRow[rowKey]);
- } else {
- downProps.disabled = true;
- }
- return (
- <>
- <Button icon={<IconArrowUp />} {...upProps} />
- <Button icon={<IconArrowDown />} {...downProps} />
- </>
- );
- },
- },
- ];
- return (
- <Table
- columns={columns}
- rowKey={rowKey}
- childrenRecordName={childrenRecordName}
- expandedRowKeys={expandedRowKeys}
- onExpand={(expanded, row, event) => {
- console.log([expanded, row, event]);
- if (event) {
- event.stopPropagation();
- }
- }}
- onRow={(record, index) => {
- return {
- onClick: () => {
- console.log(`Clicked row ${index}: `, record);
- },
- };
- }}
- onExpandedRowsChange={rows => setExpandedRowKeys(rows.map(item => item[rowKey]))}
- rowSelection={{
- getCheckboxProps: record => ({
- disabled: record.name && record.name.includes('Jimmy'), // Column configuration not to be checked
- }),
- fixed: 'left',
- }}
- dataSource={data}
- // indentSize={0}
- // expandIcon={false}
- scroll={{
- x: '160%',
- y: 480,
- }}
- />
- );
- };
- export default Demo;
|