-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from purple-force/master
feat: 补充内容
- Loading branch information
Showing
3 changed files
with
139 additions
and
0 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
131 changes: 131 additions & 0 deletions
131
playground/app/routes/_layout+/row-selection-order/index.tsx
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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { useMemo, useState } from 'react' | ||
import { Button, Table, type TableColumnsType, Tag } from 'antd' | ||
import { type ResizableColumnsType, useAntdResizableHeader } from 'use-antd-resizable-header' | ||
|
||
interface DataType { | ||
key: string | ||
name: string | ||
age: number | ||
address: string | ||
tags: string[] | ||
} | ||
|
||
type Columns = ResizableColumnsType<TableColumnsType<DataType>> | ||
|
||
const data: DataType[] = [ | ||
{ | ||
key: '1', | ||
name: 'John Brown', | ||
age: 32, | ||
address: 'New York No. 1 Lake Park', | ||
tags: ['nice', 'developer'], | ||
}, | ||
{ | ||
key: '2', | ||
name: 'Jim Green', | ||
age: 42, | ||
address: 'London No. 1 Lake Park', | ||
tags: ['loser'], | ||
}, | ||
{ | ||
key: '3', | ||
name: 'Joe Black', | ||
age: 32, | ||
address: 'Sydney No. 1 Lake Park', | ||
tags: ['cool', 'teacher'], | ||
}, | ||
] | ||
|
||
export function Component() { | ||
const [times, setTimes] = useState(0) | ||
|
||
const columns: Columns = [ | ||
{ | ||
title: 'Name', | ||
width: 300, | ||
dataIndex: 'name', | ||
key: 'name', | ||
// 设置拖拽最小宽度 | ||
minConstraints: 20, | ||
render: (text) => <a>{text}</a>, | ||
}, | ||
Table.SELECTION_COLUMN, | ||
{ | ||
title: 'Age', | ||
dataIndex: 'age', | ||
key: 'age', | ||
width: 100, | ||
}, | ||
{ | ||
title: 'Address', | ||
dataIndex: 'address', | ||
key: 'address', | ||
width: 100, | ||
}, | ||
{ | ||
title: 'Tags', | ||
key: 'tags', | ||
dataIndex: 'tags', | ||
render: (_, { tags }) => ( | ||
<> | ||
{tags.map((tag) => { | ||
let color = tag.length > 5 ? 'geekblue' : 'green' | ||
if (tag === 'loser') { | ||
color = 'volcano' | ||
} | ||
return ( | ||
<Tag color={color} key={tag}> | ||
{tag.toUpperCase()} | ||
</Tag> | ||
) | ||
})} | ||
</> | ||
), | ||
width: 300, | ||
}, | ||
{ | ||
title: 'Action', | ||
key: 'action', | ||
render: (_, record) => ( | ||
<div className={'w-full truncate space-x-1'}> | ||
<span>{record.name}</span> | ||
<a | ||
onClick={() => { | ||
setTimes((t) => t + 1) | ||
}} | ||
> | ||
Render | ||
</a> | ||
<span>{times}</span> | ||
</div> | ||
), | ||
}, | ||
] | ||
|
||
const { components, resizableColumns, tableWidth, resetColumns } = useAntdResizableHeader({ | ||
columns: useMemo(() => columns, [times]), | ||
columnsState: { | ||
persistenceKey: 'row-selection-order', | ||
persistenceType: 'sessionStorage', | ||
}, | ||
}) | ||
|
||
return ( | ||
<div className='App'> | ||
<Table | ||
columns={resizableColumns} | ||
components={components} | ||
dataSource={data} | ||
scroll={{ x: tableWidth }} | ||
rowSelection={{}} | ||
/> | ||
<Button | ||
onClick={() => { | ||
resetColumns() | ||
}} | ||
> | ||
Reset | ||
</Button> | ||
</div> | ||
) | ||
} |
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