mirror of
https://github.com/zhigang1992/react.git
synced 2026-01-29 22:51:54 +08:00
* feat(snippet): add custom symbol and toast * docs(snippet): add docs for custom symbol and toast * fix(snippet): default toast type as normal type * test(snippet): add custom symbol and toast * docs(snippet): add enum type of APIs * docs(toast): add normal-types * chore: release v1.8.0-canary.1 * feat(modal): optimize the animation of layer * test: update snapshots * chore: release v1.8.0-canary.2 * docs: format import statement * feat(modal): optimize the max width, height and alignment of layer * test: update snapshots * chore: release v1.8.0-canary.3 * Button (#319) * feat(button): center the icon if the button is empty * test(button): add testcase for icon only * docs(button): add example for icon only * chore: release v1.8.0-canary.4 * fix(snippet): remove space when the symbol is empty (#325) * fix(snippet): remove space when the symbol is empty style(snippet): remove unnecessary escape symbols * fix(snippet): ignore spaces in symbol style: fix lint warning * chore: release v1.8.0-canary.5 * feat(tabs): sync the label and set value to required (#334) * feat(tabs): sync the label and set value to required * test(tabs): add testcase for label sync * docs(tabs): update value to required * feat(modal): use Button to reconstrust Modal.Action (#332) * feat(modal): use Button to reconstrust Modal.Action * docs(modal): add example for action loading * test: update snapshots * chore: release v1.8.0-canary.6 * fix(input): always synchronize external value and ignore changes (#336) * fix(input): always synchronize external value and ignore changes * feat(input): support imperative API to update * fix(textarea): imperative api same as input (#341) * feat(dropdown): allow dropdown to set the specified container (#344) * feat(dropdown): allow dropdown to set the specified container * test(modal): update snapshots * docs(select): add example for custom popup container * fix(dropdown): fix type of getPopupContainer * test(dropdown): add testcase for specified container rendering * chore: release v1.8.0-canary.7 * fix(link): fix hard-coded background color (#347) * style(description): fix title font-weight (#348) * docs(link): fix duplicate examples (#346) * style(popover-item): the whole item should be clickable when using with link (#345) * fix(modal): fix type of Modal.Action (#351) * chore: release v1.8.0-canary.8 * feat(modal): lock tab action inside modal (#354) * feat(button): add style to focus buttons * feat(collections): add util function * feat(modal): lock tab action inside modal * test(modal): add tests for modal focus * test: update style of button * fix(table): fix column's props are not tracked (#362) * chore: release v1.8.0-canary.9 * fix(table): children of column should be kept in sync (#364) * chore: release v1.8.0-canary.10 Co-authored-by: Augusto <augustoconti10@gmail.com> Co-authored-by: yqrashawn <namy.19@gmail.com> Co-authored-by: Zhao Lei <firede@firede.us>
116 lines
3.1 KiB
TypeScript
116 lines
3.1 KiB
TypeScript
import React, { useEffect, useMemo, useRef, useState } from 'react'
|
|
import TableColumn from './table-column'
|
|
import TableHead from './table-head'
|
|
import TableBody from './table-body'
|
|
import useRealShape from '../utils/use-real-shape'
|
|
import useResize from '../utils/use-resize'
|
|
import { TableContext, TableColumnItem, TableConfig } from './table-context'
|
|
import useCurrentState from '../utils/use-current-state'
|
|
|
|
export type TableOnRow = (row: any, index: number) => void
|
|
export type TableOnCell = (cell: any, index: number, colunm: number) => void
|
|
export type TableOnChange = (data: any) => void
|
|
|
|
interface Props {
|
|
data?: Array<any>
|
|
emptyText?: string
|
|
hover?: boolean
|
|
onRow: TableOnRow
|
|
onCell: TableOnCell
|
|
onChange: TableOnChange
|
|
className?: string
|
|
}
|
|
|
|
const defaultProps = {
|
|
hover: true,
|
|
emptyText: '',
|
|
onRow: (() => {}) as TableOnRow,
|
|
onCell: (() => {}) as TableOnCell,
|
|
onChange: (() => {}) as TableOnChange,
|
|
className: '',
|
|
}
|
|
|
|
type NativeAttrs = Omit<React.TableHTMLAttributes<any>, keyof Props>
|
|
export type TableProps = Props & typeof defaultProps & NativeAttrs
|
|
|
|
const Table: React.FC<React.PropsWithChildren<TableProps>> = ({
|
|
children,
|
|
data,
|
|
hover,
|
|
emptyText,
|
|
onRow,
|
|
onCell,
|
|
onChange,
|
|
className,
|
|
...props
|
|
}) => {
|
|
const ref = useRef<HTMLTableElement>(null)
|
|
const [{ width }, updateShape] = useRealShape<HTMLTableElement>(ref)
|
|
const [columns, setColumns] = useState<Array<TableColumnItem>>([])
|
|
const [selfData, setSelfData, dataRef] = useCurrentState<Array<TableColumnItem>>([])
|
|
const updateColumn = (column: TableColumnItem) => {
|
|
setColumns(last => {
|
|
const hasColumn = last.find(item => item.value === column.value)
|
|
if (!hasColumn) return [...last, column]
|
|
return last.map(item => {
|
|
if (item.value !== column.value) return item
|
|
return column
|
|
})
|
|
})
|
|
}
|
|
const removeRow = (rowIndex: number) => {
|
|
const next = dataRef.current.filter((_, index) => index !== rowIndex)
|
|
onChange(next)
|
|
setSelfData([...next])
|
|
}
|
|
|
|
const initialValue = useMemo<TableConfig>(
|
|
() => ({
|
|
columns,
|
|
updateColumn,
|
|
removeRow,
|
|
}),
|
|
[columns],
|
|
)
|
|
|
|
useEffect(() => {
|
|
if (!data) return
|
|
setSelfData(data)
|
|
}, [data])
|
|
useResize(() => updateShape())
|
|
|
|
return (
|
|
<TableContext.Provider value={initialValue}>
|
|
<table ref={ref} className={className} {...props}>
|
|
<TableHead columns={columns} width={width} />
|
|
<TableBody
|
|
data={selfData}
|
|
hover={hover}
|
|
emptyText={emptyText}
|
|
onRow={onRow}
|
|
onCell={onCell}
|
|
/>
|
|
{children}
|
|
|
|
<style jsx>{`
|
|
table {
|
|
border-collapse: separate;
|
|
border-spacing: 0;
|
|
width: 100%;
|
|
}
|
|
`}</style>
|
|
</table>
|
|
</TableContext.Provider>
|
|
)
|
|
}
|
|
|
|
type TableComponent<P = {}> = React.FC<P> & {
|
|
Column: typeof TableColumn
|
|
}
|
|
type ComponentProps = Partial<typeof defaultProps> &
|
|
Omit<Props, keyof typeof defaultProps> &
|
|
NativeAttrs
|
|
;(Table as TableComponent<ComponentProps>).defaultProps = defaultProps
|
|
|
|
export default Table as TableComponent<ComponentProps>
|