1 | import React, { Component } from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 |
|
4 | const propTypes = {
|
5 | clsfix:PropTypes.string,
|
6 | onChange:PropTypes.func,
|
7 | value:PropTypes.string
|
8 | };
|
9 | const defaultProps = {
|
10 | clsfix:'u-search-tabs',
|
11 | onChange:()=>{},
|
12 | };
|
13 |
|
14 | class SearchTabs extends Component {
|
15 |
|
16 | constructor(props){
|
17 | super(props);
|
18 | this.state={
|
19 | activeValue:props.value
|
20 | }
|
21 | }
|
22 |
|
23 | componentWillReceiveProps(nextProps){
|
24 | if('value' in nextProps && nextProps.value!=this.state.activeValue){
|
25 | this.setState({
|
26 | activeValue:nextProps.value
|
27 | })
|
28 | }
|
29 | }
|
30 |
|
31 | getChildren=()=>{
|
32 | let { children,clsfix } = this.props;
|
33 | let childs = [];
|
34 | if(children.length>1){
|
35 | React.Children.map(children,(child,index)=>{
|
36 | childs.push(
|
37 | React.cloneElement(child,{
|
38 | tabClick:this.childClick,
|
39 | active:child.props.value==this.state.activeValue,
|
40 | clsfix:clsfix
|
41 | })
|
42 | )
|
43 | if(index < children.length-1){
|
44 | childs.push(
|
45 | <span className={`${clsfix}-split`} key={`split-${index}`}>|</span>
|
46 | )
|
47 | }
|
48 | })
|
49 | }else{
|
50 | childs.push(
|
51 | React.cloneElement(children,{
|
52 | tabClick:this.childClick,
|
53 | active:children.props.value==this.state.activeValue,
|
54 | clsfix:clsfix
|
55 | })
|
56 | )
|
57 | }
|
58 | return childs;
|
59 | }
|
60 | childClick=(activeValue)=>{
|
61 | this.setState({
|
62 | activeValue
|
63 | })
|
64 | this.props.onChange(activeValue)
|
65 | }
|
66 |
|
67 | render(){
|
68 |
|
69 | let { clsfix,children, onChange, value, ...others } = this.props;
|
70 |
|
71 | return(
|
72 | <div className={`${clsfix}`} {...others}>
|
73 | {
|
74 | this.getChildren()
|
75 | }
|
76 | </div>
|
77 | )
|
78 | }
|
79 | };
|
80 | SearchTabs.propTypes = propTypes;
|
81 | SearchTabs.defaultProps = defaultProps;
|
82 | export default SearchTabs; |
\ | No newline at end of file |