index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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, { Component, PureComponent, Suspense, ReactNode } from 'react'
  21. import classnames from 'classnames'
  22. import InputText from './InputText'
  23. import NumberRange from 'components/NumberRange'
  24. import Select from './Select'
  25. import Radio from './Radio'
  26. import TreeSelect from './TreeSelect'
  27. import Date from './Date'
  28. import DateRange from './DateRange'
  29. import Slider from './Slider'
  30. import { IControl, IControlOption } from '../types'
  31. import { ControlTypes, ControlQueryMode } from '../constants'
  32. import { Form } from 'antd'
  33. import { TreeNode } from 'antd/lib/tree-select'
  34. import { RadioChangeEvent } from 'antd/lib/radio'
  35. import { transformOptions } from '../util'
  36. const FormItem = Form.Item
  37. import styles from '../Panel/Layouts/Layouts.less'
  38. interface IParentInfo {
  39. control: IControl
  40. value: any
  41. }
  42. interface IControlProps {
  43. queryMode: ControlQueryMode
  44. control: IControl
  45. value: any
  46. size?: 'default' | 'large' | 'small'
  47. currentOptions: Array<object | IControlOption>
  48. onChange: (control: IControl, value) => void
  49. onSearch: (changedValues?: object) => void
  50. }
  51. export class Control extends PureComponent<IControlProps, {}> {
  52. public static defaultProps = {
  53. size: 'default'
  54. }
  55. private renderControl = (control: IControl) => {
  56. const { currentOptions, value, size } = this.props
  57. let component
  58. switch (control.type) {
  59. case ControlTypes.InputText:
  60. component = (
  61. <InputText
  62. value={value}
  63. size={size}
  64. onChange={this.inputChange}
  65. onSearch={this.search}
  66. />
  67. )
  68. break
  69. case ControlTypes.NumberRange:
  70. component = (
  71. <NumberRange
  72. value={value}
  73. size={size}
  74. onChange={this.change}
  75. onSearch={this.search}
  76. />
  77. )
  78. break
  79. case ControlTypes.Slider:
  80. component = (
  81. <Slider
  82. control={control}
  83. value={value}
  84. onChange={this.change}
  85. />
  86. )
  87. break
  88. case ControlTypes.Select:
  89. component = (
  90. <Select
  91. control={control}
  92. value={value}
  93. size={size}
  94. onChange={this.change}
  95. options={
  96. transformOptions(
  97. control,
  98. currentOptions || []
  99. ) as IControlOption[]
  100. }
  101. />
  102. )
  103. break
  104. case ControlTypes.Radio:
  105. component = (
  106. <Radio
  107. control={control}
  108. value={value}
  109. size={size}
  110. onChange={this.radioChange}
  111. options={
  112. transformOptions(
  113. control,
  114. currentOptions || []
  115. ) as IControlOption[]
  116. }
  117. />
  118. )
  119. break
  120. case ControlTypes.TreeSelect:
  121. component = (
  122. <TreeSelect
  123. control={control}
  124. value={value}
  125. size={size}
  126. onChange={this.change}
  127. options={
  128. transformOptions(control, currentOptions || []) as TreeNode[]
  129. }
  130. />
  131. )
  132. break
  133. case ControlTypes.Date:
  134. component = (
  135. <Date
  136. control={control}
  137. value={value}
  138. size={size}
  139. onChange={this.change}
  140. />
  141. )
  142. break
  143. case ControlTypes.DateRange:
  144. component = (
  145. <DateRange
  146. control={control}
  147. value={value}
  148. size={size}
  149. onChange={this.change}
  150. />
  151. )
  152. break
  153. }
  154. return this.wrapFormItem(control, component)
  155. }
  156. private wrapFormItem = (
  157. control: IControl,
  158. component: Component
  159. ): ReactNode => {
  160. const { size } = this.props
  161. const itemClassNames = classnames({
  162. [styles.controlItem]: true,
  163. [styles.small]: size === 'small'
  164. })
  165. return (
  166. <FormItem label={control.name} className={itemClassNames}>
  167. {component}
  168. </FormItem>
  169. )
  170. }
  171. private change = (val) => {
  172. const { control, onChange } = this.props
  173. onChange(control, val)
  174. }
  175. private inputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  176. const { control, onChange } = this.props
  177. onChange(control, e.target.value.trim())
  178. }
  179. private radioChange = (e: RadioChangeEvent) => {
  180. const { control, onChange } = this.props
  181. onChange(control, e.target.value)
  182. }
  183. private search = (val) => {
  184. const { queryMode, control, onSearch } = this.props
  185. if (queryMode === ControlQueryMode.Immediately) {
  186. onSearch({ [control.key]: val })
  187. } else {
  188. onSearch()
  189. }
  190. }
  191. public render() {
  192. const { control } = this.props
  193. return <Suspense fallback={null}>{this.renderControl(control)}</Suspense>
  194. }
  195. }
  196. export default Control