-
-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
610 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,88 @@ | ||
import React from 'react'; | ||
import { Resizable } from 'react-resizable'; | ||
import React, { useState } from 'react'; | ||
import Table from 'rc-table'; | ||
import '../../assets/index.less'; | ||
import 'react-resizable/css/styles.css'; | ||
import type { ColumnType } from '@/interface'; | ||
|
||
const ResizableTitle = props => { | ||
const { onResize, width, ...restProps } = props; | ||
const data = [ | ||
{ a: '123', b: 'xxxxxxxx xxxxxxxx', d: 3, key: '1' }, | ||
{ a: 'cdd', b: 'edd12221 edd12221', d: 3, key: '2' }, | ||
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '3' }, | ||
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '4' }, | ||
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '5' }, | ||
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '6' }, | ||
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '7' }, | ||
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '8' }, | ||
{ a: '133', c: 'edd12221 edd12221', d: 2, key: '9' }, | ||
]; | ||
|
||
if (!width) { | ||
return <th {...restProps} />; | ||
} | ||
const Demo = () => { | ||
const [widthMap, setWidthMap] = useState<Map<React.Key, number>>(new Map()); | ||
|
||
const columns1 = [ | ||
{ title: 'title1', dataIndex: 'aaa', key: 'aaa', width: 100 }, | ||
{ title: 'title2', dataIndex: 'bbb', key: 'bbb', width: 100 }, | ||
].map(i => ({ | ||
...i, | ||
width: widthMap.get(i.key ?? i.dataIndex) ?? i.width, | ||
})); | ||
|
||
const columns2 = [ | ||
{ title: 'title1', dataIndex: 'a', key: 'a', fixed: 'left' }, | ||
{ title: 'title2', dataIndex: 'b', key: 'b', fixed: 'left' }, | ||
{ title: 'title3', dataIndex: 'c', key: 'c' }, | ||
{ title: 'title4', dataIndex: 'b', key: 'd' }, | ||
{ title: 'title5', dataIndex: 'b', key: 'e' }, | ||
{ title: 'title6', dataIndex: 'b', key: 'f' }, | ||
{ title: 'title7', dataIndex: 'b', key: 'g' }, | ||
{ title: 'title8', dataIndex: 'b', key: 'h' }, | ||
{ title: 'title9', dataIndex: 'b', key: 'i' }, | ||
{ title: 'title10', dataIndex: 'b', key: 'j' }, | ||
{ title: 'title11', dataIndex: 'b', key: 'k', fixed: 'right' }, | ||
{ title: 'title12', dataIndex: 'b', key: 'l', fixed: 'right' }, | ||
].map(i => ({ | ||
...i, | ||
width: widthMap.get(i.key ?? i.dataIndex) ?? 150, | ||
})); | ||
|
||
return ( | ||
<Resizable width={width} height={0} onResize={onResize}> | ||
<th {...restProps} /> | ||
</Resizable> | ||
<div> | ||
table width: 800px {'columns=[{width: 100, width: 100}]'} 情况 | ||
<Table | ||
columnResizable | ||
style={{ width: 800 }} | ||
scroll={{ y: 300, x: columns1.reduce((t, c) => t + c.width, 0) }} | ||
columns={columns1} | ||
data={data} | ||
onColumnResizeComplete={({ columnKeyWidths }) => { | ||
setWidthMap(prev => { | ||
const result = new Map(prev); | ||
columnKeyWidths.forEach(i => { | ||
result.set(i.columnKey, i.width); | ||
}); | ||
return result; | ||
}); | ||
}} | ||
/> | ||
<br /> | ||
大多数情况 | ||
<Table | ||
columnResizable | ||
style={{ width: 800 }} | ||
scroll={{ y: 300, x: columns2.reduce((t, c) => t + c.width, 0) }} | ||
columns={columns2} | ||
data={data} | ||
onColumnResizeComplete={({ columnKeyWidths }) => { | ||
setWidthMap(prev => { | ||
const result = new Map(prev); | ||
columnKeyWidths.forEach(i => { | ||
result.set(i.columnKey, i.width); | ||
}); | ||
return result; | ||
}); | ||
}} | ||
/> | ||
{/* <Table resizable style={{ width: 800 }} columns={columns} data={data} /> */} | ||
</div> | ||
); | ||
}; | ||
|
||
interface RecordType { | ||
a: string; | ||
b?: string; | ||
c?: string; | ||
d?: number; | ||
key: string; | ||
} | ||
|
||
interface DemoState { | ||
columns: ColumnType<RecordType>[]; | ||
} | ||
|
||
class Demo extends React.Component<{}, DemoState> { | ||
state: DemoState = { | ||
columns: [ | ||
{ title: 'title1', dataIndex: 'a', key: 'a', width: 100 }, | ||
{ title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, | ||
{ title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, | ||
{ | ||
title: 'Operations', | ||
dataIndex: '', | ||
key: 'd', | ||
render() { | ||
return <a href="#">Operations</a>; | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
components = { | ||
header: { | ||
cell: ResizableTitle, | ||
}, | ||
}; | ||
|
||
data = [ | ||
{ a: '123', key: '1' }, | ||
{ a: 'cdd', b: 'edd', key: '2' }, | ||
{ a: '1333', c: 'eee', d: 2, key: '3' }, | ||
]; | ||
|
||
handleResize = | ||
index => | ||
(e, { size }) => { | ||
this.setState(({ columns }) => { | ||
const nextColumns = [...columns]; | ||
nextColumns[index] = { | ||
...nextColumns[index], | ||
width: size.width, | ||
}; | ||
return { columns: nextColumns }; | ||
}); | ||
}; | ||
|
||
render() { | ||
const columns = this.state.columns.map((col, index) => ({ | ||
...col, | ||
onHeaderCell: (column: ColumnType<RecordType>) => | ||
({ | ||
width: column.width, | ||
onResize: this.handleResize(index), | ||
}) as any, | ||
})); | ||
|
||
return ( | ||
<div> | ||
<h2>Integrate with react-resizable</h2> | ||
<Table components={this.components} columns={columns} data={this.data} /> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default Demo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.