ant-design/components/input/Search.tsx
偏右 dab6f43c02 Feat search enter button (#7620)
* Add enterButton for Input.Search

close #7596

* update snapshot
2017-09-15 17:03:43 +08:00

61 lines
1.5 KiB
TypeScript

import 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 {
onSearch?: (value: string) => any;
enterButton?: boolean | React.ReactNode;
}
export default class Search extends React.Component<SearchProps, any> {
static defaultProps = {
prefixCls: 'ant-input-search',
enterButton: false,
};
input: Input;
onSearch = () => {
const { onSearch } = this.props;
if (onSearch) {
onSearch(this.input.input.value);
}
this.input.focus();
}
saveInput = (node) => {
this.input = node;
}
render() {
const { className, prefixCls, size, enterButton, ...others } = this.props;
delete (others as any).onSearch;
const searchSuffix = enterButton
? (
<Button
className={`${prefixCls}-button`}
type="primary"
size={size}
onClick={this.onSearch}
>
{enterButton === true ? <Icon type="search" /> : enterButton}
</Button>
) : <Icon className={`${prefixCls}-icon`} type="search" />;
const inputClassName = classNames(prefixCls, className, {
[`${prefixCls}-enter-button`]: !!enterButton,
});
return (
<Input
onPressEnter={this.onSearch}
{...others}
size={size}
className={inputClassName}
suffix={searchSuffix}
ref={this.saveInput}
/>
);
}
}