TabularSection.js 7.5 KB
import React,{PropTypes} from 'react';
import ReactDOM from 'react-dom';  
import cx from 'classnames';
import s from './form.scss';
import TextField from './TextField';
import DateField from './DateField';
import moment from 'moment';

class TabularSection extends React.Component {
	constructor(props) {
        super(props); 
        this.state={ 
        };  
    }
    static propTypes = {   
        tabularSection:PropTypes.array,
        fields:PropTypes.object,
        addRowAction:PropTypes.bool,
        readOnly:PropTypes.bool 
    } 
    static defaultProps={ 
        addRowAction:true,
        readOnly:false
    } 
    componentDidMount(){ 
        const {tabularSection,fields}=this.props;
        tabularSection.map((section,index)=>{ 
            if(fields&&fields.tabular_sections&&section){
                const {tabular_sections}=fields;
                const datas=tabular_sections[section.name]||[]; 
                if(datas&&datas.length==0){
                    const initValue={};
                    section.fields.map((field,i)=>{
                        initValue[field.name]=''
                    });
                    datas.addField(initValue);
                }
            }
        });
    } 
    renderTable(tableFields,section){  
        const {fields,addRowAction,readOnly,employee}=this.props;  
        if(fields&&fields.tabular_sections&&section){
            const {tabular_sections}=fields;
            const datas=tabular_sections[section.name]||[]; 
            return (
                <table className={cx(s.table)}>
                    <tbody>
                        <tr>
                            {tableFields.map((field,i)=>{
                                return <th key={i}>{field.display_name}</th>
                            })} 
                            {addRowAction&&<th><i className='kr_icon' style={{cursor:'pointer'}} 
                                onClick={() => { 
                                    const initValue={};
                                    section.fields.map((field,i)=>{
                                        initValue[field.name]=''
                                    }); 
                                    datas.addField(initValue)    
                                }}>&#xe617;</i>
                            </th>}
                        </tr>
                        {datas.map((data,index)=>{ 
                            return (
                                <tr key={index}>
                                {
                                    tableFields.map((field,i) => {
                                        const {display_name,options='{}',name,type}=field;
                                        const fieldOptions=JSON.parse(options); 
                                        switch(type) {
                                            case 'text':  
                                                return(
                                                    <td key={i}><TextField tabular={true}  field={data[name]}  options={fieldOptions} name={name} label=''></TextField></td> 
                                                );
                                            case 'date':
                                                return (   
                                                    <td key={i}><DateField tabular={true}  field={data[name]}  options={fieldOptions} name={name} label=''></DateField></td> 
                                                );
                                            case 'datetime':
                                                return (  
                                                    <td key={i}><TextField tabular={true}  field={data[name]}  options={fieldOptions} name={name} label=''></TextField></td> 
                                                ); 
                                            default:
                                                return (  
                                                    <td key={i}><TextField tabular={true}  field={data[name]}  options={fieldOptions} name={name} label=''></TextField></td> 
                                                );
                                        }  
                                    })
                                }
                                {addRowAction&&<td  style={{textAlign:'center',cursor:'pointer'}}><i className='kr_icon' 
                                    onClick={() => {
                                        datas.removeField(index)
                                    }}>&#xe612;</i>
                                </td>}
                            </tr>);
                        })}
                    </tbody>
                </table>
            )
        }else{
            return null;
        }
    }
    renderReadOnlyTable(tableFields,section){
        const {fields,addRowAction,readOnly,employee}=this.props;  
        let datas=[];
        if(section&&section.name&&employee&&employee.tabular_sections){
            datas=employee.tabular_sections[section.name];
        }
        return (
            <table className={cx(s.table)}>
                <tbody>
                    <tr>
                        {tableFields.map((field,i)=>{
                            return <th key={i}>{field.display_name}</th>
                        })}  
                    </tr>
                    {datas.map((data,index)=>{ 
                            return (
                                <tr key={index}>
                                {
                                    tableFields.map((field,i) => {
                                        const {display_name,options='{}',name,type}=field;
                                        const fieldOptions=JSON.parse(options);
                                        if(type=='date'&&type=='datetime'){
                                            return (<td key={i}>{moment(data[name]).format('YYYY-MM-DD')}</td>);
                                        }else{
                                            return (<td key={i}>{data[name]}</td>)
                                        } 
                                    })
                                } 
                            </tr>);
                        })}
                </tbody> 
            </table>
        )
    }
    render(){  
    	const self=this;  
        const {tabularSection,readOnly,employee}=this.props; 
        if(readOnly){
            return(
                <div  style={{float:'left', width:'100%'}} > 
                    {tabularSection.map((section,index)=>{ 
                        return (
                            <div key={index} className={cx(s.tabular_item_wrap)}>
                                <h5 className={cx(s.tabular_title)}>{section.display_name}</h5>
                                {this.renderReadOnlyTable(section.fields,section)}
                            </div>
                        )
                    })}
                </div>
            )
        }else{
            return( 
                <div  style={{float:'left', width:'100%'}} > 
                    {tabularSection.map((section,index)=>{ 
                        return (
                            <div key={index} className={cx(s.tabular_item_wrap)}>
                                <h5 className={cx(s.tabular_title)}>{section.display_name}</h5>
                                {this.renderTable(section.fields,section)}
                            </div>
                        )
                    })}
                </div> 
            )
        } 
    }
}

 
export default TabularSection;