LogTable.jsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { Button, Form, Input, Popconfirm, Table } from 'antd'
  2. import React, { useContext, useEffect, useRef, useState } from 'react'
  3. import styled from 'styled-components'
  4. import { Link } from 'react-router-dom'
  5. const EditableContext = React.createContext(null)
  6. const FormItem = Form.Item
  7. const LogTable = styled.div`
  8. margin-left: 24px;
  9. .delButton {
  10. margin-left: 40px;
  11. }
  12. `
  13. const EditableRow = ({ index, ...props }) => {
  14. const [form] = Form.useForm()
  15. return (
  16. <Form form={form} component={false}>
  17. <EditableContext.Provider value={form}>
  18. <tr {...props} />
  19. </EditableContext.Provider>
  20. </Form>
  21. )
  22. }
  23. const EditableCell = ({
  24. title,
  25. editable,
  26. children,
  27. dataIndex,
  28. record,
  29. handleSave,
  30. ...restProps
  31. }) => {
  32. const [editing, setEditing] = useState(false)
  33. const inputRef = useRef(null)
  34. const form = useContext(EditableContext)
  35. useEffect(() => {
  36. if (editing) {
  37. inputRef.current.focus()
  38. }
  39. }, [editing])
  40. const toggleEdit = () => {
  41. setEditing(!editing)
  42. form.setFieldsValue({
  43. [dataIndex]: record[dataIndex],
  44. })
  45. }
  46. const save = async () => {
  47. try {
  48. const values = await form.validateFields()
  49. toggleEdit()
  50. handleSave({ ...record, ...values })
  51. } catch (errInfo) {
  52. console.log('Save failed:', errInfo)
  53. }
  54. }
  55. let childNode = children
  56. if (editable) {
  57. childNode = editing ? (
  58. <FormItem
  59. style={{
  60. margin: 0,
  61. }}
  62. name={dataIndex}
  63. rules={[
  64. {
  65. required: true,
  66. message: `${title} is required.`,
  67. },
  68. ]}>
  69. <Input ref={inputRef} onPressEnter={save} onBlur={save} />
  70. </FormItem>
  71. ) : (
  72. <div
  73. className="editable-cell-value-wrap"
  74. style={{
  75. paddingRight: 24,
  76. }}
  77. onClick={toggleEdit}>
  78. {children}
  79. </div>
  80. )
  81. }
  82. return <td {...restProps}>{childNode}</td>
  83. }
  84. export default function LogItems() {
  85. const itemData = [
  86. {
  87. key: '0',
  88. name: '每日供应商销量预测作业2',
  89. type: 'java',
  90. sort: '业务预测',
  91. creTime: '2022.8.18 12:00:00',
  92. updTime: '2022.8.18 12:00:00',
  93. },
  94. {
  95. key: '1',
  96. name: '每日供应商销量预测作业21',
  97. type: 'java',
  98. sort: '业务预测',
  99. creTime: '2022.8.18 12:00:00',
  100. updTime: '2022.8.18 12:00:00',
  101. },
  102. ]
  103. const [dataSource, setDataSource] = useState(
  104. itemData
  105. // [
  106. // {
  107. // key: '0',
  108. // name: '每日供应商销量预测作业2',
  109. // type: 'java',
  110. // sort: '业务预测',
  111. // creTime: '2022.8.18 12:00:00',
  112. // updTime: '2022.8.18 12:00:00',
  113. // },
  114. // {
  115. // key: '1',
  116. // name: '每日供应商销量预测作业21',
  117. // type: 'java',
  118. // sort: '业务预测',
  119. // creTime: '2022.8.18 12:00:00',
  120. // updTime: '2022.8.18 12:00:00',
  121. // },
  122. // ]
  123. )
  124. const [count, setCount] = useState(2)
  125. const handleDelete = key => {
  126. const newData = dataSource.filter(item => item.key !== key)
  127. setDataSource(newData)
  128. }
  129. const defaultColumns = [
  130. {
  131. title: '作业名称',
  132. dataIndex: 'name',
  133. width: '16.6%',
  134. },
  135. {
  136. title: '作业类型',
  137. dataIndex: 'type',
  138. width: '16.6%',
  139. },
  140. {
  141. title: '作业分类',
  142. dataIndex: 'sort',
  143. width: '16.6%',
  144. },
  145. {
  146. title: '创建时间',
  147. dataIndex: 'creTime',
  148. width: '16.6%',
  149. },
  150. {
  151. title: '更新时间',
  152. dataIndex: 'updTime',
  153. width: '16.6%',
  154. },
  155. {
  156. title: '操作',
  157. dataIndex: 'operation',
  158. width: '16.6%',
  159. render: (_, record) =>
  160. dataSource.length >= 1 ? (
  161. <>
  162. <Popconfirm>
  163. <a>编辑</a>
  164. </Popconfirm>
  165. <Popconfirm
  166. title="Sure to delete?"
  167. onConfirm={() => handleDelete(record.key)}>
  168. <a className="delButton">删除</a>
  169. </Popconfirm>
  170. </>
  171. ) : null,
  172. },
  173. ]
  174. const handleSave = row => {
  175. const newData = [...dataSource]
  176. const index = newData.findIndex(item => row.key === item.key)
  177. const item = newData[index]
  178. newData.splice(index, 1, { ...item, ...row })
  179. setDataSource(newData)
  180. }
  181. const components = {
  182. body: {
  183. row: EditableRow,
  184. cell: EditableCell,
  185. },
  186. }
  187. const columns = defaultColumns.map(col => {
  188. if (!col.editable) {
  189. return col
  190. }
  191. return {
  192. ...col,
  193. onCell: record => ({
  194. record,
  195. editable: col.editable,
  196. dataIndex: col.dataIndex,
  197. title: col.title,
  198. handleSave,
  199. }),
  200. }
  201. })
  202. return (
  203. <LogTable>
  204. <Table
  205. components={components}
  206. rowClassName={() => 'editable-row'}
  207. bordered
  208. dataSource={dataSource}
  209. columns={columns}
  210. style={{
  211. marginTop: '20px',
  212. }}
  213. />
  214. </LogTable>
  215. )
  216. }