2017-07-20 19:59:49 +08:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { connect } from 'react-redux'
|
|
|
|
import { Icon, Input, AutoComplete } from 'antd'
|
|
|
|
import './Search.scss'
|
|
|
|
import { withRouter } from 'react-router';
|
|
|
|
import axios from 'axios';
|
2017-08-08 10:07:55 +08:00
|
|
|
import { setCurrGroup } from '../../../reducer/modules/group'
|
|
|
|
import { changeMenuItem } from '../../../reducer/modules/menu'
|
2017-07-28 14:56:22 +08:00
|
|
|
const Option = AutoComplete.Option;
|
2017-07-20 19:59:49 +08:00
|
|
|
|
|
|
|
@connect(
|
|
|
|
state => ({
|
|
|
|
groupList: state.group.groupList,
|
|
|
|
projectList: state.project.projectList
|
2017-07-28 14:56:22 +08:00
|
|
|
}),{
|
|
|
|
setCurrGroup,
|
|
|
|
changeMenuItem
|
|
|
|
}
|
2017-07-20 19:59:49 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
@withRouter
|
|
|
|
export default class Srch extends Component{
|
|
|
|
constructor(props){
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
dataSource:[]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
groupList : PropTypes.array,
|
|
|
|
projectList: PropTypes.array,
|
|
|
|
router: PropTypes.object,
|
|
|
|
history: PropTypes.object,
|
2017-07-28 14:56:22 +08:00
|
|
|
location: PropTypes.object,
|
|
|
|
setCurrGroup: PropTypes.func,
|
|
|
|
changeMenuItem : PropTypes.func
|
2017-07-20 19:59:49 +08:00
|
|
|
}
|
|
|
|
|
2017-07-28 14:56:22 +08:00
|
|
|
onSelect = (value,option) => {
|
|
|
|
if( option.props.tpye == "分组" ){
|
|
|
|
this.props.changeMenuItem('/group');
|
|
|
|
this.props.history.push('/group/'+value);
|
|
|
|
this.props.setCurrGroup({"group_name":value,"_id":option.props['id']});
|
2017-07-20 19:59:49 +08:00
|
|
|
} else {
|
2017-07-28 14:56:22 +08:00
|
|
|
this.props.history.push('/project/'+option.props['id']);
|
2017-07-20 19:59:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSearch = (value) => {
|
|
|
|
axios.get('/project/search?q='+value)
|
|
|
|
.then((res) => {
|
|
|
|
if(res.data && res.data.errcode === 0){
|
|
|
|
const dataSource = [];
|
2017-07-28 14:56:22 +08:00
|
|
|
for(let title in res.data.data) {
|
2017-07-20 19:59:49 +08:00
|
|
|
res.data.data[title].map(item => {
|
2017-07-28 14:56:22 +08:00
|
|
|
dataSource.push(
|
|
|
|
title == "group" ?
|
|
|
|
( <Option
|
|
|
|
key={`${item._id}`}
|
|
|
|
tpye="分组"
|
|
|
|
value={`${item.groupName}`}
|
|
|
|
id={`${item._id}`}
|
|
|
|
>
|
|
|
|
{`分组: ${item.groupName}`}
|
|
|
|
</Option>) :
|
|
|
|
(<Option
|
|
|
|
key={`${item._id}`}
|
|
|
|
tpye="项目"
|
|
|
|
value={`${item._id}`}
|
|
|
|
id={`${item._id}`}
|
|
|
|
>
|
|
|
|
{`项目: ${item.name}`}
|
|
|
|
</Option>)
|
|
|
|
)
|
2017-07-20 19:59:49 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
dataSource: dataSource
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
console.log("查询项目或分组失败");
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log(err);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
getDataSource(groupList){
|
|
|
|
const groupArr =[];
|
|
|
|
groupList.forEach(item =>{
|
|
|
|
groupArr.push("group: "+ item["group_name"]);
|
|
|
|
})
|
|
|
|
return groupArr;
|
|
|
|
}
|
|
|
|
|
|
|
|
render(){
|
|
|
|
const { dataSource } = this.state;
|
2017-07-28 14:56:22 +08:00
|
|
|
|
2017-07-20 19:59:49 +08:00
|
|
|
return(
|
|
|
|
<div className="search-wrapper">
|
|
|
|
<AutoComplete
|
|
|
|
className="search-dropdown"
|
|
|
|
dataSource={dataSource}
|
|
|
|
style={{ width: '100%' }}
|
|
|
|
defaultActiveFirstOption= {false}
|
|
|
|
onSelect={this.onSelect}
|
|
|
|
onSearch={this.handleSearch}
|
|
|
|
filterOption={(inputValue, option) => option.props.children.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1}
|
|
|
|
>
|
|
|
|
<Input
|
|
|
|
prefix={<Icon type="search" className="srch-icon" />}
|
2017-07-28 14:56:22 +08:00
|
|
|
style={{}}
|
2017-07-25 21:04:11 +08:00
|
|
|
placeholder="搜索分组/项目"
|
2017-07-20 19:59:49 +08:00
|
|
|
className="search-input"
|
|
|
|
/>
|
|
|
|
</AutoComplete>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2017-08-07 19:34:34 +08:00
|
|
|
}
|