index.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 } from 'react'
  21. import { Input } from 'antd'
  22. const InputGroup = Input.Group
  23. const styles = require('./NumberRange.less')
  24. interface INumberRangeProps {
  25. placeholder?: string
  26. value?: string[]
  27. size?: 'default' | 'large' | 'small'
  28. onChange?: (value: string[]) => void
  29. onSearch?: (value: string[]) => void
  30. }
  31. interface INumberRangeStates {
  32. value: string[]
  33. }
  34. export class NumberRange extends PureComponent<INumberRangeProps, INumberRangeStates> {
  35. constructor (props) {
  36. super(props)
  37. this.state = {
  38. value: props.value ? props.value.slice() : ['', '']
  39. }
  40. }
  41. private static defaultProps = {
  42. placeholder: '',
  43. size: 'default'
  44. }
  45. public componentWillReceiveProps (nextProps) {
  46. const nextValue = nextProps.value
  47. const { value } = this.state
  48. if (nextValue) {
  49. if (nextValue[0] !== value[0] || nextValue[1] !== value[1]) {
  50. this.setState({
  51. value: nextValue.slice()
  52. })
  53. }
  54. } else {
  55. this.setState({
  56. value: ['', '']
  57. })
  58. }
  59. }
  60. private inputChange = (dir) => {
  61. const { onChange } = this.props
  62. const { value } = this.state
  63. return function (e) {
  64. if (dir === 'from') {
  65. onChange([e.target.value.trim(), value[1]])
  66. } else {
  67. onChange([value[0], e.target.value.trim()])
  68. }
  69. }
  70. }
  71. private inputSearch = () => {
  72. const { onSearch } = this.props
  73. if (onSearch) {
  74. onSearch(this.state.value)
  75. }
  76. }
  77. public render () {
  78. const { placeholder, size } = this.props
  79. const { value } = this.state
  80. return (
  81. <InputGroup className={styles.range} {...(size && { size })} compact>
  82. <Input
  83. className={styles.number}
  84. value={value[0]}
  85. placeholder={`${placeholder || ''}从`}
  86. onChange={this.inputChange('from')}
  87. onPressEnter={this.inputSearch}
  88. />
  89. <Input className={styles.numberDivider} placeholder="-" readOnly tabIndex={-1} />
  90. <Input
  91. className={styles.number}
  92. value={value[1]}
  93. placeholder="到"
  94. onChange={this.inputChange('to')}
  95. onPressEnter={this.inputSearch}
  96. />
  97. </InputGroup>
  98. )
  99. }
  100. }
  101. export default NumberRange