Item.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import React, { useContext } from 'react'
  21. import classnames from 'classnames'
  22. import {
  23. Input,
  24. InputNumber,
  25. Radio,
  26. Checkbox,
  27. Select,
  28. Form,
  29. Icon,
  30. Col
  31. } from 'antd'
  32. const RadioGroup = Radio.Group
  33. const CheckboxGroup = Checkbox.Group
  34. const { Option } = Select
  35. const FormItem = Form.Item
  36. import { GetFieldDecoratorOptions } from 'antd/lib/form/Form'
  37. import ColorPicker from 'components/ColorPicker'
  38. import Upload from 'components/Upload'
  39. import IconFont from 'components/IconFont'
  40. import { SettingItem } from './types'
  41. import { SlideSettingContext } from './util'
  42. import api from 'utils/api'
  43. import utilStyles from 'assets/less/util.less'
  44. import { ASSETS_HOST } from 'app/globalConstants'
  45. interface IItemProps {
  46. item: SettingItem
  47. }
  48. const Item: React.FC<IItemProps> = (props) => {
  49. const { item } = props
  50. const { form, size, slideId, layerId } = useContext(SlideSettingContext)
  51. let visible = true
  52. const { relatedItems } = item
  53. if (Array.isArray(relatedItems)) {
  54. relatedItems.some(({ name, values }) => {
  55. const relatedValue = form.getFieldValue(name)
  56. if (values.findIndex((val) => val === relatedValue) < 0) {
  57. visible = false
  58. return true
  59. }
  60. })
  61. }
  62. const itemCls = classnames({
  63. [utilStyles.hide]: !visible
  64. })
  65. const { getFieldDecorator } = form
  66. const options: GetFieldDecoratorOptions = { initialValue: item.default }
  67. let control: React.ReactNode
  68. switch (item.component) {
  69. case 'input':
  70. control = <Input size={size} placeholder={item.placeholder} />
  71. break
  72. case 'inputnumber':
  73. control = (
  74. <InputNumber
  75. size={size}
  76. placeholder={item.placeholder}
  77. min={item.min === undefined ? -Infinity : item.min}
  78. max={item.max === undefined ? Infinity : item.max}
  79. />
  80. )
  81. break
  82. case 'radio':
  83. control = (
  84. <RadioGroup size={size}>
  85. {item.values.map(({ value, name }) => (
  86. <Radio key={value} value={value}>
  87. {name}
  88. </Radio>
  89. ))}
  90. </RadioGroup>
  91. )
  92. break
  93. case 'checkbox':
  94. control = <Checkbox />
  95. options.valuePropName = item.valuePropName
  96. break
  97. case 'checkboxGroup':
  98. control = <CheckboxGroup options={item.values} />
  99. break
  100. case 'select':
  101. control = (
  102. <Select size={size}>
  103. {item.values.map(({ name, value }) => (
  104. <Option key={value} value={value}>
  105. {name}
  106. </Option>
  107. ))}
  108. </Select>
  109. )
  110. break
  111. case 'colorPicker':
  112. let color = form.getFieldValue(item.name)
  113. if (color) {
  114. color = `rgba(${color.join()})`
  115. }
  116. control = (
  117. <ColorPicker rawValue size={size}>
  118. <IconFont
  119. type='icon-palette'
  120. className='display-setting-form-palette'
  121. // style={{ color }}
  122. />
  123. </ColorPicker>
  124. )
  125. break
  126. case 'upload':
  127. const action = `${api.display}/${item.action}`
  128. .replace(/({slideId})/, slideId ? `${slideId}` : '')
  129. .replace(/({layerId})/, layerId ? `${layerId}` : '')
  130. const img = form.getFieldValue(item.name)
  131. control = (
  132. <Upload name={item.name} action={action}>
  133. {img ? (
  134. <div className='display-setting-form-img'>
  135. <img src={ASSETS_HOST + img} alt={item.title} />
  136. <Icon
  137. type='delete'
  138. onClick={(e) => {
  139. e.stopPropagation()
  140. form.setFieldsValue({ [item.name]: null })
  141. }}
  142. />
  143. </div>
  144. ) : (
  145. <Icon type='plus' />
  146. )}
  147. </Upload>
  148. )
  149. break
  150. }
  151. const { labelCol, wrapperCol, span } = item
  152. return (
  153. <Col span={span || 24} className={itemCls}>
  154. <FormItem
  155. labelCol={{ span: labelCol || 12 }}
  156. wrapperCol={{ span: wrapperCol || 12 }}
  157. label={item.title}
  158. >
  159. {getFieldDecorator(item.name, options)(control)}
  160. </FormItem>
  161. </Col>
  162. )
  163. }
  164. export default Item