SldEditFormCom.js 10 KB

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