index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import React, { PureComponent } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { Input, Select, Button, Icon } from 'antd';
  4. import styles from './index.less';
  5. export default class DataSearch extends PureComponent {
  6. static propTypes = {
  7. size: PropTypes.string,
  8. select: PropTypes.bool,
  9. selectProps: PropTypes.object,
  10. onSearch: PropTypes.func,
  11. selectOptions: PropTypes.array,
  12. style: PropTypes.object,
  13. keyword: PropTypes.string,
  14. }
  15. constructor(props) {
  16. super(props);
  17. const { select, selectProps, keyword } = this.props;
  18. this.state = {
  19. selectValue: select && selectProps ? selectProps.defaultValue : '',
  20. inputValue: keyword ? keyword : '',
  21. }
  22. }
  23. handleSearch = () => {
  24. const query = { keyword: this.state.inputValue };
  25. if (this.props.select) {
  26. query.field = this.state.selectValue;
  27. }
  28. this.props.onSearch && this.props.onSearch(query);
  29. }
  30. handleSelectChange = (value) => {
  31. this.setState({
  32. ...this.state,
  33. selectValue: value,
  34. });
  35. }
  36. handleInputChange = (e) => {
  37. this.setState({
  38. ...this.state,
  39. inputValue: e.target.value,
  40. });
  41. }
  42. handleClearInput = () => {
  43. this.setState({
  44. inputValue: '',
  45. }, () => this.handleSearch());
  46. }
  47. render() {
  48. const { size, select, selectOptions, selectProps, style } = this.props;
  49. const { inputValue } = this.state;
  50. const suffix = inputValue ? <Icon type="close-circle" onClick={this.handleClearInput} /> : null;
  51. return (
  52. <Input.Group compact size={size} className={styles.search} style={style}>
  53. {select && <Select onChange={this.handleSelectChange} size={size} {...selectProps}>
  54. {selectOptions && selectOptions.map((item, key) => <Select.Option value={item.value} key={key}>{item.name || item.value}</Select.Option>)}
  55. </Select>}
  56. <Input placeholder="请输入" suffix={suffix} onChange={this.handleInputChange} onPressEnter={this.handleSearch} size={size} value={inputValue}/>
  57. <Button onClick={this.handleSearch} size={size} type="primary" icon="search">搜索</Button>
  58. </Input.Group>
  59. );
  60. }
  61. }