TableHead.js 3.2 KB
import React,{PropTypes} from 'react';
import ReactDOM from 'react-dom';  
import cx from 'classnames';
import s from './header.scss';  

 

class TableHead extends React.Component {
    constructor(props) {
        super(props); 
        this.changeInput=this.changeInput.bind(this);
        this.showInput=this.showInput.bind(this);
        this.hideInput=this.hideInput.bind(this); 
        this.state={
            value:'',
            showInput:false,
            columnNames:[]
        }
    }
    static propTypes = {    
        onChange:PropTypes.func 
    } 
    static defaultProps={  
         onChange:()=>{

         }
    }  
    changeInput(e){
        this.setState({
            value:e.target.value
        })
    }
    showInput(){
        this.setState({
            showInput:true
        })
    }
    hideInput(){
        const {columnNames,value}=this.state;
        const {onChange}=this.props;
        if(!value){
            return ;
        }   
        columnNames.push(value);
        this.setState({
            showInput:false,
            columnNames,
            value:''
        })
        onChange(columnNames);
    }
    removeColumnByIndex(index){
        const {columnNames}=this.state; 
        const {onChange}=this.props;
        columnNames.splice(index,1)
        this.setState({
            columnNames
        })
        onChange(columnNames); //调用删除的方法,重新渲染state
    }
    componentDidMount(){  
        const {columnNames}=this.props;
        this.setState({
            columnNames
        })
    }  
    componentWillReceiveProps(nextProps){   
        const {columnNames}=nextProps;
        this.setState({
            columnNames
        })
    }
    componentDidUpdate(prevProps,prevState){  
    }
    componentWillUnmount() {  
        
    }  
    render(){     
        const {showInput,columnNames}=this.state,self=this;  
        return (
            <div className={cx(s.custom_header_wrap)} >
                <div>
                    {columnNames.map((columnName,i)=>{
                        return (
                            <div key={i} className={cx(s.column_name_wrap)}>
                                <div className={cx(s.remove_column_wrap)} onClick={self.removeColumnByIndex.bind(self,i)}><i className='kr_icon'>&#xe612;</i></div>
                                <div className={cx(s.column_name_item)}>{columnName}</div>
                            </div>
                        )
                    })}
                </div>
                <div className={cx(s.add_custom_wrap)}>
                    <div className={cx(s.column_wrap)}>
                        {showInput&&<input type="text"
                                value={this.state.value}
                                placeholder="请出入列名称"
                                onChange={this.changeInput}
                            ></input>}
                        {!showInput&&<span className={cx("kr_icon",s.input_group_addon)} onClick={this.showInput}>&#xe602;</span>}
                        {showInput&&<span className={cx("kr_icon",s.input_group_addon)} onClick={this.hideInput}>&#xe63f;</span>}
                    </div>
                </div>
            </div>
        ) 
    }
}



export default TableHead;