Skip to content

Commit 254c430

Browse files
committed
fix searchbar submit bug
add name for search input and onSubmit event handler for search form
1 parent f99b483 commit 254c430

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/components/searchbar/searchbar.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ import Icon from '../icon';
1212
class SearchBar extends React.Component {
1313
static propTypes = {
1414
placeholder: React.PropTypes.string,
15+
searchName: React.PropTypes.string,
1516
onChange: React.PropTypes.func,
1617
onClear: React.PropTypes.func,
1718
onCancel: React.PropTypes.func,
19+
onSubmit: React.PropTypes.func,
1820
lang: React.PropTypes.object
1921
};
2022

2123
static defaultProps = {
2224
placeholder: '搜索',
25+
searchName: 'q',
2326
onChange: undefined,
2427
onClear: undefined,
2528
onCancel: undefined,
29+
onSubmit: undefined,
2630
lang: {
2731
cancel: '取消'
2832
}
@@ -50,21 +54,30 @@ class SearchBar extends React.Component {
5054
if(this.props.onChange) this.props.onChange('',e);
5155
}
5256

57+
submitHandle(e) {
58+
if (this.props.onSubmit) {
59+
e.preventDefault();
60+
e.stopPropagation();
61+
this.props.onSubmit(this.state.text, e);
62+
}
63+
}
64+
5365
render() {
54-
const {children, placeholder, className, ...others} = this.props;
66+
const {children, placeholder, className, searchName, ...others} = this.props;
5567
const clz = classNames({
5668
'weui_search_bar': true,
5769
'weui_search_focusing': this.state.focus
5870
}, className);
5971

6072
return (
6173
<div className={clz}>
62-
<form className='weui_search_outer'>
74+
<form className='weui_search_outer' onSubmit={this.submitHandle.bind(this)}>
6375
<div className='weui_search_inner'>
6476
<Icon value='search'/>
6577
<input
6678
ref='searchInput'
6779
type='search'
80+
name={searchName}
6881
className='weui_search_input'
6982
placeholder={placeholder}
7083
onFocus={e=>this.setState({focus:true})}

0 commit comments

Comments
 (0)