SldEditFormCom.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. return (<FormItem
  41. key={index}
  42. label={val.label}
  43. extra={val.extra}
  44. style={{width:val.width!=undefined?val.width+80:250}}
  45. >
  46. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  47. <Input maxLength={val.maxLength?val.maxLength:250} className={styles.item} disabled={val.disable} placeholder={val.placeholder}/>
  48. )}
  49. </FormItem>
  50. );
  51. }else if(val.type == 'inputnum'){
  52. //数字搜索框
  53. return (
  54. <FormItem key={index}
  55. extra={val.extra}
  56. label={val.label}>
  57. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(<InputNumber min={0} max={999999999} step={val.step?val.step:0} className={styles.item} placeholder={val.placeholder} precision={val.precision!=undefined?val.precision:0} disabled={val.disable} onChange={(e)=>this.handleInputOnchange(e,val)}/>)}
  58. </FormItem>
  59. );
  60. }else if(val.type == 'select'){
  61. //下拉选择框
  62. return (<FormItem key={index} extra={val.extra} label={val.label}>
  63. {getFieldDecorator(val.name,{initialValue:val.initialValue?val.initialValue:undefined,rules:val.rules})(
  64. <Select placeholder={val.placeholder}
  65. className={styles.item}
  66. onChange={val.onChange}
  67. getPopupContainer={triggerNode => triggerNode.parentNode}
  68. >
  69. {val.sel_data.map((items,indexs)=>{
  70. return <Option key={indexs} value={items.key}>{items.name}</Option>
  71. })}
  72. </Select>
  73. )}
  74. </FormItem>
  75. );
  76. }else if(val.type == 'textarea'){
  77. return (<FormItem
  78. key={index}
  79. help={val.help}
  80. extra={val.extra}
  81. label={val.label}>
  82. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  83. <TextArea className={styles.item} disabled={val.is_disable!=undefined&&val.is_disable?true:false} style={{minHeight: 32}} rows={4} placeholder={val.placeholder}/>
  84. )}
  85. </FormItem>);
  86. }else if(val.type == 'rangepicker'){
  87. //时间范围选择器
  88. return (<FormItem
  89. key={index}
  90. style={{width:val.width!=undefined?val.width+80:250}}
  91. extra={val.extra}
  92. label={val.label}>
  93. {getFieldDecorator(val.name)(
  94. <RangePicker
  95. style={{width:val.width!=undefined&&val.width?val.width:'100%'}}
  96. className={styles.item}
  97. placeholder={[val.placeholder1,val.placeholder2,]}
  98. getCalendarContainer={(triggerNode)=>{
  99. return triggerNode.parentNode
  100. }}
  101. />
  102. )}
  103. </FormItem>
  104. );
  105. }else if(val.type == 'datepicker'){
  106. //时间选择器
  107. return (<FormItem key={index} extra={val.extra} label={val.label}>
  108. {val.initialValue&&getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  109. <DatePicker style={{width:180}}
  110. placeholder={val.placeholder}
  111. className={styles.item}
  112. getCalendarContainer={(triggerNode)=>{
  113. return triggerNode.parentNode
  114. }}
  115. />
  116. )}
  117. {!val.initialValue&&getFieldDecorator(val.name,{rules:val.rules})(
  118. <DatePicker style={{width:180}}
  119. placeholder={val.placeholder}
  120. className={styles.item}
  121. getCalendarContainer={(triggerNode)=>{
  122. return triggerNode.parentNode
  123. }}
  124. />
  125. )}
  126. </FormItem>
  127. );
  128. }else if(val.type == 'rangeval'){
  129. //范围选择器
  130. return (<FormItem key={index} extra={val.extra} label={val.label}>
  131. <InputGroup compact className={styles.item}>
  132. {getFieldDecorator([val.name1])(<Input maxLength={250} style={{ width: '40%', textAlign: 'center' }} placeholder={val.placeholder1} />)}
  133. <Input style={{ width: '20%', borderLeft: 0, pointerEvents: 'none', backgroundColor: '#fff' }} placeholder="~" disabled />
  134. {getFieldDecorator([val.name2])(<Input maxLength={250} style={{ width: '40%', textAlign: 'center', borderLeft: 0 }} placeholder={val.placeholder2} />)}
  135. </InputGroup>
  136. </FormItem>
  137. );
  138. }else if(val.type == 'input_after'){
  139. //带图标后缀
  140. return (<FormItem
  141. key={index}
  142. extra={val.extra}
  143. label={val.label}
  144. >
  145. <div onClick={() => val.callback(val.operate_obj)}>
  146. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  147. <Input style={{width:150,marginLeft:3}} disabled={true} addonAfter={sldInputAfterAddons()} placeholder={val.placeholder}/>
  148. )}
  149. </div>
  150. </FormItem>);
  151. }else if(val.type == 'textarea_single'){
  152. return <FormItem
  153. key={index}
  154. extra={val.extra}
  155. label={val.label}>
  156. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  157. <TextArea className={styles.item} style={{minHeight: 30}} rows={1}/>
  158. )}
  159. </FormItem>;
  160. }else if(val.type == 'TreeSelect'){
  161. return <FormItem key={index} extra={val.extra} label={val.label}>
  162. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  163. <TreeSelect
  164. className={styles.item}
  165. treeData={val.data}
  166. showSearch={true}
  167. placeholder={val.placeholder}
  168. allowClear={val.allowClear}
  169. onSelect={val.onSelect}
  170. dropdownStyle={{maxHeight:300}}
  171. getPopupContainer={triggerNode => triggerNode.parentNode}
  172. />
  173. )}
  174. </FormItem>;
  175. }else if(val.type == 'cascader'){
  176. //三级地址选择
  177. return (<FormItem
  178. key={index}
  179. extra={val.extra}
  180. label={val.label}
  181. >
  182. {getFieldDecorator(val.name,{initialValue:val.initialValue,rules:val.rules})(
  183. <Cascader style={{width:200}} options={JSON.parse(localStorage.getItem('common_area_list'))} placeholder={val.placeholder}/>
  184. )}
  185. </FormItem>
  186. );
  187. }else if(val.type == 'single_checkbox'){
  188. //选择框
  189. return (<FormItem
  190. key={index}
  191. extra={val.extra}
  192. label={val.label}
  193. >
  194. {getFieldDecorator(val.name,{valuePropName:'checked',initialValue:val.initialValue,rules:val.rules})(
  195. <Checkbox
  196. >
  197. {val.check_con}
  198. </Checkbox>
  199. )}
  200. </FormItem>
  201. );
  202. }else if(val.type == 'radio'){
  203. //radio
  204. return (<FormItem
  205. key={index}
  206. extra={val.extra}
  207. label={val.label}
  208. style={{width:val.width!=undefined?val.width+80:250}}
  209. >
  210. {getFieldDecorator(val.name,{valuePropName:'checked',rules:val.rules,initialValue:val.initialValue})(
  211. <RadioGroup size={'small'} defaultValue={val.initialValue} style={{width:val.width!=undefined?val.width:'100%'}} onChange={(e)=>this.redioOnChange(e,val)}>
  212. {val.sel_data.map((item,index)=>{
  213. return <Radio key={index} value={item.key}>{item.name}</Radio>
  214. })}
  215. </RadioGroup>
  216. )}
  217. </FormItem>
  218. );
  219. }else if(val.type == 'checkboxgroup'){
  220. //radio
  221. return (<FormItem
  222. key={index}
  223. style={{width:val.width!=undefined?val.width+80:250}}
  224. extra={val.extra}
  225. label={val.label}
  226. >
  227. {getFieldDecorator(val.name,{initialValue: val.initialValue,rules:val.rules})(
  228. <CheckboxGroup style={{width:193}} options={val.sldOptions} onChange={(value) => this.sldCheckShop(val, value)}/>,
  229. )}
  230. </FormItem>
  231. );
  232. }
  233. }
  234. //渲染每一项
  235. renderSearchSecond = (search_data) => {
  236. return search_data.map((item,index)=>{
  237. return this.commonCon(item,index);
  238. });
  239. }
  240. renderSimpleForm() {
  241. const {search_data} = this.props;
  242. return (
  243. search_data.length>0&&
  244. <div style={{display:'block'}}>
  245. <div className={styles.flex_com_row_wrap}>
  246. {this.renderSearchSecond(search_data)}
  247. </div>
  248. </div>
  249. );
  250. }
  251. render() {
  252. return this.renderSimpleForm();
  253. }
  254. }