SearchModal.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import React, {
  2. useState, useCallback, useEffect,
  3. } from 'react';
  4. import Select, { components, SelectComponentsConfig } from 'react-select';
  5. import AsyncSelect from 'react-select/async';
  6. import { Modal, ModalBody } from 'reactstrap';
  7. import { useDebounce } from 'usehooks-ts';
  8. import { useSWRxSearch } from '~/stores/search';
  9. import { useSearchModal } from '../stores/search';
  10. import { SearchForm } from './SearchForm';
  11. import { SearchHelp } from './SearchHelp';
  12. import { SearchMethodMenuItem } from './SearchMethodMenuItem';
  13. import { SearchResultMenuItem } from './SearchResultMenuItem';
  14. const SearchModal = (): JSX.Element => {
  15. const [searchKeyword, setSearchKeyword] = useState('');
  16. const { data: searchModalData, close: closeSearchModal } = useSearchModal();
  17. const changeSearchTextHandler = useCallback((searchText: string) => {
  18. setSearchKeyword(searchText);
  19. }, []);
  20. const clickClearButtonHandler = useCallback(() => {
  21. setSearchKeyword('');
  22. }, []);
  23. // search result
  24. // -----------------------------------------------------------------------------------------------------------------------------------------------
  25. const debouncedKeyword = useDebounce(searchKeyword, 500);
  26. const isEmptyKeyword = debouncedKeyword.trim() === '';
  27. const { data: searchResult, isLoading } = useSWRxSearch(isEmptyKeyword ? null : searchKeyword, null, { limit: 10 });
  28. // const options = searchResult?.data.map(pageWithMeta => ({ label: pageWithMeta.data.path }));
  29. // -----------------------------------------------------------------------------------------------------------------------------------------------
  30. // search method
  31. // -----------------------------------------------------------------------------------------------------------------------------------------------
  32. // -----------------------------------------------------------------------------------------------------------------------------------------------
  33. // react-select settings
  34. // -----------------------------------------------------------------------------------------------------------------------------------------------
  35. // const components = {
  36. // IndicatorSeparator: () => null,
  37. // IndicatorsContainer: () => null,
  38. // DropdownIndicator: () => null,
  39. // MenuList: (props) => {
  40. // return (
  41. // <components.MenuList {...props}>
  42. // <SearchMethodMenuItem searchKeyword={searchKeyword} />
  43. // <SearchResultMenuItem searchKeyword={searchKeyword} />
  44. // </components.MenuList>
  45. // );
  46. // },
  47. // };
  48. // -----------------------------------------------------------------------------------------------------------------------------------------------
  49. useEffect(() => {
  50. if (!searchModalData?.isOpened) {
  51. setSearchKeyword('');
  52. }
  53. }, [searchModalData?.isOpened]);
  54. return (
  55. <Modal size="lg" isOpen={searchModalData?.isOpened ?? false} toggle={closeSearchModal}>
  56. <ModalBody>
  57. <Select
  58. autoFocus
  59. menuIsOpen
  60. components={{
  61. MenuList: (props) => {
  62. return (
  63. <components.MenuList {...props}>
  64. <SearchMethodMenuItem searchKeyword={searchKeyword} />
  65. <SearchResultMenuItem searchKeyword={searchKeyword} />
  66. </components.MenuList>
  67. );
  68. },
  69. }}
  70. placeholder="Search..."
  71. onInputChange={(value) => { setSearchKeyword(value) }}
  72. >
  73. </Select>
  74. </ModalBody>
  75. </Modal>
  76. );
  77. };
  78. export default SearchModal;