Slider.tsx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 { Slider as AntSlider } from 'antd'
  22. import { SliderValue } from 'antd/lib/slider'
  23. import { IControl } from '../types'
  24. import { metricAxisLabelFormatter } from 'app/containers/Widget/components/util'
  25. interface ISliderProps {
  26. control: Omit<IControl, 'relatedItems' | 'relatedViews'>
  27. value?: any
  28. onChange?: (value: SliderValue) => void
  29. }
  30. interface ISliderStates {
  31. value?: any
  32. prevValueProp: any
  33. }
  34. class Slider extends PureComponent<ISliderProps, ISliderStates> {
  35. public state: ISliderStates = {
  36. value: void 0,
  37. prevValueProp: void 0
  38. }
  39. public static getDerivedStateFromProps: GetDerivedStateFromProps<
  40. ISliderProps,
  41. ISliderStates
  42. > = (props, state) => {
  43. if (state.prevValueProp !== props.value) {
  44. return {
  45. prevValueProp: props.value,
  46. value: props.value
  47. }
  48. }
  49. return null
  50. }
  51. private sliderChange = (value) => {
  52. this.setState({ value })
  53. }
  54. private sliderAfterChange = (value) => {
  55. this.props.onChange(value)
  56. }
  57. private getTooltipPopupContainer = (node) => node.parentNode
  58. public render() {
  59. const { control } = this.props
  60. const { value } = this.state
  61. const { max, min, step, label } = control
  62. const marks = label
  63. ? {
  64. marks: {
  65. [min]: metricAxisLabelFormatter(min),
  66. [max]: metricAxisLabelFormatter(max),
  67. ...(value && {
  68. [value[0]]: metricAxisLabelFormatter(value[0]),
  69. [value[1]]: metricAxisLabelFormatter(value[1])
  70. })
  71. }
  72. }
  73. : void 0
  74. return (
  75. <AntSlider
  76. max={max}
  77. min={min}
  78. step={step}
  79. value={value}
  80. onChange={this.sliderChange}
  81. onAfterChange={this.sliderAfterChange}
  82. getTooltipPopupContainer={this.getTooltipPopupContainer}
  83. {...marks}
  84. range
  85. />
  86. )
  87. }
  88. }
  89. export default Slider