-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
/
Search.tsx
146 lines (135 loc) · 4 KB
/
Search.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import * as React from 'react';
import classNames from 'classnames';
import Input, { InputProps } from './Input';
import Icon from '../icon';
import Button from '../button';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
function noop() {}
export interface SearchProps extends InputProps {
inputPrefixCls?: string;
onSearch?: (
value: string,
event?: React.MouseEvent<HTMLElement> | React.KeyboardEvent<HTMLInputElement>,
) => any;
enterButton?: boolean | React.ReactNode;
loading?: boolean;
}
export default class Search extends React.Component<SearchProps, any> {
static defaultProps = {
enterButton: false,
};
private input: Input;
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();
}
saveInput = (node: Input) => {
this.input = node;
};
getButtonOrIcon(prefixCls: string) {
const { enterButton, size, disabled, loading } = this.props;
const enterButtonAsElement = enterButton as React.ReactElement<any>;
const iconClassName = classNames(`${prefixCls}-icon`, {
[`${prefixCls}-loading`]: !!loading,
});
const buttonClassName = classNames(`${prefixCls}-button`, {
[`${prefixCls}-loading`]: !!loading,
});
let node;
if (!enterButton) {
node = (
<Icon className={iconClassName} type={loading ? 'loading' : 'search'} key="searchIcon" />
);
} else if (enterButtonAsElement.type === Button || enterButtonAsElement.type === 'button') {
node = React.cloneElement(
enterButtonAsElement,
enterButtonAsElement.type === Button
? {
className: buttonClassName,
size,
}
: {},
);
} else {
node = (
<Button
className={buttonClassName}
type="primary"
size={size}
disabled={disabled}
key="enterButton"
>
{enterButton === true ? (
<Icon type={loading ? 'loading' : 'search'} />
) : (
<span>
<span
className={classNames({
[`${prefixCls}-button-text-loading`]: !!loading,
})}
>
{enterButton}
</span>
{loading && <Icon type="loading" />}
</span>
)}
</Button>
);
}
return React.cloneElement(node, {
onClick: loading ? noop : this.onSearch,
});
}
renderSearch = ({ getPrefixCls }: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
inputPrefixCls: customizeInputPrefixCls,
className,
size,
suffix,
enterButton,
loading,
...others
} = this.props;
delete (others as any).onSearch;
const prefixCls = getPrefixCls('input-search', customizePrefixCls);
const inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);
const buttonOrIcon = this.getButtonOrIcon(prefixCls);
let searchSuffix = suffix ? [suffix, buttonOrIcon] : buttonOrIcon;
if (Array.isArray(searchSuffix)) {
searchSuffix = (searchSuffix as React.ReactElement<any>[]).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 (
<Input
onPressEnter={loading ? noop : this.onSearch}
{...others}
size={size}
className={inputClassName}
prefixCls={inputPrefixCls}
suffix={searchSuffix}
ref={this.saveInput}
/>
);
};
render() {
return <ConfigConsumer>{this.renderSearch}</ConfigConsumer>;
}
}