Browse Source

Update rc-table (#8187)

* Update rc-table

* Add table drag sorting demo
pull/8230/head
Wei Zhu 7 years ago
committed by GitHub
parent
commit
f78d33ed59
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 44
      CHANGELOG.zh-CN.md
  2. 1
      components/table/Column.tsx
  3. 16
      components/table/Table.tsx
  4. 512
      components/table/__tests__/__snapshots__/demo.test.js.snap
  5. 204
      components/table/demo/drag-sorting.md
  6. 2
      components/table/demo/dynamic-settings.md
  7. 11
      components/table/index.en-US.md
  8. 11
      components/table/index.zh-CN.md
  9. 5
      package.json

44
CHANGELOG.zh-CN.md

@ -23,6 +23,50 @@ timeline: true
- 新增 `showAction`
- 新增 `onMouseEnter``onMouseLeave`
- 新增 `focus()`、`blur()` 和 `autoFocus`
- Table
- 新增 `components` 属性,可以通过该属性覆盖 table 默认元素:
```javascript
const components = {
table: MyTable,
header: {
wrapper: HeaderWrapper,
row: HeaderRow,
cell: HeaderCell,
},
body: {
wrapper: BodyWrapper,
row: BodyRow,
cell: BodyCell,
},
};
<Table components={components} columns={columns data={data}} />
```
- 新增 `onRow` 用于设置行属性。
- 新增 `onHeaderRow` 用于设置头部的行属性。
- 新增 `column[onCell]` 用于设置单元格属性 。
- 新增 `column[onHeaderCell]` 用于设置头部单元格属性。
- 新增 `column[align]` 用于设置列文字对其。
- 废弃以下属性
- `onRowClick`
- `onRowDoubleClick`
- `onRowContextMenu`
- 'onRowMouseEnter'
- 'onRowMouseLeave'
以上属性请使用 `onRow` 代替:
```javascript
<Table onRow={(record) => ({
onClick: () => {},
onDoubleClick: () => {},
onContextMenu: () => {},
onMouseEnter: () => {},
onMouseLeave: () => {},
})} />
```
- 废弃 `getBodyWrapper`, 请使用 `components` 代替。
## 2.13.10

1
components/table/Column.tsx

@ -22,6 +22,7 @@ export interface ColumnProps<T> {
sortOrder?: boolean | ('ascend' | 'descend');
children?: ColumnProps<T>[];
onCellClick?: (record: T, event: any) => void;
onCell?: (record: T) => any;
}
export default class Column<T> extends React.Component<ColumnProps<T>, React.ComponentState> {}

16
components/table/Table.tsx

@ -40,6 +40,20 @@ const emptyObject = {};
export type TableColumnConfig<T> = ColumnProps<T>;
export interface TableComponents {
table?: React.ComponentType<any>;
header?: {
wrapper?: React.ComponentType<any>;
row?: React.ComponentType<any>;
cell?: React.ComponentType<any>;
};
body?: {
wrapper?: React.ComponentType<any>;
row?: React.ComponentType<any>;
cell?: React.ComponentType<any>;
};
}
export interface TableRowSelection<T> {
type?: 'checkbox' | 'radio';
selectedRowKeys?: string[] | number[];
@ -59,6 +73,7 @@ export interface TableProps<T> {
pagination?: PaginationProps | boolean;
size?: 'default' | 'middle' | 'small';
dataSource?: T[];
components?: TableComponents;
columns?: ColumnProps<T>[];
rowKey?: string | ((record: T, index: number) => string);
rowClassName?: (record: T, index: number) => string;
@ -74,6 +89,7 @@ export interface TableProps<T> {
locale?: Object;
indentSize?: number;
onRowClick?: (record: T, index: number, event: Event) => any;
onRow?: (record: T, index: number) => any;
useFixedHeader?: boolean;
bordered?: boolean;
showHeader?: boolean;

512
components/table/__tests__/__snapshots__/demo.test.js.snap

@ -1049,6 +1049,182 @@ exports[`renders ./components/table/demo/custom-filter-panel.md correctly 1`] =
</div>
`;
exports[`renders ./components/table/demo/drag-sorting.md correctly 1`] = `
<div
class="ant-table-wrapper"
>
<div
class="ant-spin-nested-loading"
>
<div
class="ant-spin-container"
>
<div
class="ant-table ant-table-large ant-table-scroll-position-left"
>
<div
class="ant-table-content"
>
<div
class="ant-table-body"
>
<table
class=""
>
<colgroup>
<col />
<col />
<col />
</colgroup>
<thead
class="ant-table-thead"
>
<tr>
<th
class=""
>
<span>
Name
</span>
</th>
<th
class=""
>
<span>
Age
</span>
</th>
<th
class=""
>
<span>
Address
</span>
</th>
</tr>
</thead>
<tbody
class="ant-table-tbody"
>
<tr
class="ant-table-row ant-table-row-level-0"
index="0"
style="cursor:move"
>
<td
class=""
>
<span
class="ant-table-row-indent indent-level-0"
style="padding-left:0px"
/>
John Brown
</td>
<td
class=""
>
32
</td>
<td
class=""
>
New York No. 1 Lake Park
</td>
</tr>
<tr
class="ant-table-row ant-table-row-level-0"
index="1"
style="cursor:move"
>
<td
class=""
>
<span
class="ant-table-row-indent indent-level-0"
style="padding-left:0px"
/>
Jim Green
</td>
<td
class=""
>
42
</td>
<td
class=""
>
London No. 1 Lake Park
</td>
</tr>
<tr
class="ant-table-row ant-table-row-level-0"
index="2"
style="cursor:move"
>
<td
class=""
>
<span
class="ant-table-row-indent indent-level-0"
style="padding-left:0px"
/>
Joe Black
</td>
<td
class=""
>
32
</td>
<td
class=""
>
Sidney No. 1 Lake Park
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<ul
class="ant-pagination ant-table-pagination"
unselectable="unselectable"
>
<li
aria-disabled="true"
class="ant-pagination-disabled ant-pagination-prev"
tabindex="0"
title="Previous Page"
>
<a
class="ant-pagination-item-link"
/>
</li>
<li
class="ant-pagination-item ant-pagination-item-1 ant-pagination-item-active"
tabindex="0"
title="1"
>
<a>
1
</a>
</li>
<li
aria-disabled="true"
class="ant-pagination-disabled ant-pagination-next"
tabindex="0"
title="Next Page"
>
<a
class="ant-pagination-item-link"
/>
</li>
</ul>
</div>
</div>
</div>
`;
exports[`renders ./components/table/demo/dynamic-settings.md correctly 1`] = `
<div>
<div
@ -3527,342 +3703,6 @@ exports[`renders ./components/table/demo/expand-children.md correctly 1`] = `
New York No. 1 Lake Park
</td>
</tr>
<tr
class="ant-table-row ant-table-row-level-1"
style="display:none"
>
<td
class="ant-table-selection-column"
>
<span>
<label
class="ant-checkbox-wrapper"
>
<span
class="ant-checkbox"
>
<input
class="ant-checkbox-input"
type="checkbox"
/>
<span
class="ant-checkbox-inner"
/>
</span>
</label>
</span>
</td>
<td
class=""
>
<span
class="ant-table-row-indent indent-level-1"
style="padding-left:20px"
/>
<span
class="ant-table-row-expand-icon ant-table-row-spaced"
/>
John Brown
</td>
<td
class=""
>
42
</td>
<td
class=""
>
New York No. 2 Lake Park
</td>
</tr>
<tr
class="ant-table-row ant-table-row-level-1"
style="display:none"
>
<td
class="ant-table-selection-column"
>
<span>
<label
class="ant-checkbox-wrapper"
>
<span
class="ant-checkbox"
>
<input
class="ant-checkbox-input"
type="checkbox"
/>
<span
class="ant-checkbox-inner"
/>
</span>
</label>
</span>
</td>
<td
class=""
>
<span
class="ant-table-row-indent indent-level-1"
style="padding-left:20px"
/>
<span
class="ant-table-row-expand-icon ant-table-row-collapsed"
/>
John Brown jr.
</td>
<td
class=""
>
30
</td>
<td
class=""
>
New York No. 3 Lake Park
</td>
</tr>
<tr
class="ant-table-row ant-table-row-level-2"
style="display:none"
>
<td
class="ant-table-selection-column"
>
<span>
<label
class="ant-checkbox-wrapper"
>
<span
class="ant-checkbox"
>
<input
class="ant-checkbox-input"
type="checkbox"
/>
<span
class="ant-checkbox-inner"
/>
</span>
</label>
</span>
</td>
<td
class=""
>
<span
class="ant-table-row-indent indent-level-2"
style="padding-left:40px"
/>
<span
class="ant-table-row-expand-icon ant-table-row-spaced"
/>
Jimmy Brown
</td>
<td
class=""
>
16
</td>
<td
class=""
>
New York No. 3 Lake Park
</td>
</tr>
<tr
class="ant-table-row ant-table-row-level-1"
style="display:none"
>
<td
class="ant-table-selection-column"
>
<span>
<label
class="ant-checkbox-wrapper"
>
<span
class="ant-checkbox"
>
<input
class="ant-checkbox-input"
type="checkbox"
/>
<span
class="ant-checkbox-inner"
/>
</span>
</label>
</span>
</td>
<td
class=""
>
<span
class="ant-table-row-indent indent-level-1"
style="padding-left:20px"
/>
<span
class="ant-table-row-expand-icon ant-table-row-collapsed"
/>
Jim Green sr.
</td>
<td
class=""
>
72
</td>
<td
class=""
>
London No. 1 Lake Park
</td>
</tr>
<tr
class="ant-table-row ant-table-row-level-2"
style="display:none"
>
<td
class="ant-table-selection-column"
>
<span>
<label
class="ant-checkbox-wrapper"
>
<span
class="ant-checkbox"
>
<input
class="ant-checkbox-input"
type="checkbox"
/>
<span
class="ant-checkbox-inner"
/>
</span>
</label>
</span>
</td>
<td
class=""
>
<span
class="ant-table-row-indent indent-level-2"
style="padding-left:40px"
/>
<span
class="ant-table-row-expand-icon ant-table-row-collapsed"
/>
Jim Green
</td>
<td
class=""
>
42
</td>
<td
class=""
>
London No. 2 Lake Park
</td>
</tr>
<tr
class="ant-table-row ant-table-row-level-3"
style="display:none"
>
<td
class="ant-table-selection-column"
>
<span>
<label
class="ant-checkbox-wrapper"
>
<span
class="ant-checkbox"
>
<input
class="ant-checkbox-input"
type="checkbox"
/>
<span
class="ant-checkbox-inner"
/>
</span>
</label>
</span>
</td>
<td
class=""
>
<span
class="ant-table-row-indent indent-level-3"
style="padding-left:60px"
/>
<span
class="ant-table-row-expand-icon ant-table-row-spaced"
/>
Jim Green jr.
</td>
<td
class=""
>
25
</td>
<td
class=""
>
London No. 3 Lake Park
</td>
</tr>
<tr
class="ant-table-row ant-table-row-level-3"
style="display:none"
>
<td
class="ant-table-selection-column"
>
<span>
<label
class="ant-checkbox-wrapper"
>
<span
class="ant-checkbox"
>
<input
class="ant-checkbox-input"
type="checkbox"
/>
<span
class="ant-checkbox-inner"
/>
</span>
</label>
</span>
</td>
<td
class=""
>
<span
class="ant-table-row-indent indent-level-3"
style="padding-left:60px"
/>
<span
class="ant-table-row-expand-icon ant-table-row-spaced"
/>
Jimmy Green sr.
</td>
<td
class=""
>
18
</td>
<td
class=""
>
London No. 4 Lake Park
</td>
</tr>
<tr
class="ant-table-row ant-table-row-level-0"
>

204
components/table/demo/drag-sorting.md

@ -0,0 +1,204 @@
---
order: 25
title:
en-US: Drag sorting
zh-CN: 拖拽排序
---
## zh-CN
使用自定义元素,我们可以集成 react-dnd 来实现拖拽排序。
## en-US
By using custom components, we can integrate table with react-dnd to implement drag sorting.
````jsx
import { Table } from 'antd';
import { DragDropContext, DragSource, DropTarget } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import update from 'immutability-helper';
function dragDirection(
dragIndex,
hoverIndex,
initialClientOffset,
clientOffset,
sourceClientOffset,
) {
const hoverMiddleY = (initialClientOffset.y - sourceClientOffset.y) / 2;
const hoverClientY = clientOffset.y - sourceClientOffset.y;
if (dragIndex < hoverIndex && hoverClientY > hoverMiddleY) {
return 'downward';
}
if (dragIndex > hoverIndex && hoverClientY < hoverMiddleY) {
return 'upward';
}
}
let BodyRow = (props) => {
const {
isOver,
connectDragSource,
connectDropTarget,
moveRow,
dragRow,
clientOffset,
sourceClientOffset,
initialClientOffset,
...restProps
} = props;
const style = { cursor: 'move' };
let className = restProps.className;
if (isOver && initialClientOffset) {
const direction = dragDirection(
dragRow.index,
restProps.index,
initialClientOffset,
clientOffset,
sourceClientOffset
);
if (direction === 'downward') {
className += ' drop-over-downward';
}
if (direction === 'upward') {
className += ' drop-over-upward';
}
}
return connectDragSource(
connectDropTarget(
<tr
{...restProps}
className={className}
style={style}
/>
)
);
};
const rowSource = {
beginDrag(props) {
return {
index: props.index,
};
},
};
const rowTarget = {
drop(props, monitor) {
const dragIndex = monitor.getItem().index;
const hoverIndex = props.index;
// Don't replace items with themselves
if (dragIndex === hoverIndex) {
return;
}
// Time to actually perform the action
props.moveRow(dragIndex, hoverIndex);
// Note: we're mutating the monitor item here!
// Generally it's better to avoid mutations,
// but it's good here for the sake of performance
// to avoid expensive index searches.
monitor.getItem().index = hoverIndex;
},
};
BodyRow = DropTarget('row', rowTarget, (connect, monitor) => ({
connectDropTarget: connect.dropTarget(),
isOver: monitor.isOver(),
sourceClientOffset: monitor.getSourceClientOffset(),
}))(
DragSource('row', rowSource, (connect, monitor) => ({
connectDragSource: connect.dragSource(),
dragRow: monitor.getItem(),
clientOffset: monitor.getClientOffset(),
initialClientOffset: monitor.getInitialClientOffset(),
}))(BodyRow)
);
const columns = [{
title: 'Name',
dataIndex: 'name',
key: 'name',
}, {
title: 'Age',
dataIndex: 'age',
key: 'age',
}, {
title: 'Address',
dataIndex: 'address',
key: 'address',
}];
class DragSortingTable extends React.Component {
state = {
data: [{
key: '1',
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
}, {
key: '2',
name: 'Jim Green',
age: 42,
address: 'London No. 1 Lake Park',
}, {
key: '3',
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
}],
}
components = {
body: {
row: BodyRow,
},
}
moveRow = (dragIndex, hoverIndex) => {
const { data } = this.state;
const dragRow = data[dragIndex];
this.setState(
update(this.state, {
data: {
$splice: [[dragIndex, 1], [hoverIndex, 0, dragRow]],
},
}),
);
}
render() {
return (
<Table
columns={columns}
dataSource={this.state.data}
components={this.components}
onRow={(record, index) => ({
index,
moveRow: this.moveRow,
})}
/>
);
}
}
const Demo = DragDropContext(HTML5Backend)(DragSortingTable);
ReactDOM.render(<Demo />, mountNode);
````
````css
#components-table-demo-drag-sorting tr.drop-over-downward td {
border-bottom: 2px dashed #1890ff;
}
#components-table-demo-drag-sorting tr.drop-over-upward td {
border-top: 2px dashed #1890ff;
}
````

2
components/table/demo/dynamic-settings.md

@ -1,5 +1,5 @@
---
order: 25
order: 26
title:
en-US: Dynamic Settings
zh-CN: 动态控制表格属性

11
components/table/index.en-US.md

@ -53,6 +53,7 @@ const columns = [{
| Property | Description | Type | Default |
| -------- | ----------- | ---- | ------- |
| bordered | Whether to show all table borders | boolean | `false` |
| components | Override default table elements | object | - |
| columns | Columns of table | [ColumnProps](https://git.io/vMMXC)\[] | - |
| dataSource | Data record array to be displayed | any\[] | - |
| defaultExpandAllRows | Expand all rows initially | boolean | `false` |
@ -74,11 +75,8 @@ const columns = [{
| onChange | Callback executed when pagination, filters or sorter is changed | Function(pagination, filters, sorter) | |
| onExpand | Callback executed when the row expand icon is clicked | Function(expanded, record) | |
| onExpandedRowsChange | Callback executed when the expanded rows change | Function(expandedRows) | |
| onRowClick | Callback executed when a row is clicked | Function(record, index, event) | - |
| onRowContextMenu | Callback executed when right click on a row | Function(record, index, event) | - |
| onRowDoubleClick| Callback executed when a row is double clicked | Function(record, index, event) | - |
| onRowMouseEnter | Callback executed when mouse enters a row | Function(record, index, event) | - |
| onRowMouseLeave | Callback executed when mouse leaves a row | Function(record, index, event) | - |
| onHeaderRow | Set props on per header row | Function(column, index) | - |
| onRow | Set props on per row | Function(record, index) | - |
### Column
@ -104,9 +102,10 @@ One of the Table `columns` prop for describing the table's columns, Column has t
| defaultSortOrder | Default order of sorted values: `'ascend'` `'descend'` `null` | string | - |
| title | Title of this column | string\|ReactNode | - |
| width | Width of this column | string\|number | - |
| onCellClick | Callback executed when table cell is clicked | Function(record, event) | - |
| onCell | Set props on per cell | Function(record) | - |
| onFilter | Callback executed when the confirm filter button is clicked | Function | - |
| onFilterDropdownVisibleChange | Callback executed when `filterDropdownVisible` is changed | function(visible) {} | - |
| onHeaderCell | Set props on per header cell | Function(column) | - |
### ColumnGroup

11
components/table/index.zh-CN.md

@ -55,6 +55,7 @@ const columns = [{
| --- | --- | --- | --- |
| bordered | 是否展示外边框和列边框 | boolean | false |
| columns | 表格列的配置描述,具体项见下表 | [ColumnProps](https://git.io/vMMXC)\[] | - |
| components | 覆盖默认的 table 元素 | object | - |
| dataSource | 数据数组 | any\[] | |
| defaultExpandAllRows | 初始时,是否展开所有行 | boolean | false |
| defaultExpandedRowKeys | 默认展开的行 | string\[] | - |
@ -75,11 +76,8 @@ const columns = [{
| onChange | 分页、排序、筛选变化时触发 | Function(pagination, filters, sorter) | |
| onExpand | 点击展开图标时触发 | Function(expanded, record) | |
| onExpandedRowsChange | 展开的行变化时触发 | Function(expandedRows) | |
| onRowClick | 点击行时触发 | Function(record, index, event) | - |
| onRowContextMenu | 右键行时触发 | Function(record, index, event) | - |
| onRowDoubleClick | 双击行时触发 | Function(record, index, event) | - |
| onRowMouseEnter | 鼠标移入行时触发 | Function(record, index, event) | - |
| onRowMouseLeave | 鼠标移出行时触发 | Function(record, index, event) | - |
| onHeaderRow | 设置头部列属性 | Function(column, index) | - |
| onRow | 设置列属性 | Function(record, index) | - |
### Column
@ -104,9 +102,10 @@ const columns = [{
| sortOrder | 排序的受控属性,外界可用此控制列的排序,可设置为 `'ascend'` `'descend'` `false` | boolean\|string | - |
| title | 列头显示文字 | string\|ReactNode | - |
| width | 列宽度 | string\|number | - |
| onCellClick | 单元格点击回调 | Function(record, event) | - |
| onCell | 设置单元格属性 | Function(record) | - |
| onFilter | 本地模式下,确定筛选的运行函数 | Function | - |
| onFilterDropdownVisibleChange | 自定义筛选菜单可见变化时调用 | function(visible) {} | - |
| onHeaderCell | 设置头部单元格属性 | Function(column) | - |
### ColumnGroup

5
package.json

@ -68,7 +68,7 @@
"rc-slider": "~8.4.0",
"rc-steps": "~3.0.0",
"rc-switch": "~1.5.1",
"rc-table": "~5.6.9",
"rc-table": "~6.1.0",
"rc-tabs": "~9.1.2",
"rc-time-picker": "~3.1.0",
"rc-tooltip": "~3.7.0",
@ -120,6 +120,7 @@
"eslint-tinker": "^0.4.0",
"fetch-jsonp": "^1.0.3",
"glob": "^7.1.1",
"immutability-helper": "^2.5.0",
"jest": "^21.1.0",
"jsonml.js": "^0.1.0",
"lint-staged": "^4.0.2",
@ -136,6 +137,8 @@
"react": "^16.0.0",
"react-color": "^2.11.7",
"react-copy-to-clipboard": "^5.0.0",
"react-dnd": "^2.5.4",
"react-dnd-html5-backend": "^2.5.4",
"react-document-title": "^2.0.1",
"react-dom": "^16.0.0",
"react-github-button": "^0.1.1",

Loading…
Cancel
Save