index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, { PureComponent, GetDerivedStateFromProps } from 'react'
  21. import { Select, InputNumber, Input } from 'antd'
  22. import { IRelativeDate } from './types'
  23. import {
  24. RelativeDateType,
  25. RelativeDateTypeLabels,
  26. RelativeDateValueType,
  27. RelativeDateValueTypeLables
  28. } from './constants'
  29. import { getDefaultRelativeDate } from './util'
  30. const Option = Select.Option
  31. const InputGroup = Input.Group
  32. interface IRelativeDatePickerProps {
  33. value?: IRelativeDate
  34. onChange?: (value: IRelativeDate) => void
  35. }
  36. interface IRelativeDatePickerStates {
  37. value: IRelativeDate
  38. }
  39. class IRelativeDatePicker extends PureComponent<
  40. IRelativeDatePickerProps,
  41. IRelativeDatePickerStates
  42. > {
  43. public state: IRelativeDatePickerStates = {
  44. value: getDefaultRelativeDate()
  45. }
  46. public static getDerivedStateFromProps: GetDerivedStateFromProps<
  47. IRelativeDatePickerProps,
  48. IRelativeDatePickerStates
  49. > = (props, state) => {
  50. const { type, valueType, value } = state.value
  51. return {
  52. value: {
  53. type: props.value?.type || type,
  54. valueType: props.value?.valueType || valueType,
  55. value: props.value?.value || value
  56. }
  57. }
  58. }
  59. private changeType = (type) => {
  60. const { onChange } = this.props
  61. const nextValueState = { ...this.state.value, type }
  62. this.setState({
  63. value: nextValueState
  64. })
  65. if (onChange) {
  66. onChange(nextValueState)
  67. }
  68. }
  69. private changeValueType = (valueType) => {
  70. const { onChange } = this.props
  71. const valueState = this.state.value
  72. const nextValueState = {
  73. ...valueState,
  74. valueType,
  75. value:
  76. valueType !== RelativeDateValueType.Current
  77. ? valueState.value === 0
  78. ? 1
  79. : valueState.value
  80. : 0
  81. }
  82. this.setState({
  83. value: nextValueState
  84. })
  85. if (onChange) {
  86. onChange(nextValueState)
  87. }
  88. }
  89. private changeValue = (val) => {
  90. const { onChange } = this.props
  91. const nextValueState = { ...this.state.value, value: val }
  92. this.setState({
  93. value: nextValueState
  94. })
  95. if (onChange) {
  96. onChange(nextValueState)
  97. }
  98. }
  99. public render() {
  100. const { type, valueType, value } = this.state.value
  101. const relativeDateTypeOptions = Object.entries(RelativeDateTypeLabels).map(
  102. ([val, label]) => (
  103. <Option key={val} value={val}>
  104. {label}
  105. </Option>
  106. )
  107. )
  108. const relativeDateValueTypeOptions = Object.entries(
  109. RelativeDateValueTypeLables
  110. ).map(([val, label]) => (
  111. <Option key={val} value={val}>
  112. {val === RelativeDateValueType.Current ? label[type] : label}
  113. </Option>
  114. ))
  115. const selectStyle = {
  116. width: valueType !== RelativeDateValueType.Current ? '33%' : '50%'
  117. }
  118. return (
  119. <InputGroup compact>
  120. <Select
  121. dropdownMatchSelectWidth={false}
  122. value={valueType}
  123. onChange={this.changeValueType}
  124. style={selectStyle}
  125. >
  126. {relativeDateValueTypeOptions}
  127. </Select>
  128. {valueType !== RelativeDateValueType.Current && (
  129. <InputNumber
  130. min={1}
  131. precision={0}
  132. value={value}
  133. onChange={this.changeValue}
  134. style={{ width: '34%' }}
  135. />
  136. )}
  137. <Select
  138. dropdownMatchSelectWidth={false}
  139. value={type}
  140. onChange={this.changeType}
  141. style={selectStyle}
  142. >
  143. {relativeDateTypeOptions}
  144. </Select>
  145. </InputGroup>
  146. )
  147. }
  148. }
  149. export default IRelativeDatePicker