forgeplus-react/src/forge/Main/sub/UpdateDescModal.jsx

101 lines
3.0 KiB
React
Raw Normal View History

2021-03-24 14:08:10 +08:00
import React , { forwardRef, useEffect } from 'react';
2023-04-06 15:17:11 +08:00
import {Form , Modal , Input, Select } from 'antd';
2021-03-24 14:08:10 +08:00
import "./sub.scss";
2023-04-06 15:17:11 +08:00
import axios from 'axios';
import { useState } from 'react';
2021-03-24 14:08:10 +08:00
const { TextArea } = Input;
2023-04-06 15:17:11 +08:00
function UpdateDescModal({form , visible , onCancel , onOk,desc,website,lesson_url, topicsInfo}){
2021-03-24 14:08:10 +08:00
const { getFieldDecorator, validateFields , setFieldsValue } = form;
2023-04-06 15:17:11 +08:00
const [topics, setTopics] = useState(undefined);
useEffect(()=>{
// 获取所有的项目标签
axios.get(`/v1/project_topics`).then(res=>{
if(res && res.data){
setTopics(res.data.project_topics);
}
})
}, [])
2021-03-24 14:08:10 +08:00
useEffect(()=>{
if(desc || website){
setFieldsValue({
website,desc,lesson_url
2021-03-24 14:08:10 +08:00
})
}
},[desc,website])
function onSure(){
validateFields((err,values)=>{
if(!err){
onCancel();
2023-04-06 15:17:11 +08:00
onOk(values.desc,values.website,values.lesson_url, values.project_topic_names)
2021-03-24 14:08:10 +08:00
}
})
}
2023-04-06 15:17:11 +08:00
// 校验项目标签
function checkTopics(rule, value, callback){
const boolLength = value.filter(item=>item.length > 20);
2023-04-11 14:44:17 +08:00
const bool = value.filter(item=>!/^(?![-])[a-zA-Z0-9\u4e00-\u9fa5\\-]+$/.test(item));
2023-04-06 15:17:11 +08:00
if(value.length > 5){
callback("最多创建5个标签");
}else if(boolLength.length){
callback("每个标签最多支持20个汉字");
2023-04-11 14:44:17 +08:00
}else if(bool.length){
callback("标签名只允许包含中文、字母、数字或者中划线(-), 不能以中划线开头");
2023-04-06 15:17:11 +08:00
}
callback();
}
2021-03-24 14:08:10 +08:00
return(
<Modal
title={"修改信息"}
closable={false}
visible={visible}
centered
onCancel={onCancel}
onOk={onSure}
okText="确定"
cancelText="取消"
2023-04-06 15:17:11 +08:00
width="520px"
2021-03-24 14:08:10 +08:00
className={"descmodal"}
maskClosable={false}
2021-03-24 14:08:10 +08:00
>
<Form>
2021-05-07 16:26:50 +08:00
<Form.Item label="项目简介">
2021-03-24 14:08:10 +08:00
{getFieldDecorator("desc",{
rules:[]
})(
<TextArea placeholder="请输入项目简介" rows={4} maxLength={200}/>
2021-03-24 14:08:10 +08:00
)}
</Form.Item>
2023-04-06 15:17:11 +08:00
<Form.Item label="项目标签">
{getFieldDecorator("project_topic_names",{
rules:[{validator:checkTopics}],
initialValue: topicsInfo && topicsInfo.map(item=>item.name)
})(
<Select mode='tags' placeholder="请输入标签名称" getPopupContainer={trigger => trigger.parentNode} maxLength={2}>
{topics && topics.map(item=> <Select.Option key={item.name}>{item.name}</Select.Option>)}
</Select>
)}
</Form.Item>
2021-03-24 14:08:10 +08:00
<Form.Item label="website">
{getFieldDecorator("website",{
rules:[]
})(
2021-11-30 15:43:19 +08:00
<Input placeholder="website链接" maxLength={200}/>
2021-03-24 14:08:10 +08:00
)}
</Form.Item>
<Form.Item label="实践课程">
{getFieldDecorator("lesson_url",{
rules:[]
})(
2021-11-30 15:43:19 +08:00
<Input placeholder="实践课程链接" maxLength={200}/>
)}
</Form.Item>
2021-03-24 14:08:10 +08:00
</Form>
</Modal>
)
}
export default Form.create()(forwardRef(UpdateDescModal));