Condition.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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, {
  21. PureComponent,
  22. GetDerivedStateFromProps,
  23. ChangeEvent
  24. } from 'react'
  25. import { Input, Select } from 'antd'
  26. import { IControl, IControlCondition } from '../../types'
  27. import { getOperatorOptions } from '../../util'
  28. import OperatorTypes, { OperatorTypesLocale } from 'app/utils/operatorTypes'
  29. const InputGroup = Input.Group
  30. const Option = Select.Option
  31. interface IConditionProps {
  32. controls: IControl[]
  33. value?: IControlCondition
  34. onChange?: (value: IControlCondition) => void
  35. }
  36. interface IConditionStates {
  37. operatorOptions: OperatorTypes[]
  38. previousValue: IControlCondition
  39. }
  40. class Condition extends PureComponent<IConditionProps, IConditionStates> {
  41. public state: IConditionStates = {
  42. operatorOptions: [],
  43. previousValue: void 0
  44. }
  45. public static getDerivedStateFromProps: GetDerivedStateFromProps<
  46. IConditionProps,
  47. IConditionStates
  48. > = (props, state) => {
  49. const { controls, value } = props
  50. let nextState: Partial<IConditionStates> = {}
  51. if (props.value !== state.previousValue) {
  52. nextState = { previousValue: props.value }
  53. }
  54. if (value?.control && value?.control !== state.previousValue?.control) {
  55. const control = controls.find((c) => c.key === value.control)
  56. if (control) {
  57. nextState = {
  58. ...nextState,
  59. operatorOptions: getOperatorOptions(control.type, control.multiple)
  60. }
  61. }
  62. }
  63. return nextState
  64. }
  65. private controlChange = (val) => {
  66. const { controls, value, onChange } = this.props
  67. const currentControlKey = value?.control
  68. let operator = value?.operator
  69. if (currentControlKey && val) {
  70. const currentControl = controls.find((c) => c.key === currentControlKey)
  71. const selectedControl = controls.find((c) => c.key === val)
  72. if (
  73. currentControl &&
  74. !!currentControl.multiple !== !!selectedControl.multiple
  75. ) {
  76. operator = void 0
  77. }
  78. }
  79. onChange({ ...value, control: val, operator })
  80. }
  81. private operatorChange = (val) => {
  82. const { value, onChange } = this.props
  83. onChange({ ...value, operator: val })
  84. }
  85. private valueChange = (e: ChangeEvent<HTMLInputElement>) => {
  86. const { value, onChange } = this.props
  87. onChange({ ...value, value: e.target.value })
  88. }
  89. public render() {
  90. const { controls, value } = this.props
  91. const { operatorOptions } = this.state
  92. const controlKey = value?.control
  93. const operator = value?.operator
  94. const conditionValue = value?.value
  95. return (
  96. <InputGroup compact>
  97. <Select
  98. placeholder="关联控件"
  99. value={controlKey}
  100. dropdownMatchSelectWidth={false}
  101. style={{ width: '40%' }}
  102. onChange={this.controlChange}
  103. allowClear
  104. >
  105. {controls.map(({ key, name }) => (
  106. <Option key={key} value={key}>
  107. {name}
  108. </Option>
  109. ))}
  110. </Select>
  111. <Select
  112. placeholder="关系"
  113. value={operator}
  114. dropdownMatchSelectWidth={false}
  115. style={{ width: '30%' }}
  116. onChange={this.operatorChange}
  117. allowClear
  118. >
  119. {operatorOptions.map((o) => (
  120. <Option key={o} value={o}>
  121. {OperatorTypesLocale[o]}
  122. </Option>
  123. ))}
  124. </Select>
  125. <Input
  126. placeholder="值"
  127. value={conditionValue}
  128. style={{ width: '30%' }}
  129. onChange={this.valueChange}
  130. />
  131. </InputGroup>
  132. )
  133. }
  134. }
  135. export default Condition