Item.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. interface IItemProps {
  45. item: SettingItem
  46. }
  47. const Item: React.FC<IItemProps> = (props) => {
  48. const { item } = props
  49. const { form, size, slideId, layerId } = useContext(SlideSettingContext)
  50. let visible = true
  51. const { relatedItems } = item
  52. if (Array.isArray(relatedItems)) {
  53. relatedItems.some(({ name, values }) => {
  54. const relatedValue = form.getFieldValue(name)
  55. if (values.findIndex((val) => val === relatedValue) < 0) {
  56. visible = false
  57. return true
  58. }
  59. })
  60. }
  61. const itemCls = classnames({
  62. [utilStyles.hide]: !visible
  63. })
  64. const { getFieldDecorator } = form
  65. const options: GetFieldDecoratorOptions = { initialValue: item.default }
  66. let control: React.ReactNode
  67. switch (item.component) {
  68. case 'input':
  69. control = <Input size={size} placeholder={item.placeholder} />
  70. break
  71. case 'inputnumber':
  72. control = (
  73. <InputNumber
  74. size={size}
  75. placeholder={item.placeholder}
  76. min={item.min === undefined ? -Infinity : item.min}
  77. max={item.max === undefined ? Infinity : item.max}
  78. />
  79. )
  80. break
  81. case 'radio':
  82. control = (
  83. <RadioGroup size={size}>
  84. {item.values.map(({ value, name }) => (
  85. <Radio key={value} value={value}>
  86. {name}
  87. </Radio>
  88. ))}
  89. </RadioGroup>
  90. )
  91. break
  92. case 'checkbox':
  93. control = <Checkbox />
  94. options.valuePropName = item.valuePropName
  95. break
  96. case 'checkboxGroup':
  97. control = <CheckboxGroup options={item.values} />
  98. break
  99. case 'select':
  100. control = (
  101. <Select size={size}>
  102. {item.values.map(({ name, value }) => (
  103. <Option key={value} value={value}>
  104. {name}
  105. </Option>
  106. ))}
  107. </Select>
  108. )
  109. break
  110. case 'colorPicker':
  111. let color = form.getFieldValue(item.name)
  112. if (color) {
  113. color = `rgba(${color.join()})`
  114. }
  115. control = (
  116. <ColorPicker rawValue size={size}>
  117. <IconFont
  118. type="icon-palette"
  119. className="display-setting-form-palette"
  120. style={{ color }}
  121. />
  122. </ColorPicker>
  123. )
  124. break
  125. case 'upload':
  126. const action = `${api.display}/${item.action}`
  127. .replace(/({slideId})/, slideId ? `${slideId}` : '')
  128. .replace(/({layerId})/, layerId ? `${layerId}` : '')
  129. const img = form.getFieldValue(item.name)
  130. control = (
  131. <Upload name={item.name} action={action}>
  132. {img ? (
  133. <div className="display-setting-form-img">
  134. <img src={img} alt={item.title} />
  135. <Icon
  136. type="delete"
  137. onClick={(e) => {
  138. e.stopPropagation()
  139. form.setFieldsValue({ [item.name]: null })
  140. }}
  141. />
  142. </div>
  143. ) : (
  144. <Icon type="plus" />
  145. )}
  146. </Upload>
  147. )
  148. break
  149. }
  150. const { labelCol, wrapperCol, span } = item
  151. return (
  152. <Col span={span || 24} className={itemCls}>
  153. <FormItem
  154. labelCol={{ span: labelCol || 12 }}
  155. wrapperCol={{ span: wrapperCol || 12 }}
  156. label={item.title}
  157. >
  158. {getFieldDecorator(item.name, options)(control)}
  159. </FormItem>
  160. </Col>
  161. )
  162. }
  163. export default Item