框架:React
UI:antd
import { useEffect, useState } from "react"
import type { ColumnsType } from 'antd/es/table'
import { EditOutlined, MinusCircleOutlined, InfoCircleOutlined } from '@ant-design/icons'
interface DataType {
user_name: string
user_role: number
}
export default function Table() {
const [showIcon, setShowIcon] = useState(false)
const [iconIndex, setIconIndex] = useState(-1)
const columns: ColumnsType<DataType> = [
{
title: 'Name',
dataIndex: 'user_name',
render: (user_name, record, index) => (
<Space>
<span>{user_name}</span>
<InfoCircleOutlined style={{ visibility: showIcon && index == iconIndex ? 'visible' : 'hidden' }} onClick={() => toDetailModal(user_name)} />
</Space>
)
},
{
title: 'Role',
dataIndex: 'user_role',
render: (user_role, record, index) => (
<Space>
<span>{user_role === 0 ? 'Admin' : 'Viewer'}</span>
<EditOutlined style={{ visibility: showIcon && index == iconIndex ? 'visible' : 'hidden' }} onClick={() => handleRoleUpdate(record)} />
</Space>
)
},
{
title: '',
key: 'action',
width: 200,
render: (_, record) => (
<MinusCircleOutlined onClick={() => handleDelete(record)} />
),
},
]
const hoverRow = (record: any, index: any) => {
setShowIcon(true)
setIconIndex(index)
}
return (<Table
onRow={(record, index) => {
return {
onMouseEnter: () => { hoverRow(record, index) },
onMouseLeave: () => setShowIcon(false),
}
}}
columns={columns}
dataSource={yourDataSource} />)
}