import classNames from "classnames"; import React, { useRef, useState } from "react"; import { useKeyPress, useOutsideClick } from "../../hooks"; import { KEY_CODE } from "../../types"; import { bem } from "../../utilities/bem"; import { BoxProps, Box } from "../Box"; import { Button, BUTTON_VARIANT } from "../Button"; import { Flex } from "../Flex"; import { ICON_TYPE, Icon } from "../Icon"; const cn = "SearchFilter"; export enum SEARCH_FILTER_DIRECTION { LEFT = "left", RIGHT = "right", } export interface SearchFilterProps extends BoxProps { /** * Current value of the search input */ inputValue: string; /** * Handler for the onChange event of the search input */ onInputChange: (event: React.ChangeEvent) => void; /** * Handler for the onSubmit event of the search form. */ onSearchSubmit: (event: React.FormEvent) => void; /** * Called when the input is cleared (only available if autoClose is false) and should be used to clear the input since this is a controlled component */ onClear?: (event: React.MouseEvent) => void; /** * If false, the search input will remain in the open state and not be closed when it loses focus */ autoClose?: boolean; /** * Callback to execute when the input is collapsed. If using autoClose mode, this should be used to clear the inputValue. */ onClose?: () => void; /** * The direction (relative to the button) to which the input should expand when opened. */ direction?: SEARCH_FILTER_DIRECTION; /** * Placeholder text for the input */ placeholder?: string; } export const SearchFilter = ({ className, direction = SEARCH_FILTER_DIRECTION.LEFT, placeholder = "Search", inputValue, onInputChange, onSearchSubmit, onClear, autoClose = true, onClose, ...rest }: SearchFilterProps) => { const Component = autoClose ? "button" : "div"; const [isOpen, setIsOpen] = useState(!autoClose); const searchElement = useRef(null); const inputElement = useRef(null); const focusInput = () => { if (inputElement && inputElement.current) { inputElement.current.focus(); } }; const blurInput = () => { if (inputElement && inputElement.current) { inputElement.current.blur(); } }; const openSearch = () => { setIsOpen(true); focusInput(); }; const closeSearch = () => { setIsOpen(false); blurInput(); if (onClose) { onClose(); } }; const toggleSearchInput = () => { if (isOpen) { closeSearch(); } else { openSearch(); } }; const handleToggleClick = () => { if (autoClose) { toggleSearchInput(); } }; useOutsideClick(searchElement, () => { if (autoClose) { closeSearch(); } }); useKeyPress(KEY_CODE.ESC, () => { if (autoClose) { closeSearch(); } }); const ToggleButton = () => { return ( ); }; return ( {direction === SEARCH_FILTER_DIRECTION.RIGHT && }
{autoClose && isOpen && (