mock平台

Search.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import React, { PureComponent as Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { connect } from 'react-redux';
  4. import { Icon, Input, AutoComplete } from 'antd';
  5. import './Search.scss';
  6. import { withRouter } from 'react-router';
  7. import axios from 'axios';
  8. import { setCurrGroup, fetchGroupMsg } from '../../../reducer/modules/group';
  9. import { changeMenuItem } from '../../../reducer/modules/menu';
  10. import { fetchInterfaceListMenu } from '../../../reducer/modules/interface';
  11. const Option = AutoComplete.Option;
  12. @connect(
  13. state => ({
  14. groupList: state.group.groupList,
  15. projectList: state.project.projectList
  16. }),
  17. {
  18. setCurrGroup,
  19. changeMenuItem,
  20. fetchGroupMsg,
  21. fetchInterfaceListMenu
  22. }
  23. )
  24. @withRouter
  25. export default class Srch extends Component {
  26. constructor(props) {
  27. super(props);
  28. this.state = {
  29. dataSource: []
  30. };
  31. }
  32. static propTypes = {
  33. groupList: PropTypes.array,
  34. projectList: PropTypes.array,
  35. router: PropTypes.object,
  36. history: PropTypes.object,
  37. location: PropTypes.object,
  38. setCurrGroup: PropTypes.func,
  39. changeMenuItem: PropTypes.func,
  40. fetchInterfaceListMenu: PropTypes.func,
  41. fetchGroupMsg: PropTypes.func
  42. };
  43. onSelect = async (value, option) => {
  44. if (option.props.type === '分组') {
  45. this.props.changeMenuItem('/group');
  46. this.props.history.push('/group/' + option.props['id']);
  47. this.props.setCurrGroup({ group_name: value, _id: option.props['id'] - 0 });
  48. } else if (option.props.type === '项目') {
  49. await this.props.fetchGroupMsg(option.props['groupId']);
  50. this.props.history.push('/project/' + option.props['id']);
  51. } else if (option.props.type === '接口') {
  52. await this.props.fetchInterfaceListMenu(option.props['projectId']);
  53. this.props.history.push(
  54. '/project/' + option.props['projectId'] + '/interface/api/' + option.props['id']
  55. );
  56. }
  57. };
  58. handleSearch = value => {
  59. axios
  60. .get('/api/project/search?q=' + value)
  61. .then(res => {
  62. if (res.data && res.data.errcode === 0) {
  63. const dataSource = [];
  64. for (let title in res.data.data) {
  65. res.data.data[title].map(item => {
  66. switch (title) {
  67. case 'group':
  68. dataSource.push(
  69. <Option
  70. key={`分组${item._id}`}
  71. type="分组"
  72. value={`${item.groupName}`}
  73. id={`${item._id}`}
  74. >
  75. {`分组: ${item.groupName}`}
  76. </Option>
  77. );
  78. break;
  79. case 'project':
  80. dataSource.push(
  81. <Option
  82. key={`项目${item._id}`}
  83. type="项目"
  84. id={`${item._id}`}
  85. groupId={`${item.groupId}`}
  86. >
  87. {`项目: ${item.name}`}
  88. </Option>
  89. );
  90. break;
  91. case 'interface':
  92. dataSource.push(
  93. <Option
  94. key={`接口${item._id}`}
  95. type="接口"
  96. id={`${item._id}`}
  97. projectId={`${item.projectId}`}
  98. >
  99. {`接口: ${item.title}`}
  100. </Option>
  101. );
  102. break;
  103. default:
  104. break;
  105. }
  106. });
  107. }
  108. this.setState({
  109. dataSource: dataSource
  110. });
  111. } else {
  112. console.log('查询项目或分组失败');
  113. }
  114. })
  115. .catch(err => {
  116. console.log(err);
  117. });
  118. };
  119. // getDataSource(groupList){
  120. // const groupArr =[];
  121. // groupList.forEach(item =>{
  122. // groupArr.push("group: "+ item["group_name"]);
  123. // })
  124. // return groupArr;
  125. // }
  126. render() {
  127. const { dataSource } = this.state;
  128. return (
  129. <div className="search-wrapper">
  130. <AutoComplete
  131. className="search-dropdown"
  132. dataSource={dataSource}
  133. style={{ width: '100%' }}
  134. defaultActiveFirstOption={false}
  135. onSelect={this.onSelect}
  136. onSearch={this.handleSearch}
  137. // filterOption={(inputValue, option) =>
  138. // option.props.children.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1
  139. // }
  140. >
  141. <Input
  142. prefix={<Icon type="search" className="srch-icon" />}
  143. placeholder="搜索分组/项目/接口"
  144. className="search-input"
  145. />
  146. </AutoComplete>
  147. </div>
  148. );
  149. }
  150. }