index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. const Option = Select.Option;
  6. export default class DataSearch extends PureComponent {
  7. static propTypes = {
  8. size: PropTypes.string,
  9. select: PropTypes.bool,
  10. selectProps: PropTypes.object,
  11. onSearch: PropTypes.func,
  12. selectOptions: PropTypes.array,
  13. style: PropTypes.object,
  14. keyword: PropTypes.string,
  15. filterSelectProps: PropTypes.object,
  16. }
  17. constructor(props) {
  18. super(props);
  19. const { select, selectOptions, selectProps, keyword, field } = this.props;
  20. this.state = {
  21. selectValue: select && selectProps ? selectProps.defaultValue : '',
  22. inputValue: keyword ? keyword : '',
  23. mode: field && selectOptions.filter(item => item.value === field)[0].mode || 'input',
  24. }
  25. }
  26. handleSearch = () => {
  27. const query = { keyword: this.state.inputValue };
  28. if (this.props.select) {
  29. query.field = this.state.selectValue;
  30. }
  31. this.props.onSearch && this.props.onSearch(query);
  32. }
  33. handleSelectChange = (value) => {
  34. // 进行模式匹配
  35. const { selectOptions } = this.props;
  36. const match = selectOptions.filter(item => item.value === value)[0];
  37. this.setState({
  38. ...this.state,
  39. selectValue: value,
  40. inputValue: '',
  41. mode: match.mode,
  42. });
  43. }
  44. handleInputChange = (e) => {
  45. this.setState({
  46. ...this.state,
  47. inputValue: e.target.value,
  48. });
  49. }
  50. handleInputSelectChange = (value) => {
  51. this.setState({
  52. ...this.state,
  53. inputValue: value,
  54. })
  55. }
  56. handleInputSelectClear = (value) => {
  57. if (!value) {
  58. this.setState({
  59. inputValue: ''
  60. }, () => this.handleSearch());
  61. }
  62. }
  63. handleClearInput = () => {
  64. this.setState({
  65. inputValue: '',
  66. }, () => this.handleSearch());
  67. }
  68. renderKeyWordComponent = () => {
  69. const { mode, inputValue } = this.state;
  70. const { size, filterSelectProps = {} } = this.props;
  71. const { data = [] } = filterSelectProps;
  72. const suffix = inputValue ? <Icon type="close-circle" onClick={this.handleClearInput} /> : null;
  73. const input = (
  74. <Input
  75. placeholder="请输入"
  76. suffix={suffix}
  77. onChange={this.handleInputChange}
  78. onPressEnter={this.handleSearch}
  79. size={size}
  80. value={inputValue}
  81. />
  82. );
  83. const select = (
  84. <Select
  85. allowClear
  86. showSearch
  87. size={size}
  88. value={inputValue}
  89. placeholder="请选择/请输入进行筛选"
  90. optionFilterProp="children"
  91. style={{ flexShrink: 1, flexGrow: 1 }}
  92. onChange={this.handleInputSelectClear}
  93. onSelect={this.handleInputSelectChange}
  94. filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}
  95. >
  96. {data.map(item => <Option key={item.value} value={item.value}>{item.name}</Option>)}
  97. </Select>
  98. );
  99. switch (mode) {
  100. case 'input':
  101. return input;
  102. break;
  103. case 'select':
  104. return select;
  105. break;
  106. default:
  107. return input;
  108. break;
  109. }
  110. }
  111. render() {
  112. const { size, select, selectOptions, selectProps, style } = this.props;
  113. return (
  114. <Input.Group compact size={size} className={styles.search} style={style}>
  115. {select && <Select onChange={this.handleSelectChange} size={size} {...selectProps}>
  116. {selectOptions && selectOptions.map((item, key) => <Option value={item.value} key={key}>{item.name || item.value}</Option>)}
  117. </Select>}
  118. {this.renderKeyWordComponent()}
  119. <Button onClick={this.handleSearch} size={size} type="primary" icon="search">搜索</Button>
  120. </Input.Group>
  121. );
  122. }
  123. }