ant-design/components/input/Search.tsx
vagusX 149729e524
use ant design icons 4.0 (#18217)
* feat: use @ant-design/icons@4.0

* feat: use createFromIconfontCN to make site works

* feat: update doc for Icon

* feat: use icon in component Alert

* feat: use icon in component Avatar

* feat: use icon in component Breadcrumb

* feat: use icon in component Button

* feat: use icon in component Cascader

* feat: use icon in component Collapse

* feat: use icon in component Datepicker

* feat: use icon in component Dropdown

* feat: use icon in component Form

* feat: use icon in component Input

* feat: use icon in component InputNumber

* feat: use icon in component Layout

* feat: use icon in component Mention

* feat: use icon in component Message

* feat: use icon in component Modal

* feat: use icon in component Notification

* feat: use icon in component PageHeader

* feat: use icon in component Pagination

* feat: use icon in component Popconfirm

* feat: use icon in component Progress

* feat: use icon in component Rate

* feat: use icon in component Result

* feat: use icon in component Select

* feat: use icon in component Step

* feat: use icon in component Switch

* feat: use icon in component Table

* feat: use icon in component Tab

* feat: use icon in component Tag

* feat: handle rest component which using Icon

* fix: remove unused vars

* feat: use latest alpha ant design icons

* fix: failed test in uploadlist.test.js

* test: update snapshot for icons

* doc: add Icon for site

* doc: use @ant-design/icons in site

* chore: use latest icons

* fix: tslint issue

* fix: test cases

* fix: types for react

* fix: lint rules for import orders

* fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render

* fix: eslint error in demo/**.md

* inject antd icons

* update snapshot

* fix site

* doc: update docs

* fix: code snippets icon in site

* feat: use latest @ant-design/icons

* fix: icon props in message
2019-08-13 14:07:17 +08:00

150 lines
3.7 KiB
TypeScript

import * as React from 'react';
import classNames from 'classnames';
import { Search as IconSearch } from '@ant-design/icons';
import Input, { InputProps } from './Input';
import Button from '../button';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
export interface SearchProps extends InputProps {
inputPrefixCls?: string;
onSearch?: (
value: string,
event?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLInputElement>,
) => void;
enterButton?: boolean | React.ReactNode;
}
export default class Search extends React.Component<SearchProps, any> {
static defaultProps = {
enterButton: false,
};
private input: Input;
saveInput = (node: Input) => {
this.input = node;
};
onSearch = (e: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLInputElement>) => {
const { onSearch } = this.props;
if (onSearch) {
onSearch(this.input.input.value, e);
}
this.input.focus();
};
focus() {
this.input.focus();
}
blur() {
this.input.blur();
}
renderSuffix = (prefixCls: string) => {
const { suffix, enterButton } = this.props;
if (enterButton) return suffix;
const node = (
<IconSearch className={`${prefixCls}-icon`} key="searchIcon" onClick={this.onSearch} />
);
if (suffix) {
let cloneSuffix = suffix;
if (React.isValidElement(cloneSuffix) && !cloneSuffix.key) {
cloneSuffix = React.cloneElement(cloneSuffix, {
key: 'originSuffix',
});
}
return [cloneSuffix, node];
}
return node;
};
renderAddonAfter = (prefixCls: string) => {
const { enterButton, size, disabled, addonAfter } = this.props;
if (!enterButton) return addonAfter;
const btnClassName = `${prefixCls}-button`;
let button: React.ReactNode;
const enterButtonAsElement = enterButton as React.ReactElement<any>;
if (enterButtonAsElement.type === Button || enterButtonAsElement.type === 'button') {
button = React.cloneElement(enterButtonAsElement, {
onClick: this.onSearch,
key: 'enterButton',
...(enterButtonAsElement.type === Button
? {
className: btnClassName,
size,
}
: {}),
});
} else {
button = (
<Button
className={btnClassName}
type="primary"
size={size}
disabled={disabled}
key="enterButton"
onClick={this.onSearch}
>
{enterButton === true ? <IconSearch /> : enterButton}
</Button>
);
}
if (addonAfter) {
return [button, addonAfter];
}
return button;
};
renderSearch = ({ getPrefixCls }: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
inputPrefixCls: customizeInputPrefixCls,
size,
enterButton,
className,
...restProps
} = this.props;
delete (restProps as any).onSearch;
const prefixCls = getPrefixCls('input-search', customizePrefixCls);
const inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
let inputClassName;
if (enterButton) {
inputClassName = classNames(prefixCls, className, {
[`${prefixCls}-enter-button`]: !!enterButton,
[`${prefixCls}-${size}`]: !!size,
});
} else {
inputClassName = classNames(prefixCls, className);
}
return (
<Input
onPressEnter={this.onSearch}
{...restProps}
size={size}
prefixCls={inputPrefixCls}
addonAfter={this.renderAddonAfter(prefixCls)}
suffix={this.renderSuffix(prefixCls)}
ref={this.saveInput}
className={inputClassName}
/>
);
};
render() {
return <ConfigConsumer>{this.renderSearch}</ConfigConsumer>;
}
}