ActOnSettingForm.tsx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import React from 'react'
  2. import { IDataParamSource, IDataParamConfig } from './Dropbox'
  3. import { decodeMetricName } from '../util'
  4. import { Radio, Button } from 'antd'
  5. const RadioGroup = Radio.Group
  6. const styles = require('./Workbench.less')
  7. interface IActOnSettingFormProps {
  8. list: IDataParamSource[]
  9. config: IDataParamConfig
  10. onSave: (config: IDataParamConfig) => void
  11. onCancel: () => void
  12. }
  13. interface IActOnSettingFormStates {
  14. actOn: string
  15. list: IDataParamSource[]
  16. }
  17. export class ActOnSettingForm extends React.PureComponent<IActOnSettingFormProps, IActOnSettingFormStates> {
  18. constructor (props) {
  19. super(props)
  20. this.state = {
  21. actOn: 'all',
  22. list: []
  23. }
  24. }
  25. public componentWillMount () {
  26. const { config } = this.props
  27. if (config.actOn) {
  28. this.setState({
  29. actOn: config.actOn
  30. })
  31. }
  32. }
  33. public componentWillReceiveProps (nextProps) {
  34. const { list, config } = nextProps
  35. if (list && config) {
  36. this.setState({
  37. list: list.slice(),
  38. actOn: config.actOn || this.state.actOn
  39. })
  40. }
  41. }
  42. private radioChange = (e) => {
  43. this.setState({
  44. actOn: e.target.value
  45. })
  46. }
  47. private save = () => {
  48. this.props.onSave({
  49. actOn: this.state.actOn
  50. })
  51. }
  52. public reset = () => {
  53. this.setState({
  54. actOn: 'all'
  55. })
  56. }
  57. public render () {
  58. const { onCancel } = this.props
  59. const { list, actOn } = this.state
  60. const radioList = [{ name: 'all' }].concat(list).map((l) => (
  61. <Radio key={l.name} value={l.name}>
  62. {l.name === 'all' ? l.name : decodeMetricName(l.name)}
  63. </Radio>
  64. ))
  65. return (
  66. <div className={styles.actOnSettingForm}>
  67. <RadioGroup onChange={this.radioChange} value={actOn}>
  68. {radioList}
  69. </RadioGroup>
  70. <div className={styles.footer}>
  71. <Button type="primary" onClick={this.save}>保存</Button>
  72. <Button onClick={onCancel}>取消</Button>
  73. </div>
  74. </div>
  75. )
  76. }
  77. }
  78. export default ActOnSettingForm