🚀 Perf: [前台]:新增/编辑书签加入表单验证

This commit is contained in:
fanxb 2019-08-01 17:44:50 +08:00
parent 4c64cbe67b
commit 7561ccec06

View File

@ -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 { class AddModal extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
addType: 0, addType: "bookmark",
addName: "", name: "",
addValue: "", url: "",
nameHelp: "",
urlHelp: "",
file: null file: null
}; };
} }
@ -34,11 +47,28 @@ class AddModal extends React.Component {
const { currentEditNode } = nextProps; const { currentEditNode } = nextProps;
if (currentEditNode != null) { if (currentEditNode != null) {
this.type = "edit"; 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 { } else {
this.type = "add"; 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 = () => { submit = () => {
@ -55,18 +85,18 @@ class AddModal extends React.Component {
* @param {*} node * @param {*} node
*/ */
editOne(node) { editOne(node) {
const { addName, addValue } = this.state; const { name, url } = this.state;
const { bookmarkId, type } = this.props.currentEditNode; const { bookmarkId, type } = this.props.currentEditNode;
const body = { const body = {
bookmarkId, bookmarkId,
name: addName, name: name,
url: addValue, url: url,
type type
}; };
httpUtil.post("/bookmark/updateOne", body).then(() => { httpUtil.post("/bookmark/updateOne", body).then(() => {
message.success("编辑成功"); message.success("编辑成功");
node.name = addName; node.name = name;
node.url = addValue; node.url = url;
this.props.closeModal(); this.props.closeModal();
}); });
} }
@ -77,10 +107,15 @@ class AddModal extends React.Component {
addOne() { addOne() {
const { currentAddFolder, updateTreeData, closeModal, treeData } = this.props; const { currentAddFolder, updateTreeData, closeModal, treeData } = this.props;
const path = currentAddFolder == null ? "" : currentAddFolder.path + "." + currentAddFolder.bookmarkId; 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(); const form = new FormData();
form.append("path", path); form.append("path", path);
form.append("file", this.state.file.originFileObj); form.append("file", file.originFileObj);
httpUtil httpUtil
.put("/bookmark/uploadBookmarkFile", form, { .put("/bookmark/uploadBookmarkFile", form, {
headers: { "Content-Type": "multipart/form-data" } headers: { "Content-Type": "multipart/form-data" }
@ -90,10 +125,10 @@ class AddModal extends React.Component {
}); });
} else { } else {
let body = { let body = {
type: this.state.addType, type: this.state.addType === "bookmark" ? 0 : 1,
path, path,
name: this.state.addName, name,
url: this.state.addValue url
}; };
httpUtil.put("/bookmark", body).then(res => { httpUtil.put("/bookmark", body).then(res => {
// addToTree(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 (
<div style={{ textAlign: "center" }}>
<Upload {...uploadProps} style={{ width: "100%" }}>
<Button type="primary">
<Icon type="upload" />
选择书签文件(支持chrome,firefox)
</Button>
<div>{file ? file.name : ""}</div>
</Upload>
</div>
);
}
render() { render() {
const { isShowModal, currentEditNode, closeModal } = this.props; 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 type = currentEditNode == null ? "add" : "edit";
const formItemLayout = { const formItemLayout = {
labelCol: { labelCol: {
@ -126,46 +186,29 @@ class AddModal extends React.Component {
sm: { span: 20 } sm: { span: 20 }
} }
}; };
const uploadProps = {
accept: ".html,.htm",
onChange: e => {
this.setState({ file: e.fileList[0] });
},
beforeUpload: file => {
return false;
},
fileList: []
};
return ( return (
<Modal destroyOnClose title={type === "add" ? "新增" : "编辑"} visible={isShowModal} onCancel={closeModal} footer={false}> <Modal destroyOnClose title={type === "add" ? "新增" : "编辑"} visible={isShowModal} onCancel={closeModal} footer={false}>
<Form {...formItemLayout}> <Form {...formItemLayout}>
{type === "add" ? ( {type === "add" ? (
<Form.Item label="类别"> <Form.Item label="类别">
<Radio.Group defaultValue={0} onChange={e => this.setState({ addType: e.target.value })}> <Radio.Group defaultValue="bookmark" onChange={e => this.setState({ addType: e.target.value })}>
<Radio value={0}>书签</Radio> <Radio value="bookmark">书签</Radio>
<Radio value={1}>文件夹</Radio> <Radio value="folder">文件夹</Radio>
<Radio value={2}>上传书签html</Radio> <Radio value="file">上传书签html</Radio>
</Radio.Group> </Radio.Group>
</Form.Item> </Form.Item>
) : null} ) : null}
{addType < 2 ? ( {addType === "bookmark" || addType === "folder" ? (
<Form.Item label="名称"> <Form.Item label="名称" validateStatus={nameHelp === "" ? "success" : "error"} help={nameHelp}>
<Input type="text" onChange={e => this.setState({ addName: e.target.value })} value={addName} /> <Input type="text" name="name" onChange={this.changeValue.bind(this)} value={name} />
</Form.Item> </Form.Item>
) : null} ) : null}
{addType === 0 ? ( {addType === "bookmark" ? (
<Form.Item label="URL"> <Form.Item label="URL" validateStatus={urlHelp === "" ? "success" : "error"} help={urlHelp}>
<Input type="text" value={addValue} onChange={e => this.setState({ addValue: e.target.value })} /> <Input type="text" name="url" value={url} onChange={this.changeValue.bind(this)} />
</Form.Item> </Form.Item>
) : null} ) : null}
{addType === 2 ? ( {addType === "file" ? this.renderFileUpload() : null}
<Upload {...uploadProps}>
<Button type="primary">
<Icon type="upload" />
{this.state.file == null ? "选择文件" : this.state.file.name.substr(0, 20)}
</Button>
</Upload>
) : null}
<div style={{ textAlign: "center", paddingTop: "1em" }}> <div style={{ textAlign: "center", paddingTop: "1em" }}>
<Button type="primary" onClick={this.submit}> <Button type="primary" onClick={this.submit}>
提交 提交