1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
   | import { Button, Table, Modal, Form, Input } from 'antd'; import {     DeleteOutlined,     ExclamationCircleOutlined, } from '@ant-design/icons';
  import axios from 'axios'; import React, { useContext, useState, useEffect, useRef } from 'react'
  const { confirm } = Modal;
  export default function NewsCategory() {     const [dataSource, setDataSource] = useState([]);
      const EditableContext = React.createContext(null);
      const columns = [         {             title: 'ID',             dataIndex: 'id',             render: (id) => <b>{id}</b>         },         {             title: '分类名称',             dataIndex: 'title',             onCell: (record) => ({                 record,                 editable: true,                 dataIndex: 'title',                 title: '分类名称',                 handleSave: handleCellSave,             }),         },         {             title: '操作',             render: item => {                 return <div>                     <Button danger shape='circle' icon={<DeleteOutlined />} onClick={() => confirmDelete(item)} />                 </div>             }         },     ];
      const handleCellSave = record => {                  setDataSource(dataSource.map(item => {             if (item.id === record.id) {                 return {                     id: item.id,                     title: record.title,                     value: record.title,                 }             }             return item;         }))
          axios.patch(`/api/categories/${record.id}`, {             title: record.title,             value: record.title,         })     }
      const confirmDelete = item => {         confirm({             title: 'Do you Want to delete this item?',             icon: <ExclamationCircleOutlined />,             onOk() {                                  realDelete(item)             },             onCancel() {                 console.log('Cancel');             },         });     }
      const realDelete = (item) => {                  setDataSource(dataSource.filter(data => data.id !== item.id));         axios.delete(`/api/categories/${item.id}`);     }
      useEffect(() => {         axios.get(`/api/categories`).then(res => {                          setDataSource(res.data);         })     }, []);
      const EditableRow = ({ index, ...props }) => {         const [form] = Form.useForm();         return (             <Form form={form} component={false}>                 <EditableContext.Provider value={form}>                     <tr {...props} />                 </EditableContext.Provider>             </Form>         );     };
      const EditableCell = ({         title,         editable,         children,         dataIndex,         record,         handleSave,         ...restProps     }) => {         const [editing, setEditing] = useState(false);         const inputRef = useRef(null);         const form = useContext(EditableContext);         useEffect(() => {             if (editing) {                 inputRef.current.focus();             }         }, [editing]);
          const toggleEdit = () => {             setEditing(!editing);             form.setFieldsValue({                 [dataIndex]: record[dataIndex],             });         };
          const save = async () => {             try {                 const values = await form.validateFields();                 toggleEdit();                 handleSave({ ...record, ...values });             } catch (errInfo) {                 console.log('Save failed:', errInfo);             }         };
          let childNode = children;
          if (editable) {             childNode = editing ? (                 <Form.Item                     style={{                         margin: 0,                     }}                     name={dataIndex}                     rules={[                         {                             required: true,                             message: `${title} is required.`,                         },                     ]}                 >                     <Input ref={inputRef} onPressEnter={save} onBlur={save} />                 </Form.Item>             ) : (                 <div                     className="editable-cell-value-wrap"                     style={{                         paddingRight: 24,                     }}                     onClick={toggleEdit}                 >                     {children}                 </div>             );         }
          return <td {...restProps}>{childNode}</td>;     };
      return (         <div>             <Table dataSource={dataSource} columns={columns} pagination={{ pageSize: 5 }} rowKey={item => item.id} components={{                 body: {                     row: EditableRow,                     cell: EditableCell,                 },             }} />;         </div>     ) }
  |