Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. import React from 'react'
  2. import cx from 'classnames'
  3. import { Form, Select, Input, Switch, DatePicker, InputNumber, Radio } from 'antd'
  4.  
  5. import { transformValue, getValidateRules, getValueFromEvent, getTrigger, getValuePropName } from './utils'
  6. import { Upload } from 'mainComponents/Upload'
  7. import { TopicSearchBox } from 'mainComponents/SearchBox'
  8. import styles from './form.styl'
  9.  
  10. const Option = Select.Option
  11. const FormItem = Form.Item
  12. const RadioButton = Radio.Button
  13. const RadioGroup = Radio.Group
  14. const RangePicker = DatePicker.RangePicker
  15.  
  16. export function getFormField (property, form, content, layout) {
  17. const { getFieldDecorator, getFieldsValue, setFieldsValue } = form
  18. property = {
  19. visible: () => true,
  20. disable: () => false,
  21. valid: () => true,
  22. ...property
  23. }
  24. const formItemLayout = layout === 'horizontal' ? {
  25. labelCol: {
  26. xs: { span: 24 },
  27. sm: { span: 6 }
  28. },
  29. wrapperCol: {
  30. xs: { span: 24 },
  31. sm: { span: 14 }
  32. }
  33. } : null
  34. const values = getFieldsValue()
  35. const isValid = property.valid(values)
  36. const InputComponent = getFieldInput(property)
  37. const InputComponentProps = {
  38. disabled: property.disable(values),
  39. setFieldsValue
  40. }
  41. let initialValue = getContentValue(content, property.field) === undefined ? property.defaultValue : transformValue(getContentValue(content, property.field), property)
  42. return (
  43. isValid ? (
  44. <FormItem
  45. { ...formItemLayout }
  46. className={ cx({ [styles.hidden]: !(property.visible(values)) }) }
  47. label={ property.title }
  48. key={ property.field }
  49. >
  50. {
  51. getFieldDecorator(property.field, {
  52. rules: getValidateRules(property.rules),
  53. initialValue,
  54. valuePropName: getValuePropName(property.format),
  55. trigger: getTrigger(property.format),
  56. getValueFromEvent: getValueFromEvent(property.format)
  57. })(
  58. React.cloneElement(InputComponent, InputComponentProps)
  59. )
  60. }
  61. </FormItem>
  62. ) : null
  63. )
  64. }
  65.  
  66. export function getFieldInput (property) {
  67. return property.component ? property.component : getCommonFiledInput(property)
  68. }
  69.  
  70. export function getContentValue (content, field) {
  71. const paths = field.split('.')
  72. let value = content
  73. paths.forEach(path => { value = value[path] })
  74. return value
  75. }
  76.  
  77. function getCommonFiledInput (property) {
  78. let source = property.source
  79. if (property.source && typeof property.source === 'function') {
  80. source = property.source()
  81. }
  82. // 常用的类型组件
  83. switch (property.format) {
  84. case 'Select':
  85. return (
  86. <Select>
  87. {
  88. source.map(option => <Option key={ option.key } value={ option.value }>{ option.title }</Option>)
  89. }
  90. </Select>
  91. )
  92. case 'multiple':
  93. return (
  94. <Select
  95. mode='multiple'
  96. placeholder={ property.placeholder }
  97. >
  98. {
  99. source.map(option => <Option key={ option.key } value={ option.value }>{ option.title }</Option>)
  100. }
  101. </Select>
  102. )
  103. case 'tags':
  104. return (
  105. <Select
  106. mode='tags'
  107. placeholder={ property.placeholder }
  108. style={ { minWidth: 150 } }
  109. >
  110. {
  111. source.map(option => <Option key={ option.key } value={ option.value }>{ option.title }</Option>)
  112. }
  113. </Select>
  114. )
  115. case 'Input':
  116. return (
  117. <Input placeholder={ property.placeholder } />
  118. )
  119. case 'TextArea':
  120. // 该type 2.12 后废弃,请直接使用 Input.TextArea
  121. return (
  122. <Input type='textarea' placeholder={ property.placeholder } />
  123. )
  124. case 'InputNumber':
  125. return (
  126. <InputNumber />
  127. )
  128. case 'Switch':
  129. return (
  130. <Switch />
  131. )
  132. case 'TopicSearchBox':
  133. return (
  134. <TopicSearchBox />
  135. )
  136. case 'Upload':
  137. return (
  138. <Upload />
  139. )
  140. case 'DatePicker':
  141. return (
  142. <DatePicker format='YYYY-MM-DD HH:mm' showTime disabledDate={ property.disabledDate } />
  143. )
  144. case 'RangePicker':
  145. return <RangePicker format='YYYY-MM-DD HH:mm' />
  146. case 'RadioGroup':
  147. return (
  148. <RadioGroup>
  149. {
  150. source.map(option => <RadioButton key={ option.key } value={ option.value }>{ option.title }</RadioButton>)
  151. }
  152. </RadioGroup>
  153. )
  154. default:
  155. return <span>没有对应input 组件</span>
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement