import * as React from 'react'; import classNames from 'classnames'; import Input, { InputProps } from './Input'; import Icon from '../icon'; import Button from '../button'; export interface SearchProps extends InputProps { inputPrefixCls?: string; onSearch?: ( value: string, event?: React.MouseEvent | React.KeyboardEvent, ) => any; enterButton?: boolean | React.ReactNode; } export default class Search extends React.Component { static defaultProps = { inputPrefixCls: 'ant-input', prefixCls: 'ant-input-search', enterButton: false, }; private input: Input; onSearch = (e: React.MouseEvent | React.KeyboardEvent) => { const { onSearch } = this.props; if (onSearch) { onSearch(this.input.input.value, e); } this.input.focus(); }; focus() { this.input.focus(); } blur() { this.input.blur(); } saveInput = (node: Input) => { this.input = node; }; getButtonOrIcon() { const { enterButton, prefixCls, size, disabled } = this.props; const enterButtonAsElement = enterButton as React.ReactElement; let node; if (!enterButton) { node = ; } else if (enterButtonAsElement.type === Button || enterButtonAsElement.type === 'button') { node = React.cloneElement( enterButtonAsElement, enterButtonAsElement.type === Button ? { className: `${prefixCls}-button`, size, } : {}, ); } else { node = ( ); } return React.cloneElement(node, { onClick: this.onSearch, }); } render() { const { className, prefixCls, inputPrefixCls, size, suffix, enterButton, ...others } = this.props; delete (others as any).onSearch; const buttonOrIcon = this.getButtonOrIcon(); let searchSuffix = suffix ? [suffix, buttonOrIcon] : buttonOrIcon; if (Array.isArray(searchSuffix)) { searchSuffix = (searchSuffix as React.ReactElement[]).map((item, index) => { if (!React.isValidElement(item) || item.key) { return item; } return React.cloneElement(item, { key: index }); }); } const inputClassName = classNames(prefixCls, className, { [`${prefixCls}-enter-button`]: !!enterButton, [`${prefixCls}-${size}`]: !!size, }); return ( ); } }