From 7561ccec06cac3e5643e42347f63a8fc522071cf Mon Sep 17 00:00:00 2001 From: fanxb Date: Thu, 1 Aug 2019 17:44:50 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=80=20Perf:=20[=E5=89=8D=E5=8F=B0]:?= =?UTF-8?q?=E6=96=B0=E5=A2=9E/=E7=BC=96=E8=BE=91=E4=B9=A6=E7=AD=BE?= =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=A1=A8=E5=8D=95=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/src/pages/manage/OverView/AddModal.jsx | 131 ++++++++++++------- 1 file changed, 87 insertions(+), 44 deletions(-) diff --git a/front/src/pages/manage/OverView/AddModal.jsx b/front/src/pages/manage/OverView/AddModal.jsx index b9b27bf..2d797e2 100644 --- a/front/src/pages/manage/OverView/AddModal.jsx +++ b/front/src/pages/manage/OverView/AddModal.jsx @@ -19,13 +19,26 @@ function mapDispatchToProps(dispatch) { }; } +const reg = { + name: { + reg: /^.{1,40}$/, + text: "名称字符数为1-40" + }, + url: { + reg: /^.{1,500}$/, + text: "链接字符数为1-500" + } +}; + class AddModal extends React.Component { constructor(props) { super(props); this.state = { - addType: 0, - addName: "", - addValue: "", + addType: "bookmark", + name: "", + url: "", + nameHelp: "", + urlHelp: "", file: null }; } @@ -34,11 +47,28 @@ class AddModal extends React.Component { const { currentEditNode } = nextProps; if (currentEditNode != null) { this.type = "edit"; - this.setState({ addType: currentEditNode.type, addName: currentEditNode.name, addValue: currentEditNode.url }); + this.setState({ addType: currentEditNode.type === 0 ? "bookmark" : "folder", name: currentEditNode.name, url: currentEditNode.url }); } else { this.type = "add"; - this.setState({ addType: 0, addName: "", addValue: "", file: null }); + this.setState({ addType: "bookmark", name: "", url: "", file: null }); } + this.setState({ nameHelp: "", urlHelp: "" }); + } + + checkValue(key, value) { + const rule = reg[key]; + if (rule.reg.test(value)) { + this.setState({ [key + "Help"]: "" }); + } else { + this.setState({ [key + "Help"]: rule.text }); + } + } + + changeValue(e) { + const name = e.target.name; + const value = e.target.value.trim(); + this.checkValue(name, value); + this.setState({ [name]: value }); } submit = () => { @@ -55,18 +85,18 @@ class AddModal extends React.Component { * @param {*} node */ editOne(node) { - const { addName, addValue } = this.state; + const { name, url } = this.state; const { bookmarkId, type } = this.props.currentEditNode; const body = { bookmarkId, - name: addName, - url: addValue, + name: name, + url: url, type }; httpUtil.post("/bookmark/updateOne", body).then(() => { message.success("编辑成功"); - node.name = addName; - node.url = addValue; + node.name = name; + node.url = url; this.props.closeModal(); }); } @@ -77,10 +107,15 @@ class AddModal extends React.Component { addOne() { const { currentAddFolder, updateTreeData, closeModal, treeData } = this.props; const path = currentAddFolder == null ? "" : currentAddFolder.path + "." + currentAddFolder.bookmarkId; - if (this.state.addType === 2) { + const { name, url, file, addType } = this.state; + if (addType === "file") { + if (file == null) { + message.error("请先选择文件"); + return; + } const form = new FormData(); form.append("path", path); - form.append("file", this.state.file.originFileObj); + form.append("file", file.originFileObj); httpUtil .put("/bookmark/uploadBookmarkFile", form, { headers: { "Content-Type": "multipart/form-data" } @@ -90,10 +125,10 @@ class AddModal extends React.Component { }); } else { let body = { - type: this.state.addType, + type: this.state.addType === "bookmark" ? 0 : 1, path, - name: this.state.addName, - url: this.state.addValue + name, + url }; httpUtil.put("/bookmark", body).then(res => { // addToTree(res); @@ -112,9 +147,34 @@ class AddModal extends React.Component { } } + renderFileUpload() { + const uploadProps = { + accept: ".html,.htm", + onChange: e => { + this.setState({ file: e.fileList[0] }); + }, + beforeUpload: file => { + return false; + }, + fileList: [] + }; + const { file } = this.state; + return ( +
+ + +
{file ? file.name : ""}
+
+
+ ); + } + render() { const { isShowModal, currentEditNode, closeModal } = this.props; - const { addType, addName, addValue } = this.state; + const { addType, name, url, nameHelp, urlHelp } = this.state; const type = currentEditNode == null ? "add" : "edit"; const formItemLayout = { labelCol: { @@ -126,46 +186,29 @@ class AddModal extends React.Component { sm: { span: 20 } } }; - const uploadProps = { - accept: ".html,.htm", - onChange: e => { - this.setState({ file: e.fileList[0] }); - }, - beforeUpload: file => { - return false; - }, - fileList: [] - }; return (
{type === "add" ? ( - this.setState({ addType: e.target.value })}> - 书签 - 文件夹 - 上传书签html + this.setState({ addType: e.target.value })}> + 书签 + 文件夹 + 上传书签html ) : null} - {addType < 2 ? ( - - this.setState({ addName: e.target.value })} value={addName} /> + {addType === "bookmark" || addType === "folder" ? ( + + ) : null} - {addType === 0 ? ( - - this.setState({ addValue: e.target.value })} /> + {addType === "bookmark" ? ( + + ) : null} - {addType === 2 ? ( - - - - ) : null} + {addType === "file" ? this.renderFileUpload() : null}