index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React, { Component } from 'react'
  2. import { Modal, message } from 'antd'
  3. import { FormItem } from 'wptpc-design'
  4. class Index extends Component {
  5. state = {
  6. data: null,
  7. params: null
  8. };
  9. constructor (props) {
  10. super(props)
  11. let params = null
  12. if (props.params) {
  13. params = props.params
  14. }
  15. this.state = {
  16. params: {
  17. ...params
  18. }
  19. }
  20. }
  21. // 统一change
  22. onParamsChange = (k, v) => {
  23. const { params } = this.state
  24. const newParams = { ...params }
  25. this.setState(
  26. {
  27. params: { ...newParams, [k]: v }
  28. }
  29. )
  30. };
  31. onOk = (cb) => {
  32. if (!this.getCheck()) {
  33. return
  34. }
  35. const { params = {} } = this.state
  36. if (Object.values(params).some(item => item === '0')) {
  37. message.warning('内容数字不可以为 0!')
  38. return
  39. }
  40. if (typeof this.props.onOk === 'function') { this.props.onOk({ ...params }) }
  41. if (typeof cb === 'function') {
  42. // eslint-disable-next-line standard/no-callback-literal
  43. cb({ ...params })
  44. }
  45. };
  46. // 设置属性字段
  47. setDetail (v, k) {
  48. const { params } = this.state
  49. if (params) {
  50. params[k] = v
  51. }
  52. this.setState({
  53. params: params
  54. })
  55. }
  56. onChange = (e, key) => {
  57. const { value } = e.target
  58. const reg = /^-?[0-9]*(\.[0-9]*)?$/
  59. if ((!isNaN(value) && reg.test(value)) || value === '' || value === '-') {
  60. this.setDetail(value, key)
  61. }
  62. };
  63. render () {
  64. const {
  65. params = {}
  66. } = this.state
  67. const { onCancel, showModal } = this.props
  68. this.formSetting = [
  69. {
  70. label: '压测链接数',
  71. key: 'connects',
  72. value: params.connects,
  73. placeholder: '请输入 压测链接数 (仅数字有效)',
  74. isRequired: true,
  75. type: 'input',
  76. onChange: (e) => { this.onChange(e, 'connects') }
  77. },
  78. {
  79. label: '压测线程数',
  80. key: 'threads',
  81. value: params.threads,
  82. placeholder: '请输入 压测线程数 (仅数字有效)',
  83. isRequired: true,
  84. type: 'input',
  85. onChange: (e) => { this.onChange(e, 'threads') }
  86. },
  87. {
  88. label: '接口超过时间',
  89. key: 'timeout',
  90. value: params.timeout,
  91. placeholder: '请输入 接口超过时间 (仅数字有效)',
  92. isRequired: true,
  93. type: 'input',
  94. onChange: (e) => { this.onChange(e, 'timeout') }
  95. },
  96. {
  97. label: '压测持续时间',
  98. key: 'duration',
  99. value: params.duration,
  100. placeholder: '请输入 压测持续时间 (仅数字有效)',
  101. isRequired: true,
  102. type: 'input',
  103. onChange: (e) => { this.onChange(e, 'duration') }
  104. }
  105. ]
  106. return (
  107. <Modal
  108. title={'添加 case'}
  109. visible={showModal}
  110. width={900}
  111. onOk={this.onOk}
  112. onCancel={onCancel}
  113. >
  114. <FormItem
  115. getCheck={(cb) => {
  116. this.getCheck = cb
  117. }}
  118. onChange={this.onParamsChange}
  119. formSetting={this.formSetting}
  120. params={params}
  121. />
  122. </Modal>
  123. )
  124. }
  125. }
  126. export default Index