index.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React, { useEffect, useState } from 'react';
  2. import styles from './index.less';
  3. import { Select } from 'antd';
  4. import useMapList from '@/hooks/useMapList';
  5. import { SearchOutlined } from '@ant-design/icons';
  6. type mapResType = {
  7. text: string;
  8. value: string;
  9. };
  10. const { Option } = Select;
  11. type SearchInputProps = {
  12. onSearch: (s: string) => void;
  13. };
  14. const SearchInput: React.FC<SearchInputProps> = ({ onSearch }) => {
  15. const mapList = useMapList();
  16. const [resData, setResData] = useState<mapResType[]>([]);
  17. const [value, setValue] = useState<any>();
  18. const handleSearch = (value: string) => {
  19. //debugger;
  20. if (value) {
  21. let data: mapResType[] = [];
  22. mapList.map((item, index) => {
  23. if (item.localName.indexOf(value) > -1) {
  24. data.push({
  25. text: item.localName,
  26. value: item.localName,
  27. });
  28. }
  29. setResData(data);
  30. });
  31. } else {
  32. setResData([]);
  33. }
  34. };
  35. const handleChange = (sel: string) => {
  36. setValue(null); //清空输入的值
  37. setResData([]); //清空搜索匹配列表
  38. onSearch(sel);
  39. };
  40. return (
  41. <div className={styles.headerSearch}>
  42. <SearchOutlined style={{ fontSize: 16 }} />
  43. <Select
  44. showSearch
  45. value={value}
  46. placeholder="搜索空间"
  47. defaultActiveFirstOption={false}
  48. showArrow={false}
  49. filterOption={false}
  50. onSearch={handleSearch}
  51. onChange={handleChange}
  52. notFoundContent={null}
  53. bordered={false}
  54. style={{ width: 150, borderColor: '#c4c4c4' }}
  55. // suffixIcon={
  56. // <AudioOutlined
  57. // style={{
  58. // fontSize: 16,
  59. // color: '#1890ff',
  60. // }}
  61. // />
  62. // }
  63. >
  64. {resData.map(function (item, index) {
  65. return <Option key={item.value}>{item.text}</Option>;
  66. })}
  67. </Select>
  68. </div>
  69. );
  70. };
  71. export default SearchInput;