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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
| import React,{Component} from 'react' import { Card, Icon, Form, Input, Cascader, Button, message, } from 'antd' import LinkButton from '../../../components/link-button' import {reqCategorys,reqAddUpdatePro} from '../../../api' import PicturesWall from './pictures-wall' import RichText from './rich-text'
const {Item}=Form const {TextArea}=Input
class AddUpdate extends Component{ constructor(props){ super(props) this.pw=React.createRef() this.editor=React.createRef()
this.state={ options:[], } }
initOptions=(categorys)=>{ const options = categorys.map((v,k)=>({ value: v._id, label: v.name, isLeaf: false, }))
this.setState({ options }) }
getCategorys= async (parentId)=>{ const result = await reqCategorys(parentId) if(result.status===0){ const categorys = result.data if (parentId==='0') { this.initOptions(categorys) } else { return categorys } }else{ message.error('产品分类获取失败请刷新重试') } }
valiPrice=(rule, value, callback)=>{ if(value*1>0){ callback() }else{ callback('价格必须大于0') } }
onChange = (value, selectedOptions) => { console.log(value, selectedOptions); }
loadData = async selectedOptions => { const targetOption = selectedOptions[0]; targetOption.loading = true
const subCategorys = await this.getCategorys(targetOption.value) targetOption.loading = false if (subCategorys && subCategorys.length>0) { const childOptions = subCategorys.map(c => ({ value: c._id, label: c.name, isLeaf: true })) targetOption.children = childOptions } else { targetOption.isLeaf = true }
this.setState({ options: [...this.state.options], }) }
submit=()=>{ this.props.form.validateFields(async(error,values)=>{ if(!error){ const {name,desc,price,categoryIds}=values let pCategoryId,categoryId if(categoryIds.length===1){ pCategoryId='0' categoryId=categoryIds[0] }else{ pCategoryId=categoryIds[0] categoryId=categoryIds[1] } const imgs=this.pw.current.getImgs() const detail=this.editor.current.getDetail() const product={name,desc,price,imgs,detail,pCategoryId,categoryId} console.log(product)
const result=await reqAddUpdatePro(product) if(result.status===0){ message.success('添加产品成功') }else{ message.error('添加产品失败') }
}else{ console.log('验证失败,请检查产品数据') } })
}
componentDidMount(){ this.getCategorys('0') } render(){ const title=( <span> <LinkButton> <Icon type='arrow-left' style={{fontSize:20}} /> </LinkButton> <span>添加商品</span> </span> ) //card右 //form内的Item的布局样式 const formItemLayout = { labelCol: {span: 2}, //左侧label标签的宽度占2个格栅 wrapperCol: {span: 8 }, //右侧(输入框外面有一层包裹)占8个格栅 };
//获取from的getFieldDecorator const {getFieldDecorator}=this.props.form return( <Card title={title} extra=''> {/* 使用组件的扩展属性语法 */} <Form {...formItemLayout}> {/* label指定商品前面标签名,placeholder指定输入框提示内容 */} <Item label='商品名称'> {//商品名规则 getFieldDecorator('name',{ initialValue:'', rules:[ {required:true,message:'商品名称必须填写'} ] })(<Input placeholder='输入商品名' />) } </Item>
<Item label='商品描述'> {//autoSize指定文本域最小高度和最大高度 getFieldDecorator('desc',{ initialValue:'', rules:[ {required:true,message:'商品描述必须输入'} ] })(<TextArea placeholder='输入商品描述' autoSize={{ minRows: 2, maxRows: 6 }} />) } </Item>
<Item label='商品价格'> {//validator自定义验证规则要求价格大于0 getFieldDecorator('price',{ initialValue:'', rules:[ {required:true,message:'价格必须输入'}, {validator:(rule,value,callback)=>{ if(value*1>0){ //字符串*1:将字符串转化为数字类型 callback() //此处必须进行回调函数调用,否则将无法通过验证 }else{ callback('价格必须大于0') } }}, ] })(<Input type='number' placeholder='输入商品价格' addonAfter="元" />) } </Item>
<Item label="商品分类"> { getFieldDecorator('categoryIds', { initialValue: [], rules: [ {required: true, message: '必须指定商品分类'}, ] })(<Cascader placeholder='请指定商品分类' options={this.state.options} /*需要显示的列表数据数组*/ loadData={this.loadData} /*当选择某个列表项, 加载下一级列表的监听回调*/ /> ) }
</Item>
<Item label='商品图片'> <PicturesWall ref={this.pw} /> </Item>
<Item label='商品详情' labelCol={{span: 2}} wrapperCol={{span: 20}}> {/**指定把richtext对象装进editor里 */} <RichText ref={this.editor} /> </Item>
<Item > <Button type='primary' onClick={this.submit}>提交</Button> </Item>
</Form> </Card> ) } } export default Form.create()(AddUpdate) //包装当前类使得到form的的强大函数
|