项目作者: aond

项目描述 :
ant design editable component
高级语言: TypeScript
项目地址: git://github.com/aond/antd-editable-component.git
创建时间: 2019-08-20T05:07:58Z
项目社区:https://github.com/aond/antd-editable-component

开源协议:

下载


antd-editable-component

this is a component for ant-design-pro.

usage

  1. create an application using umi

example: application named demo

  1. mkdir demo && cd demo
  2. yarn create umi
  3. Select
  4. ant-design-pro
  5. Select
  6. > TypeScript
  7. npm i
  8. npm i -S antd-editable-component
  9. npm start
  1. modify the file demo/src/pages/Welcome.tsx
  1. import React from 'react';
  2. import { PageHeaderWrapper } from '@ant-design/pro-layout';
  3. import { Table, Popconfirm, Form, Button } from 'antd';
  4. import {FormComponentProps} from 'antd/es/form';
  5. import {Dispatch} from "redux";
  6. import {EditableContext} from "antd-editable-component/context";
  7. import EditableCell, {EditableColumnProps} from "antd-editable-component/cell";
  8. // record data type, in other word, table columns
  9. export interface ColumnDataType {
  10. key: string;
  11. name: string;
  12. age: number;
  13. address: string;
  14. }
  15. export interface ProductTableProps {
  16. form: FormComponentProps['form'];
  17. dispatch: Dispatch<any>;
  18. }
  19. export interface ProductTableState {
  20. data: Array<ColumnDataType>;
  21. editingKey: string;
  22. submitting: boolean;
  23. }
  24. // todo 将可编辑表格的通用操作提升为组件类 继承自 React.Component
  25. class ProductTable extends React.Component<ProductTableProps, ProductTableState> {
  26. private columns: Array<EditableColumnProps<ColumnDataType>>;
  27. constructor(props: ProductTableProps) {
  28. super(props);
  29. this.state = {
  30. data: [],
  31. editingKey: '',
  32. submitting: true
  33. };
  34. this.columns = [
  35. {
  36. title: 'name',
  37. dataIndex: 'name',
  38. width: '25%',
  39. editable: true,
  40. },
  41. {
  42. title: 'age',
  43. dataIndex: 'age',
  44. width: '15%',
  45. editable: true,
  46. },
  47. {
  48. title: 'address',
  49. dataIndex: 'address',
  50. width: '40%',
  51. editable: true,
  52. },
  53. {
  54. title: 'operation',
  55. dataIndex: 'operation',
  56. render: (text: any, record: ColumnDataType, index: number) => {
  57. const { editingKey } = this.state;
  58. const editable = this.isEditing(record);
  59. return editable ? (
  60. <span>
  61. <EditableContext.Consumer>
  62. {(form: FormComponentProps['form']) => (
  63. <a
  64. href="javascript:;"
  65. onClick={() => this.save(form, record.key, index)}
  66. style={{ marginRight: 8 }}
  67. >
  68. Save
  69. </a>
  70. )}
  71. </EditableContext.Consumer>
  72. <Popconfirm title="Sure to cancel?" onConfirm={() => this.cancel()}>
  73. <a>Cancel</a>
  74. </Popconfirm>
  75. </span>
  76. ) : (
  77. <Button type='link' disabled={editingKey !== ''} onClick={() => this.edit(record.key)}>
  78. Edit
  79. </Button>
  80. );
  81. },
  82. },
  83. ];
  84. }
  85. isEditing = (record: ColumnDataType) => record.key === this.state.editingKey;
  86. cancel = () => {
  87. this.setState({ editingKey: '' });
  88. };
  89. save = (form: FormComponentProps['form'], key: string, index: number) => {
  90. form.validateFields((error, row) => {
  91. debugger;
  92. if (error) {
  93. return;
  94. }
  95. const newData = [...this.state.data];
  96. // const index = newData.findIndex(item => key === item.key);
  97. if (index > -1) {
  98. const item = newData[index];
  99. newData.splice(index, 1, {
  100. ...item,
  101. ...row,
  102. });
  103. this.setState({ data: newData, editingKey: '' });
  104. } else {
  105. newData.push(row);
  106. this.setState({ data: newData, editingKey: '' });
  107. }
  108. });
  109. };
  110. edit = (key: string) => {
  111. this.setState({ editingKey: key });
  112. };
  113. // fetch the data from server.
  114. componentWillMount(): void {
  115. // faker data
  116. const data: Array<ColumnDataType> = [];
  117. for (let i = 0; i < 100; i++) {
  118. data.push({
  119. key: i.toString(),
  120. name: `Edrward ${i}`,
  121. age: 32,
  122. address: `London Park no. ${i}`,
  123. });
  124. }
  125. setTimeout(() => {
  126. this.setState({
  127. data: data,
  128. submitting: false,
  129. })
  130. }, 3000)
  131. }
  132. render() {
  133. const components = {
  134. body: {
  135. cell: EditableCell,
  136. },
  137. };
  138. const columns = this.columns.map(col => {
  139. if (!col.editable) {
  140. return col;
  141. }
  142. return {
  143. ...col,
  144. onCell: (record: ColumnDataType) => ({
  145. record,
  146. fieldType: col.fieldType,
  147. selectData: col.selectData,
  148. dataIndex: col.dataIndex,
  149. title: col.title,
  150. editing: this.isEditing(record),
  151. }),
  152. };
  153. });
  154. return (
  155. <div>
  156. <PageHeaderWrapper>
  157. <EditableContext.Provider value={this.props.form}>
  158. <Table
  159. components={components}
  160. bordered
  161. loading={this.state.submitting}
  162. dataSource={this.state.data}
  163. columns={columns}
  164. rowClassName={() => "editable-row"}
  165. pagination={{
  166. onChange: this.cancel,
  167. }}
  168. />
  169. </EditableContext.Provider>
  170. </PageHeaderWrapper>
  171. </div>
  172. );
  173. }
  174. }
  175. const Product = Form.create<ProductTableProps>()(ProductTable);
  176. export default Product;