1 | import React from 'react';
|
2 | import Icon from 'bee-icon';
|
3 | import FormControl from 'bee-form-control';
|
4 | import PropTypes from 'prop-types';
|
5 |
|
6 | const propTypes = {
|
7 | prefixCls: PropTypes.string,
|
8 | placeholder: PropTypes.string,
|
9 | onChange: PropTypes.func,
|
10 | handleClear: PropTypes.func
|
11 | }
|
12 |
|
13 | const defaultProps = {
|
14 | placeholder: '',
|
15 | };
|
16 |
|
17 | class Search extends React.Component {
|
18 | handleChange = (e) => {
|
19 | const onChange = this.props.onChange;
|
20 | if (onChange) {
|
21 | onChange(e);
|
22 | }
|
23 | }
|
24 |
|
25 | handleClear = (e) => {
|
26 | e.preventDefault();
|
27 |
|
28 | const handleClear = this.props.handleClear;
|
29 | if (handleClear) {
|
30 | handleClear(e);
|
31 | }
|
32 | }
|
33 |
|
34 | render() {
|
35 | const { placeholder, value, prefixCls } = this.props;
|
36 | const icon = (value && value.length > 0) ? (
|
37 | <a href="#" className={`${prefixCls}-action`} onClick={this.handleClear}>
|
38 | <Icon type="uf-close-c" />
|
39 | </a>
|
40 | ) : (
|
41 | <span className={`${prefixCls}-action`}><Icon type="uf-search" /></span>
|
42 | );
|
43 |
|
44 | return (
|
45 | <div>
|
46 | <FormControl
|
47 | size="sm"
|
48 | placeholder={placeholder}
|
49 | className={prefixCls}
|
50 | value={value}
|
51 | ref="input"
|
52 | onChange={this.handleChange}
|
53 | />
|
54 | {icon}
|
55 | </div>
|
56 | );
|
57 | }
|
58 | }
|
59 | Search.propTypes = propTypes;
|
60 | Search.defaultProps = defaultProps;
|
61 |
|
62 | export default Search;
|