import React, { useState, useContext, useRef } from "react"; import { Table, Form, Input, Button } from "antd"; import styles from "./Url.less"; import { UrlItem, ExtensionActions } from "definitions"; import { GENERATE_TYPE_AWESOME, GENERATE_TYPE_REFERENCE, GENERATE_TYPE_REFERENCE_RELATIVE } from "../../../../const"; const UrlContext = React.createContext(null); const UrlRow = ({ form, index, ...props }) => { return ( ); }; const UrlFormRow = Form.create()(UrlRow); class UrlCell extends React.Component { state = { editing: false }; toggleEdit = () => { const editing = !this.state.editing; this.setState({ editing }, () => { if (editing) { this.input.focus(); } }); }; save = e => { const { record, handleSave }: any = this.props; this.form.validateFields((error, values) => { if (error && error[e.currentTarget.id]) { return; } this.toggleEdit(); handleSave({ ...record, ...values }); }); }; renderCell = form => { this.form = form; const { children, dataIndex, record, title }: any = this.props; const { editing } = this.state; return editing ? ( {form.getFieldDecorator(dataIndex, { rules: [ { required: true, message: `${title} is required.` } ], initialValue: record[dataIndex] })( (this.input = node)} onPressEnter={this.save} onBlur={this.save} /> )} ) : (
{children}
); }; input: any; form: any; render() { const { editable, dataIndex, title, record, index, handleSave, children, ...restProps }: any = this.props; return ( {editable ? ( {this.renderCell} ) : ( children )} ); } } const Url = props => { const { urls, actions }: { urls: UrlItem[]; actions: ExtensionActions } = props; const [dataSource, setDataSource] = useState(urls); let [columns, setColumns] = useState([ { title: "替换项", dataIndex: "str", width: "30%", editable: true }, { title: "被替换项", dataIndex: "toStr", width: "30%", editable: true }, { title: "操作", dataIndex: "operation", render: (text, record) => (
gotoLinks(record)}>跳转 copyLinks(record)}>复制 handleDelete(record.key)}> 删除
) } ]); const components = { body: { row: UrlFormRow, cell: UrlCell } }; columns = columns.map(col => { if (!col.editable) { return col; } return { ...col, onCell: record => ({ record, editable: col.editable, dataIndex: col.dataIndex, title: col.title, handleSave: handleSave }) }; }); function handleSave(row) { const newData = [...dataSource]; const index = newData.findIndex(item => row.key === item.key); const item = newData[index]; newData.splice(index, 1, { ...item, ...row }); setDataSource(newData); actions.changeSettings({urls:newData}) } function handleAdd() { const newData = { key: +new Date(), str: "被替换的字符串", toStr: "替换成的字符串" }; setDataSource([...dataSource, newData]); } function handleDelete(key: number) { const newData = dataSource.filter(item => item.key !== key) setDataSource(newData); actions.changeSettings({urls:newData}) } function gotoLinks(record:UrlItem){ actions.gotoLink(record); } function copyLinks(record:UrlItem){ actions.copyLink(record) } function generateAwesomeUrl(){ actions.generateUrl({type:GENERATE_TYPE_AWESOME}) } function generateReferenceUrl(){ actions.generateUrl({type:GENERATE_TYPE_REFERENCE}) } function generateRelativeReferenceUrl() { actions.generateUrl({type:GENERATE_TYPE_REFERENCE_RELATIVE}) } return (
"editable-row"} bordered dataSource={dataSource} columns={columns} pagination={false} /> ); }; export default Url;