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-07-28 14:56:22 +08:00
|
|
|
import { setCurrGroup } from '../../../actions/group'
|
|
|
|
import { changeMenuItem } from '../../../actions/menu'
|
|
|
|
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}
|
|
|
|
size="large"
|
|
|
|
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-25 21:04:11 +08:00
|
|
|
size="large"
|
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>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|