Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(table): add footerRender api (#2638) #2647

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/friendly-phones-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

Table feat: add footerRender api
5 changes: 5 additions & 0 deletions .changeset/smooth-ways-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/table": minor
---

feat: add footerRender api
3 changes: 2 additions & 1 deletion packages/ui/table/src/BaseTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ export const BaseTable = forwardRef<HTMLDivElement | null, BaseTableProps>(
canScroll,
bodyTableRef,
scrollWidth,
footerRender,
} = providedValue

const hasBorder = borderedProp ?? bordered
Expand Down Expand Up @@ -266,7 +267,7 @@ export const BaseTable = forwardRef<HTMLDivElement | null, BaseTableProps>(
: undefined
}
>
{extraFooter}
{isFunction(footerRender) ? footerRender(<>{extraFooter}</>) : extraFooter}
</div>
)

Expand Down
6 changes: 6 additions & 0 deletions packages/ui/table/src/use-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export const useTable = ({
showColMenu,
rowSelection,
cellRender,
footerRender,
fieldKey = 'key',
virtual,
...rootProps
Expand Down Expand Up @@ -633,6 +634,7 @@ export const useTable = ({
onColumnResizable,
isTree: isTreeView,
cellRender,
footerRender,
showColMenu,
onLoadChildren,
}
Expand Down Expand Up @@ -805,6 +807,10 @@ export interface UseTableProps {
* 点击异步加载子项
*/
onLoadChildren?: (item: TableRowEventData) => Promise<any[] | void> | void
/**
* 自定义渲染页脚
*/
footerRender?: (...nodes: React.ReactElement[]) => React.ReactNode
}

export type UseTableReturn = ReturnType<typeof useTable>
181 changes: 181 additions & 0 deletions packages/ui/table/stories/footer-render.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import React from 'react'
import Table from '../src'

/**
* @title 自定义页脚
*/
export const FooterRender = () => {
const [columns] = React.useState([
{
title: '商品名',
dataKey: 'name',
},
{
title: '品类',
dataKey: 'type',
},
{
title: '规格',
dataKey: 'size',
},
{
title: '单价',
dataKey: 'price',
},
{
title: '门店',
dataKey: 'address',
},
{
title: '库存',
dataKey: 'stock',
},
])

const [dataSource] = React.useState([
{
name: '小米9',
type: '手机',
size: '6G+64G 幻彩蓝',
price: '3299.00',
address: '华润五彩城店',
stock: '29,000',
key: 1,
},
{
name: '小米9 SE',
type: '手机',
size: '6G+64G 幻彩蓝',
price: '1999.00',
address: '清河店',
stock: '10,000',
key: 2,
},
{
name: '小米8',
type: '手机',
size: '6G+64G 幻彩蓝',
price: '2599.00',
address: '双安店',
stock: '12,000',
key: 3,
},
{
name: 'Redmi Note7',
type: '手机',
size: '6G+64G 幻彩蓝',
price: '999.00',
address: '华润五彩城店',
stock: '140,000',
key: 4,
},
{
name: '小米8 SE',
type: '手机',
size: '6G+64G 幻彩蓝',
price: '699.00',
address: '双安店',
stock: '12,000',
key: 5,
},
{
name: '小米10',
type: '手机',
size: '6G+64G 幻彩蓝',
price: '3299.00',
address: '华润五彩城店',
stock: '29,000',
key: 6,
},
{
name: '小米10 SE',
type: '手机',
size: '6G+64G 幻彩蓝',
price: '1999.00',
address: '清河店',
stock: '10,000',
key: 7,
},
{
name: '小米8',
type: '手机',
size: '6G+64G 幻彩蓝',
price: '2599.00',
address: '双安店',
stock: '12,000',
key: 8,
},
{
name: 'Redmi Note7',
type: '手机',
size: '6G+64G 幻彩蓝',
price: '999.00',
address: '华润五彩城店',
stock: '140,000',
key: 9,
},
{
name: '小米8 SE',
type: '手机',
size: '6G+64G 幻彩蓝',
price: '699.00',
address: '双安店',
stock: '12,000',
key: 10,
},
])

const [paginationState, setPaginationState] = React.useState({
current: 1,
data: dataSource.slice(0, 5),
})

const [selectedRowKeys, setSelectedRowKeys] = React.useState<any>([])

return (
<>
<h1>FooterRender for Table</h1>
<div className="table-footer-render__wrap" style={{ minWidth: 660 }}>
<Table
pagination={{
showTotal: true,
showJumper: true,
pageSize: 5,
total: dataSource.length,
current: paginationState.current,
onChange: (page, pre, size = 5) => {
console.log('onPaginationChange', page, pre, size)

setPaginationState({
current: page,
data: dataSource.slice(size * (page - 1), size * page),
})
},
}}
columns={columns}
data={paginationState.data}
rowSelection={{
selectedRowKeys,
onChange: (keys, target, shouldChecked, rows) => {
console.log(keys, target, shouldChecked, rows)

setSelectedRowKeys(keys)
},
}}
footerRender={(pagination) => {
return (
<div
style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}
>
<div>
已选 <span style={{ color: '#237ffa' }}>{selectedRowKeys.length}</span> 项
</div>
{pagination}
</div>
)
}}
/>
</div>
</>
)
}
1 change: 1 addition & 0 deletions packages/ui/table/stories/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export * from './on-load-children.stories'
export * from './expanded-render.stories'
export * from './async-expanded-render.stories'
export * from './row-selection.stories'
export * from './footer-render.stories'
export * from './merged-cell.stories'
export * from './pagination.stories'
export * from './data-source.stories'
Expand Down