import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import { Input, Select, Button, Icon } from 'antd';
import styles from './index.less';
const Option = Select.Option;
export default class DataSearch extends PureComponent {
static propTypes = {
size: PropTypes.string,
select: PropTypes.bool,
selectProps: PropTypes.object,
onSearch: PropTypes.func,
selectOptions: PropTypes.array,
style: PropTypes.object,
keyword: PropTypes.string,
filterSelectProps: PropTypes.object,
}
constructor(props) {
super(props);
const { select, selectOptions, selectProps, keyword, field } = this.props;
this.state = {
selectValue: select && selectProps ? selectProps.defaultValue : '',
inputValue: keyword ? keyword : '',
mode: field && selectOptions.filter(item => item.value === field)[0].mode || 'input',
}
}
handleSearch = () => {
const query = { keyword: this.state.inputValue };
if (this.props.select) {
query.field = this.state.selectValue;
}
this.props.onSearch && this.props.onSearch(query);
}
handleSelectChange = (value) => {
// 进行模式匹配
const { selectOptions } = this.props;
const match = selectOptions.filter(item => item.value === value)[0];
this.setState({
...this.state,
selectValue: value,
inputValue: '',
mode: match.mode,
});
}
handleInputChange = (e) => {
this.setState({
...this.state,
inputValue: e.target.value,
});
}
handleInputSelectChange = (value) => {
this.setState({
...this.state,
inputValue: value,
})
}
handleInputSelectClear = (value) => {
if (!value) {
this.setState({
inputValue: ''
}, () => this.handleSearch());
}
}
handleClearInput = () => {
this.setState({
inputValue: '',
}, () => this.handleSearch());
}
renderKeyWordComponent = () => {
const { mode, inputValue } = this.state;
const { size, filterSelectProps = {} } = this.props;
const { data = [] } = filterSelectProps;
const suffix = inputValue ? : null;
const input = (
);
const select = (
);
switch (mode) {
case 'input':
return input;
break;
case 'select':
return select;
break;
default:
return input;
break;
}
}
render() {
const { size, select, selectOptions, selectProps, style } = this.props;
return (
{select && }
{this.renderKeyWordComponent()}
);
}
}