index.js 2.4 KB
Newer Older
dengxiaofeng's avatar
dengxiaofeng committed
1
import React, { useRef } from 'react';
dengxiaofeng's avatar
dengxiaofeng committed
2

张烨's avatar
张烨 committed
3
import { AutoComplete, Input } from 'antd';
dengxiaofeng's avatar
dengxiaofeng committed
4
import classNames from 'classnames';
dengxiaofeng's avatar
dengxiaofeng committed
5 6 7 8 9 10
import PropType from 'prop-types';
import useMergeValue from 'use-merge-value';

import { SearchOutlined } from '@ant-design/icons';

import styles from './index.less';
dengxiaofeng's avatar
dengxiaofeng committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

const HeaderSearch = props => {
  const {
    className,
    defaultValue,
    onVisibleChange,
    placeholder,
    open,
    defaultOpen,
    ...restProps
  } = props;

  const inputRef = useRef(null);

  const [value, setValue] = useMergeValue(defaultValue, {
    value: props.value,
    onChange: props.onChange,
  });

  const [searchMode, setSearchMode] = useMergeValue(defaultOpen || false, {
    value: props.open,
    onChange: onVisibleChange,
  });

张烨's avatar
张烨 committed
35 36 37
  const inputClass = classNames(styles.input, {
    [styles.show]: searchMode,
  });
dengxiaofeng's avatar
dengxiaofeng committed
38 39 40

  return (
    <div
张烨's avatar
张烨 committed
41
      className={classNames(className, styles.headerSearch)}
dengxiaofeng's avatar
dengxiaofeng committed
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
      onClick={() => {
        setSearchMode(true);
        if (searchMode && inputRef.current) {
          inputRef.current.focus();
        }
      }}
      onTransitionEnd={({ propertyName }) => {
        if (propertyName === 'width' && !searchMode) {
          if (onVisibleChange) {
            onVisibleChange(searchMode);
          }
        }
      }}
    >
      <SearchOutlined
        key="Icon"
        style={{
          cursor: 'pointer',
        }}
      />
      <AutoComplete
        key="AutoComplete"
        className={inputClass}
        value={value}
        style={{
          height: 28,
          marginTop: -6,
        }}
        options={restProps.options}
        onChange={setValue}
      >
        <Input
          ref={inputRef}
          defaultValue={defaultValue}
          aria-label={placeholder}
          placeholder={placeholder}
          onKeyDown={e => {
            if (e.key === 'Enter') {
              if (restProps.onSearch) {
                restProps.onSearch(value);
              }
            }
          }}
          onBlur={() => {
            setSearchMode(false);
          }}
        />
      </AutoComplete>
    </div>
  );
};

HeaderSearch.propTypes = {
  onSearch: PropType.func,
  onChange: PropType.func,
  onVisibleChange: PropType.func,
  className: PropType.string,
  placeholder: PropType.string,
  options: PropType.array,
  defaultOpen: PropType.bool,
  open: PropType.bool,
  defaultValue: PropType.string,
  value: PropType.string,
};
export default HeaderSearch;