SizePanel.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import React from 'react'
  2. import { IDataParamSource } from './Dropbox'
  3. import { getAggregatorLocale, decodeMetricName } from '../util'
  4. import { PIVOT_DEFAULT_SCATTER_SIZE_TIMES } from 'app/globalConstants'
  5. import { Tabs, Slider } from 'antd'
  6. const TabPane = Tabs.TabPane
  7. const styles = require('./Workbench.less')
  8. const utilStyles = require('assets/less/util.less')
  9. interface ISizePanelProps {
  10. list: IDataParamSource[]
  11. value: object
  12. hasTabs: boolean
  13. onValueChange: (key: string, value: string) => void
  14. }
  15. interface ISizePanelStates {
  16. size: number
  17. selectedTab: string
  18. }
  19. export class SizePanel extends React.PureComponent<ISizePanelProps, ISizePanelStates> {
  20. constructor (props) {
  21. super(props)
  22. this.state = {
  23. size: PIVOT_DEFAULT_SCATTER_SIZE_TIMES,
  24. selectedTab: 'all'
  25. }
  26. }
  27. public componentWillMount () {
  28. this.initSize(this.props.value)
  29. }
  30. public componentWillReceiveProps (nextProps) {
  31. if (nextProps.list !== this.props.list) {
  32. this.setState({
  33. selectedTab: 'all',
  34. size: nextProps.value['all']
  35. })
  36. } else {
  37. this.initSize(nextProps.value)
  38. }
  39. }
  40. private initSize = (value) => {
  41. this.setState({
  42. size: value[this.state.selectedTab]
  43. })
  44. }
  45. private tabSelect = (key) => {
  46. const { value } = this.props
  47. this.setState({
  48. selectedTab: key,
  49. size: value[key] || PIVOT_DEFAULT_SCATTER_SIZE_TIMES
  50. })
  51. }
  52. private sizeChange = (value) => {
  53. this.props.onValueChange(this.state.selectedTab, value)
  54. }
  55. public render () {
  56. const { list, hasTabs } = this.props
  57. const { size, selectedTab } = this.state
  58. const tabPanes = [(
  59. <TabPane tab="应用全部" key="all" />
  60. )].concat(list.map((l) => (
  61. <TabPane
  62. tab={`[${getAggregatorLocale(l.agg)}]
  63. ${decodeMetricName(l.name)}`}
  64. key={l.name}
  65. />
  66. )))
  67. return (
  68. <div className={styles.sizePanel}>
  69. <Tabs
  70. size="small"
  71. activeKey={selectedTab}
  72. onChange={this.tabSelect}
  73. className={!hasTabs ? utilStyles.hide : ''}
  74. >
  75. {tabPanes}
  76. </Tabs>
  77. <Slider min={1} max={10} value={size} onChange={this.sizeChange} />
  78. </div>
  79. )
  80. }
  81. }
  82. export default SizePanel