mock平台

VariablesSelect.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Tree } from 'antd';
  4. import { connect } from 'react-redux';
  5. import { fetchVariableParamsList } from '../../reducer/modules/interfaceCol.js';
  6. const TreeNode = Tree.TreeNode;
  7. const CanSelectPathPrefix = 'CanSelectPath-';
  8. function deleteLastObject(str) {
  9. return str
  10. .split('.')
  11. .slice(0, -1)
  12. .join('.');
  13. }
  14. function deleteLastArr(str) {
  15. return str.replace(/\[.*?\]/g, '');
  16. }
  17. @connect(
  18. state => {
  19. return {
  20. currColId: state.interfaceCol.currColId
  21. };
  22. },
  23. {
  24. fetchVariableParamsList
  25. }
  26. )
  27. class VariablesSelect extends Component {
  28. static propTypes = {
  29. click: PropTypes.func,
  30. currColId: PropTypes.number,
  31. fetchVariableParamsList: PropTypes.func,
  32. clickValue: PropTypes.string,
  33. id: PropTypes.number
  34. };
  35. state = {
  36. records: [],
  37. expandedKeys: [],
  38. selectedKeys: []
  39. };
  40. handleRecordsData(id) {
  41. let newRecords = [];
  42. this.id = id;
  43. for (let i = 0; i < this.records.length; i++) {
  44. if (this.records[i]._id === id) {
  45. break;
  46. }
  47. newRecords.push(this.records[i]);
  48. }
  49. this.setState({
  50. records: newRecords
  51. });
  52. }
  53. async componentDidMount() {
  54. const { currColId, fetchVariableParamsList, clickValue } = this.props;
  55. let result = await fetchVariableParamsList(currColId);
  56. let records = result.payload.data.data;
  57. this.records = records.sort((a, b) => {
  58. return a.index - b.index;
  59. });
  60. this.handleRecordsData(this.props.id);
  61. if (clickValue) {
  62. let isArrayParams = clickValue.lastIndexOf(']') === clickValue.length - 1;
  63. let key = isArrayParams ? deleteLastArr(clickValue) : deleteLastObject(clickValue);
  64. this.setState({
  65. expandedKeys: [key],
  66. selectedKeys: [CanSelectPathPrefix + clickValue]
  67. });
  68. // this.props.click(clickValue);
  69. }
  70. }
  71. async componentWillReceiveProps(nextProps) {
  72. if (this.records && nextProps.id && this.id !== nextProps.id) {
  73. this.handleRecordsData(nextProps.id);
  74. }
  75. }
  76. handleSelect = key => {
  77. this.setState({
  78. selectedKeys: [key]
  79. });
  80. if (key && key.indexOf(CanSelectPathPrefix) === 0) {
  81. key = key.substr(CanSelectPathPrefix.length);
  82. this.props.click(key);
  83. } else {
  84. this.setState({
  85. expandedKeys: [key]
  86. });
  87. }
  88. };
  89. onExpand = keys => {
  90. this.setState({ expandedKeys: keys });
  91. };
  92. render() {
  93. const pathSelctByTree = (data, elementKeyPrefix = '$', deepLevel = 0) => {
  94. let keys = Object.keys(data);
  95. let TreeComponents = keys.map((key, index) => {
  96. let item = data[key],
  97. casename;
  98. if (deepLevel === 0) {
  99. elementKeyPrefix = '$';
  100. elementKeyPrefix = elementKeyPrefix + '.' + item._id;
  101. casename = item.casename;
  102. item = {
  103. params: item.params,
  104. body: item.body
  105. };
  106. } else if (Array.isArray(data)) {
  107. elementKeyPrefix =
  108. index === 0
  109. ? elementKeyPrefix + '[' + key + ']'
  110. : deleteLastArr(elementKeyPrefix) + '[' + key + ']';
  111. } else {
  112. elementKeyPrefix =
  113. index === 0
  114. ? elementKeyPrefix + '.' + key
  115. : deleteLastObject(elementKeyPrefix) + '.' + key;
  116. }
  117. if (item && typeof item === 'object') {
  118. const isDisable = Array.isArray(item) && item.length === 0;
  119. return (
  120. <TreeNode key={elementKeyPrefix} disabled={isDisable} title={casename || key}>
  121. {pathSelctByTree(item, elementKeyPrefix, deepLevel + 1)}
  122. </TreeNode>
  123. );
  124. }
  125. return <TreeNode key={CanSelectPathPrefix + elementKeyPrefix} title={key} />;
  126. });
  127. return TreeComponents;
  128. };
  129. return (
  130. <div className="modal-postman-form-variable">
  131. <Tree
  132. expandedKeys={this.state.expandedKeys}
  133. selectedKeys={this.state.selectedKeys}
  134. onSelect={([key]) => this.handleSelect(key)}
  135. onExpand={this.onExpand}
  136. >
  137. {pathSelctByTree(this.state.records)}
  138. </Tree>
  139. </div>
  140. );
  141. }
  142. }
  143. export default VariablesSelect;