File size: 3,888 Bytes
9a085da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// src/components/SearchButton/SearchButton.tsx
import React, { useState, useRef, useEffect } from "react";
import { LookUp } from "../../Icons/LookUp";
import styles from "./SearchButton.module.css";

interface SearchButtonProps {
  onSearch?: (query: string) => void;
  placeholder?: string;
  initialSize?: string; // Пропс для начального размера круглой кнопки
  onToggleExpand?: (isExpanded: boolean) => void; // Новый пропс для связи с родителем
}

function SearchButton(props: Readonly<SearchButtonProps>) {
  const { onSearch, placeholder = 'Search...', initialSize = '60px', onToggleExpand } = props;
  const [isExpanded, setIsExpanded] = useState(false);
  const [searchQuery, setSearchQuery] = useState('');
  const inputRef = useRef<HTMLInputElement>(null);
  const containerRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    if (isExpanded && inputRef.current) {
      inputRef.current.focus();
    }
    // Уведомляем родительский компонент об изменении состояния
    if (onToggleExpand) {
      onToggleExpand(isExpanded);
    }
  }, [isExpanded, onToggleExpand]); // onToggleExpand добавлен в зависимости

  const handleToggleExpand = () => {
    setIsExpanded(prevExpanded => !prevExpanded); // Используем функциональное обновление
    setSearchQuery('');
  };

  const handleSearchSubmit = (event: React.FormEvent) => {
    event.preventDefault();
    if (searchQuery.trim() && onSearch) {
      onSearch(searchQuery.trim());
      // Опционально: можно схлопнуть поле ввода после поиска
      // setIsExpanded(false);
    }
  };

  const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
    if (event.key === 'Escape') {
      setIsExpanded(false);
      setSearchQuery('');
    }
  };

  useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      if (
        isExpanded &&
        containerRef.current &&
        !containerRef.current.contains(event.target as Node)
      ) {
        setIsExpanded(false);
        setSearchQuery('');
      }
    };

    document.addEventListener('mousedown', handleClickOutside);
    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
    };
  }, [isExpanded]);

  const containerInlineStyles: React.CSSProperties = {
    width: isExpanded ? undefined : initialSize, // При развороте ширина определяется CSS, иначе - initialSize
    height: initialSize, // Высота всегда фиксирована initialSize (для круглой кнопки и высоты инпута)
  };

  return (
    <div
      className={`${styles.container} ${isExpanded ? styles.expanded : ''}`}
      style={containerInlineStyles}
      ref={containerRef}
      tabIndex={isExpanded ? -1 : 0} /* Делаем контейнер фокусируемым для focus-visible, но отключаем при expanded */
    >
      {isExpanded ? (
        <form onSubmit={handleSearchSubmit} className={styles.searchForm}>
          <input
            type="text"
            ref={inputRef}
            className={styles.searchInput}
            placeholder={placeholder}
            value={searchQuery}
            onChange={(e) => setSearchQuery(e.target.value)}
            onKeyDown={handleKeyDown}
          />
          <button type="submit" className={styles.submitButton}>
            <LookUp className={styles.iconInInput} />
          </button>
        </form>
      ) : (
        <button
          className={styles.searchButton}
          onClick={handleToggleExpand}
        >
          <LookUp className={styles.icon} />
        </button>
      )}
    </div>
  );
}

export default SearchButton;