SldEditFormCom.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import React, { Component, Fragment } from 'react';
  2. import { Form, Select, Input,InputNumber,DatePicker,TreeSelect,Cascader,Checkbox,Radio } from 'antd';
  3. import global from '@/global.less';
  4. import { sldInputAfterAddons, } from '@/utils/utils';
  5. import { emojiRegex } from "@/utils/regex";
  6. import styles from './SldEditFormCom.less';
  7. const FormItem = Form.Item;
  8. const { RangePicker } = DatePicker;
  9. const InputGroup = Input.Group;
  10. const RadioGroup = Radio.Group;
  11. const CheckboxGroup = Checkbox.Group;
  12. const Option = Select.Option;
  13. const {TextArea} = Input;
  14. export default class SldEditFormCom extends Component {
  15. componentDidMount() {
  16. }
  17. //处理input内容变化事件
  18. handleInputOnchange = (e,item) => {
  19. if(item.handleChange){
  20. item.handleChange(e);
  21. }
  22. }
  23. //多选事件
  24. sldCheckShop = (items, value) => {
  25. if (items.sldCheckShop) {
  26. items.sldCheckShop(value);
  27. }
  28. };
  29. redioOnChange = (e,val) => {
  30. if(val.onChange){
  31. val.onChange(e.target.value);
  32. }
  33. }
  34. commonCon = (val,index) => {
  35. let {
  36. form: { getFieldDecorator },item_width,
  37. } = this.props;
  38. //普通输入框
  39. item_width = item_width!=undefined?item_width:'auto'
  40. if(val.type == 'input'){
  41. // 统一规则定义
  42. const unifyInputRules = [
  43. {
  44. validator: (_, value, callback) => {
  45. const hasEmoji = emojiRegex.test(value);
  46. if (hasEmoji) {
  47. callback('不支持输入表情符号');
  48. } else {
  49. callback();
  50. }
  51. },
  52. },
  53. ];
  54. return (<FormItem
  55. key={index}
  56. label={val.label}
  57. extra={val.extra}
  58. style={{width:val.width!=undefined?val.width+80:250}}
  59. >
  60. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules: [...(val.rules || []), ...unifyInputRules]})(
  61. <Input maxLength={val.maxLength!=undefined&&val.maxLength?val.maxLength:99999999} className={styles.item} disabled={val.disable} placeholder={val.placeholder}/>
  62. )}
  63. </FormItem>
  64. );
  65. }else if(val.type == 'inputnum'){
  66. //数字搜索框
  67. return (
  68. <FormItem key={index}
  69. extra={val.extra}
  70. label={val.label}>
  71. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(<InputNumber min={val.min?val.min:0} max={val.max?val.max:999999999} step={val.step?val.step:1} className={styles.item} placeholder={val.placeholder} precision={val.precision!=undefined?val.precision:0} disabled={val.disable} onChange={(e)=>this.handleInputOnchange(e,val)}/>)}
  72. </FormItem>
  73. );
  74. }else if(val.type == 'select'){
  75. //下拉选择框
  76. return (<FormItem key={index} extra={val.extra} label={val.label}>
  77. {getFieldDecorator(val.name,{initialValue:val.initialValue?val.initialValue:undefined,rules:val.rules})(
  78. <Select placeholder={val.placeholder}
  79. className={styles.item}
  80. onChange={val.onChange}
  81. getPopupContainer={triggerNode => triggerNode.parentNode}
  82. >
  83. {val.sel_data.map((items,indexs)=>{
  84. return <Option key={indexs} value={items.key}>{items.name}</Option>
  85. })}
  86. </Select>
  87. )}
  88. </FormItem>
  89. );
  90. }else if(val.type == 'textarea'){
  91. return (<FormItem
  92. key={index}
  93. help={val.help}
  94. extra={val.extra}
  95. label={val.label}>
  96. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  97. <TextArea maxLength={val.maxLength!=undefined&&val.maxLength?val.maxLength:99999999} className={styles.item} disabled={val.is_disable!=undefined&&val.is_disable?true:false} style={{minHeight: 32}} rows={4} placeholder={val.placeholder}/>
  98. )}
  99. </FormItem>);
  100. }else if(val.type == 'rangepicker'){
  101. //时间范围选择器
  102. return (<FormItem
  103. key={index}
  104. style={{width:val.width!=undefined?val.width+80:250}}
  105. extra={val.extra}
  106. label={val.label}>
  107. {getFieldDecorator(val.name)(
  108. <RangePicker
  109. style={{width:val.width!=undefined&&val.width?val.width:'100%'}}
  110. className={styles.item}
  111. placeholder={[val.placeholder1,val.placeholder2,]}
  112. getCalendarContainer={(triggerNode)=>{
  113. return triggerNode.parentNode
  114. }}
  115. />
  116. )}
  117. </FormItem>
  118. );
  119. }else if(val.type == 'datepicker'){
  120. //时间选择器
  121. return (<FormItem key={index} extra={val.extra} label={val.label}>
  122. {val.initialValue&&getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  123. <DatePicker style={{width:180}} placeholder={val.placeholder}
  124. getCalendarContainer={(triggerNode)=>{
  125. return triggerNode.parentNode
  126. }}
  127. className={styles.item}/>
  128. )}
  129. {!val.initialValue&&getFieldDecorator(val.name,{rules:val.rules})(
  130. <DatePicker style={{width:180}} placeholder={val.placeholder}
  131. getCalendarContainer={(triggerNode)=>{
  132. return triggerNode.parentNode
  133. }}
  134. className={styles.item}/>
  135. )}
  136. </FormItem>
  137. );
  138. }else if(val.type == 'rangeval'){
  139. //范围选择器
  140. return (<FormItem key={index} extra={val.extra} label={val.label}>
  141. <InputGroup compact className={styles.item}>
  142. {getFieldDecorator([val.name1])(<Input maxLength={250} style={{ width: '40%', textAlign: 'center' }} placeholder={val.placeholder1} />)}
  143. <Input maxLength={250} style={{ width: '20%', borderLeft: 0, pointerEvents: 'none', backgroundColor: '#fff' }} placeholder="~" disabled />
  144. {getFieldDecorator([val.name2])(<Input maxLength={250} style={{ width: '40%', textAlign: 'center', borderLeft: 0 }} placeholder={val.placeholder2} />)}
  145. </InputGroup>
  146. </FormItem>
  147. );
  148. }else if(val.type == 'input_after'){
  149. //带图标后缀
  150. return (<FormItem
  151. key={index}
  152. extra={val.extra}
  153. label={val.label}
  154. >
  155. <div onClick={() => val.callback(val.operate_obj)}>
  156. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  157. <Input maxLength={250} style={{width:150,marginLeft:3}} disabled={true} addonAfter={sldInputAfterAddons()} placeholder={val.placeholder}/>
  158. )}
  159. </div>
  160. </FormItem>);
  161. }else if(val.type == 'textarea_single'){
  162. return <FormItem
  163. key={index}
  164. extra={val.extra}
  165. label={val.label}>
  166. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  167. <TextArea className={styles.item} style={{minHeight: 30}} rows={1}/>
  168. )}
  169. </FormItem>;
  170. }else if(val.type == 'TreeSelect'){
  171. return <FormItem key={index} extra={val.extra} label={val.label}>
  172. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  173. <TreeSelect
  174. className={styles.item}
  175. treeData={val.data}
  176. showSearch={true}
  177. placeholder={val.placeholder}
  178. allowClear={val.allowClear}
  179. onSelect={val.onSelect}
  180. dropdownStyle={{maxHeight:300}}
  181. getPopupContainer={triggerNode => triggerNode.parentNode}
  182. />
  183. )}
  184. </FormItem>;
  185. }else if(val.type == 'cascader'){
  186. //三级地址选择
  187. return (<FormItem
  188. key={index}
  189. extra={val.extra}
  190. label={val.label}
  191. >
  192. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  193. <Cascader style={{width:200}} options={JSON.parse(localStorage.getItem('common_area_list'))} placeholder={val.placeholder}/>
  194. )}
  195. </FormItem>
  196. );
  197. }else if(val.type == 'single_checkbox'){
  198. //选择框
  199. return (<FormItem
  200. key={index}
  201. extra={val.extra}
  202. label={val.label}
  203. >
  204. {getFieldDecorator(val.name,{valuePropName:'checked',initialValue:val.initialValue,rules:val.rules})(
  205. <Checkbox
  206. >
  207. {val.check_con}
  208. </Checkbox>
  209. )}
  210. </FormItem>
  211. );
  212. }else if(val.type == 'radio'){
  213. //radio
  214. return (<FormItem
  215. key={index}
  216. extra={val.extra}
  217. label={val.label}
  218. style={{width:val.width!=undefined?val.width+80:250}}
  219. >
  220. {getFieldDecorator(val.name,{valuePropName:'checked',rules:val.rules,initialValue:val.initialValue})(
  221. <RadioGroup size={'small'} defaultValue={val.initialValue} style={{width:val.width!=undefined?val.width:'100%'}} onChange={(e)=>this.redioOnChange(e,val)}>
  222. {val.sel_data.map((item,index)=>{
  223. return <Radio key={index} value={item.key}>{item.name}</Radio>
  224. })}
  225. </RadioGroup>
  226. )}
  227. </FormItem>
  228. );
  229. }else if(val.type == 'checkboxgroup'){
  230. //radio
  231. return (<FormItem
  232. key={index}
  233. style={{width:val.width!=undefined?val.width+80:250}}
  234. extra={val.extra}
  235. label={val.label}
  236. >
  237. {getFieldDecorator(val.name,{initialValue: val.initialValue,rules:val.rules})(
  238. <CheckboxGroup style={{width:193}} options={val.sldOptions} onChange={(value) => this.sldCheckShop(val, value)}/>,
  239. )}
  240. </FormItem>
  241. );
  242. }
  243. }
  244. //渲染每一项
  245. renderSearchSecond = (search_data) => {
  246. return search_data.map((item,index)=>{
  247. return this.commonCon(item,index);
  248. });
  249. }
  250. renderSimpleForm() {
  251. const {search_data} = this.props;
  252. return (
  253. search_data.length>0&&
  254. <div style={{display:'block'}}>
  255. <div className={styles.flex_com_row_wrap}>
  256. {this.renderSearchSecond(search_data)}
  257. </div>
  258. </div>
  259. );
  260. }
  261. render() {
  262. return this.renderSimpleForm();
  263. }
  264. }