index.js 3.4 KB

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