UNPKG

1.34 kBJavaScriptView Raw
1import React from 'react';
2import Icon from 'bee-icon';
3import FormControl from 'bee-form-control';
4import PropTypes from 'prop-types';
5
6const propTypes = {
7 prefixCls: PropTypes.string,
8 placeholder: PropTypes.string,
9 onChange: PropTypes.func,
10 handleClear: PropTypes.func
11}
12
13const defaultProps = {
14 placeholder: '',
15};
16
17class 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}
59Search.propTypes = propTypes;
60Search.defaultProps = defaultProps;
61
62export default Search;