2016-03-31 09:40:55 +08:00
|
|
|
---
|
|
|
|
order: 9
|
2016-10-06 18:54:28 +08:00
|
|
|
title:
|
2016-07-10 08:55:43 +08:00
|
|
|
zh-CN: 搜索框
|
|
|
|
en-US: Search Box
|
2016-03-31 09:40:55 +08:00
|
|
|
---
|
2016-02-24 15:11:50 +08:00
|
|
|
|
2016-07-10 08:55:43 +08:00
|
|
|
## zh-CN
|
|
|
|
|
2016-02-24 19:55:23 +08:00
|
|
|
带有搜索按钮的自动补全输入框。
|
2016-02-24 15:11:50 +08:00
|
|
|
|
2016-07-10 08:55:43 +08:00
|
|
|
## en-US
|
|
|
|
|
|
|
|
Autocomplete select with search field.
|
|
|
|
|
|
|
|
|
2016-02-24 15:11:50 +08:00
|
|
|
````jsx
|
|
|
|
import { Input, Select, Button, Icon } from 'antd';
|
|
|
|
import jsonp from 'jsonp';
|
|
|
|
import querystring from 'querystring';
|
2016-02-24 19:55:23 +08:00
|
|
|
import classNames from 'classnames';
|
2016-02-24 15:11:50 +08:00
|
|
|
const Option = Select.Option;
|
|
|
|
|
|
|
|
let timeout;
|
|
|
|
let currentValue;
|
|
|
|
|
|
|
|
function fetch(value, callback) {
|
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = null;
|
|
|
|
}
|
|
|
|
currentValue = value;
|
|
|
|
|
|
|
|
function fake() {
|
|
|
|
const str = querystring.encode({
|
|
|
|
code: 'utf-8',
|
|
|
|
q: value,
|
|
|
|
});
|
2016-10-06 18:54:28 +08:00
|
|
|
jsonp(`https://suggest.taobao.com/sug?${str}`, (err, d) => {
|
2016-02-24 15:11:50 +08:00
|
|
|
if (currentValue === value) {
|
|
|
|
const result = d.result;
|
|
|
|
const data = [];
|
|
|
|
result.forEach((r) => {
|
|
|
|
data.push({
|
|
|
|
value: r[0],
|
|
|
|
text: r[0],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
callback(data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout = setTimeout(fake, 300);
|
|
|
|
}
|
|
|
|
|
|
|
|
const SearchInput = React.createClass({
|
|
|
|
getInitialState() {
|
|
|
|
return {
|
|
|
|
data: [],
|
2016-02-24 19:55:23 +08:00
|
|
|
value: '',
|
|
|
|
focus: false,
|
2016-02-24 15:11:50 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
handleChange(value) {
|
2016-02-24 19:55:23 +08:00
|
|
|
this.setState({ value });
|
2016-09-27 10:06:34 +08:00
|
|
|
fetch(value, data => this.setState({ data }));
|
2016-02-24 19:55:23 +08:00
|
|
|
},
|
|
|
|
handleSubmit() {
|
|
|
|
console.log('输入框内容是: ', this.state.value);
|
|
|
|
},
|
2016-11-24 16:16:27 +08:00
|
|
|
handleFocus() {
|
|
|
|
this.setState({ focus: true });
|
|
|
|
},
|
|
|
|
handleBlur() {
|
|
|
|
this.setState({ focus: false });
|
2016-02-24 15:11:50 +08:00
|
|
|
},
|
|
|
|
render() {
|
2016-02-24 19:55:23 +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-02-24 15:11:50 +08:00
|
|
|
});
|
2016-03-04 21:42:38 +08:00
|
|
|
const options = this.state.data.map(d => <Option key={d.value}>{d.text}</Option>);
|
2016-02-24 15:11:50 +08:00
|
|
|
return (
|
2016-05-13 11:45:09 +08:00
|
|
|
<div className="ant-search-input-wrapper" style={this.props.style}>
|
|
|
|
<Input.Group className={searchCls}>
|
|
|
|
<Select
|
|
|
|
combobox
|
|
|
|
value={this.state.value}
|
|
|
|
placeholder={this.props.placeholder}
|
|
|
|
notFoundContent=""
|
|
|
|
defaultActiveFirstOption={false}
|
|
|
|
showArrow={false}
|
|
|
|
filterOption={false}
|
|
|
|
onChange={this.handleChange}
|
2016-11-24 16:16:27 +08:00
|
|
|
onFocus={this.handleFocus}
|
|
|
|
onBlur={this.handleBlur}
|
2016-06-06 13:54:10 +08:00
|
|
|
>
|
2016-05-13 11:45:09 +08:00
|
|
|
{options}
|
|
|
|
</Select>
|
|
|
|
<div className="ant-input-group-wrap">
|
|
|
|
<Button className={btnCls} onClick={this.handleSubmit}>
|
|
|
|
<Icon type="search" />
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</Input.Group>
|
|
|
|
</div>
|
2016-02-24 15:11:50 +08:00
|
|
|
);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
ReactDOM.render(
|
|
|
|
<SearchInput placeholder="input search text" style={{ width: 200 }} />
|
|
|
|
, mountNode);
|
|
|
|
````
|