import { SearchOutlined } from '@ant-design/icons'; import { AutoComplete, Input } from 'antd'; import useMergedState from 'rc-util/es/hooks/useMergedState'; import type { AutoCompleteProps } from 'antd/es/auto-complete'; import React, { useRef } from 'react'; import classNames from 'classnames'; import styles from './index.less'; export type HeaderSearchProps = { onSearch?: (value?: string) => void; onChange?: (value?: string) => void; onVisibleChange?: (b: boolean) => void; className?: string; placeholder?: string; options: AutoCompleteProps['options']; defaultVisible?: boolean; visible?: boolean; defaultValue?: string; value?: string; }; const HeaderSearch: React.FC = (props) => { const { className, defaultValue, onVisibleChange, placeholder, visible, defaultVisible, ...restProps } = props; const inputRef = useRef(null); const [value, setValue] = useMergedState(defaultValue, { value: props.value, onChange: props.onChange, }); const [searchMode, setSearchMode] = useMergedState(defaultVisible ?? false, { value: props.visible, onChange: onVisibleChange, }); const inputClass = classNames(styles.input, { [styles.show]: searchMode, }); return (
{ setSearchMode(true); if (searchMode && inputRef.current) { inputRef.current.focus(); } }} onTransitionEnd={({ propertyName }) => { if (propertyName === 'width' && !searchMode) { if (onVisibleChange) { onVisibleChange(searchMode); } } }} > { if (e.key === 'Enter') { if (restProps.onSearch) { restProps.onSearch(value); } } }} onBlur={() => { setSearchMode(false); }} />
); }; export default HeaderSearch;