2016-03-31 09:40:55 +08:00
|
|
|
---
|
2016-05-25 18:21:27 +08:00
|
|
|
order: 4
|
2016-03-31 09:40:55 +08:00
|
|
|
title: 搜索框
|
|
|
|
---
|
2015-12-02 19:33:26 +08:00
|
|
|
|
2016-05-25 18:21:27 +08:00
|
|
|
带有搜索按钮的输入框。
|
2015-12-02 19:33:26 +08:00
|
|
|
|
|
|
|
````jsx
|
2016-05-12 11:38:49 +08:00
|
|
|
import { Input, Button } from 'antd';
|
2015-12-02 19:33:26 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
const InputGroup = Input.Group;
|
|
|
|
|
|
|
|
const SearchInput = React.createClass({
|
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
value: '',
|
2016-03-25 19:58:57 +08:00
|
|
|
focus: false,
|
2015-12-02 19:33:26 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
handleInputChange(e) {
|
|
|
|
this.setState({
|
|
|
|
value: e.target.value,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
handleFocusBlur(e) {
|
|
|
|
this.setState({
|
|
|
|
focus: e.target === document.activeElement,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
handleSearch() {
|
|
|
|
if (this.props.onSearch) {
|
2016-04-06 18:12:41 +08:00
|
|
|
this.props.onSearch(this.state.value);
|
2015-12-02 19:33:26 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
render() {
|
2016-05-12 11:38:49 +08:00
|
|
|
const { style, size, ...restProps } = this.props;
|
2015-12-02 19:33:26 +08:00
|
|
|
const btnCls = classNames({
|
|
|
|
'ant-search-btn': true,
|
|
|
|
'ant-search-btn-noempty': !!this.state.value.trim(),
|
|
|
|
});
|
|
|
|
const searchCls = classNames({
|
|
|
|
'ant-search-input': true,
|
|
|
|
'ant-search-input-focus': this.state.focus,
|
|
|
|
});
|
2016-01-07 16:29:12 +08:00
|
|
|
return (
|
2016-05-12 11:38:49 +08:00
|
|
|
<div className="ant-search-input-wrapper" style={style}>
|
|
|
|
<InputGroup className={searchCls}>
|
|
|
|
<Input {...restProps} value={this.state.value} onChange={this.handleInputChange}
|
2016-06-06 13:54:10 +08:00
|
|
|
onFocus={this.handleFocusBlur} onBlur={this.handleFocusBlur} onPressEnter={this.handleSearch}
|
|
|
|
/>
|
2016-05-12 11:38:49 +08:00
|
|
|
<div className="ant-input-group-wrap">
|
|
|
|
<Button icon="search" className={btnCls} size={size} onClick={this.handleSearch} />
|
|
|
|
</div>
|
|
|
|
</InputGroup>
|
|
|
|
</div>
|
2016-01-07 16:29:12 +08:00
|
|
|
);
|
2016-05-11 09:32:33 +08:00
|
|
|
},
|
2015-12-02 19:33:26 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
ReactDOM.render(
|
2016-04-06 18:12:41 +08:00
|
|
|
<SearchInput placeholder="input search text"
|
2016-06-06 13:54:10 +08:00
|
|
|
onSearch={value => console.log(value)} style={{ width: 200 }}
|
|
|
|
/>
|
2015-12-29 12:08:58 +08:00
|
|
|
, mountNode);
|
2015-12-02 19:33:26 +08:00
|
|
|
````
|