mock平台

MethodsList.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Row, Icon, Input, Select, Tooltip } from 'antd';
  4. import _ from 'underscore';
  5. const Option = Select.Option;
  6. // 深拷贝
  7. function deepEqual(state) {
  8. return JSON.parse(JSON.stringify(state));
  9. }
  10. const METHODS_LIST = [
  11. { name: 'md5', type: false, params: [], desc: 'md5加密' },
  12. { name: 'lower', type: false, params: [], desc: '所有字母变成小写' },
  13. { name: 'length', type: false, params: [], desc: '数据长度' },
  14. { name: 'substr', type: true, component: 'doubleInput', params: [], desc: '截取部分字符串' },
  15. { name: 'sha', type: true, component: 'select', params: ['sha1'], desc: 'sha加密' },
  16. { name: 'base64', type: false, params: [], desc: 'base64加密' },
  17. { name: 'unbase64', type: false, params: [], desc: 'base64解密' },
  18. { name: 'concat', type: true, component: 'input', params: [], desc: '连接字符串' },
  19. { name: 'lconcat', type: true, component: 'input', params: [], desc: '左连接' },
  20. { name: 'upper', type: false, desc: '所有字母变成大写' },
  21. { name: 'number', type: false, desc: '字符串转换为数字类型' }
  22. ];
  23. class MethodsList extends Component {
  24. static propTypes = {
  25. show: PropTypes.bool,
  26. click: PropTypes.func,
  27. clickValue: PropTypes.string,
  28. paramsInput: PropTypes.func,
  29. clickIndex: PropTypes.number,
  30. params: PropTypes.array
  31. };
  32. constructor(props) {
  33. super(props);
  34. this.state = {
  35. list: METHODS_LIST,
  36. moreFlag: true
  37. };
  38. }
  39. showMore = () => {
  40. this.setState({
  41. moreFlag: false
  42. });
  43. };
  44. componentDidMount() {
  45. var index = _.findIndex(METHODS_LIST, { name: this.props.clickValue });
  46. let moreFlag = index > 3 ? false : true;
  47. this.setState({
  48. moreFlag
  49. });
  50. }
  51. inputComponent = props => {
  52. let clickIndex = props.clickIndex;
  53. let paramsIndex = props.paramsIndex;
  54. let params = props.params;
  55. return (
  56. <Input
  57. size="small"
  58. placeholder="请输入参数"
  59. value={params[0]}
  60. onChange={e => this.handleParamsChange(e.target.value, clickIndex, paramsIndex, 0)}
  61. />
  62. );
  63. };
  64. doubleInputComponent = props => {
  65. let clickIndex = props.clickIndex;
  66. let paramsIndex = props.paramsIndex;
  67. let params = props.params;
  68. return (
  69. <div>
  70. <Input
  71. size="small"
  72. placeholder="start"
  73. value={params[0]}
  74. onChange={e => this.handleParamsChange(e.target.value, clickIndex, paramsIndex, 0)}
  75. />
  76. <Input
  77. size="small"
  78. placeholder="length"
  79. value={params[1]}
  80. onChange={e => this.handleParamsChange(e.target.value, clickIndex, paramsIndex, 1)}
  81. />
  82. </div>
  83. );
  84. };
  85. selectComponent = props => {
  86. const subname = ['sha1', 'sha224', 'sha256', 'sha384', 'sha512'];
  87. let clickIndex = props.clickIndex;
  88. let paramsIndex = props.paramsIndex;
  89. let params = props.params;
  90. return (
  91. <Select
  92. value={params[0] || 'sha1'}
  93. placeholder="请选择"
  94. style={{ width: 150 }}
  95. size="small"
  96. onChange={e => this.handleParamsChange(e, clickIndex, paramsIndex, 0)}
  97. >
  98. {subname.map((item, index) => {
  99. return (
  100. <Option value={item} key={index}>
  101. {item}
  102. </Option>
  103. );
  104. })}
  105. </Select>
  106. );
  107. };
  108. // 处理参数输入
  109. handleParamsChange(value, clickIndex, paramsIndex, index) {
  110. let newList = deepEqual(this.state.list);
  111. newList[paramsIndex].params[index] = value;
  112. this.setState({
  113. list: newList
  114. });
  115. this.props.paramsInput(value, clickIndex, index);
  116. }
  117. // 组件选择
  118. handleComponent(item, clickIndex, index, params) {
  119. let query = {
  120. clickIndex: clickIndex,
  121. paramsIndex: index,
  122. params
  123. };
  124. switch (item.component) {
  125. case 'select':
  126. return this.selectComponent(query);
  127. case 'input':
  128. return this.inputComponent(query);
  129. case 'doubleInput':
  130. return this.doubleInputComponent(query);
  131. default:
  132. break;
  133. }
  134. }
  135. render() {
  136. const { list, moreFlag } = this.state;
  137. const { click, clickValue, clickIndex, params } = this.props;
  138. let showList = moreFlag ? list.slice(0, 4) : list;
  139. return (
  140. <div className="modal-postman-form-method">
  141. <h3 className="methods-title title">方法</h3>
  142. {showList.map((item, index) => {
  143. return (
  144. <Row
  145. key={index}
  146. type="flex"
  147. align="middle"
  148. className={'row methods-row ' + (item.name === clickValue ? 'checked' : '')}
  149. onClick={() => click(item.name, showList[index].params)}
  150. >
  151. <Tooltip title={item.desc}>
  152. <span>{item.name}</span>
  153. </Tooltip>
  154. <span className="input-component">
  155. {item.type &&
  156. this.handleComponent(
  157. item,
  158. clickIndex,
  159. index,
  160. item.name === clickValue ? params : []
  161. )}
  162. </span>
  163. </Row>
  164. );
  165. })}
  166. {moreFlag && (
  167. <div className="show-more" onClick={this.showMore}>
  168. <Icon type="down" />
  169. <span style={{ paddingLeft: '4px' }}>更多</span>
  170. </div>
  171. )}
  172. </div>
  173. );
  174. }
  175. }
  176. export default MethodsList;