TabularSection.js 6.3 KB
import React,{PropTypes} from 'react';
import ReactDOM from 'react-dom'; 
import { Button } from 'react-bootstrap';
import TextField from './TextField'; 
import LookupField from './LookupField'; 
import EmailField from './EmailField'; 
import DropdownField from './DropdownField';
import DateField from './DateField';
import FileField from './FileField';
import NumberField from './NumberField';

import cx from 'classnames';
import s from '../FormRender.scss';


class TabularSection extends React.Component {
	constructor(props) {
        super(props);
        this.addTabularField=this.addTabularField.bind(this);
        this.state={
            rows:this.getRows()
        };  
    }
    static propTypes = {   
        formSection:PropTypes.object,
        fieldsConfig:PropTypes.object,
        formFields:PropTypes.array,
        fields:PropTypes.object,
        baseUrl:PropTypes.string,
        column:PropTypes.number,
        hideSection:PropTypes.bool,
        className:PropTypes.string
    } 
    static defaultProps={
        column:2
    }
    getRows(){
        const {column,formFields}=this.props;
        let rows=[];
        if(column>1){
            for (let i=0;i<formFields.length;i=i+column){
                rows.push(formFields.slice(i,i+column));
            }
        } 
        return rows;
    }
    getFieldConfig(typeName,fieldKey){
        const {fieldsConfig}=this.props;
        if(fieldsConfig)
            return Object.assign({},fieldsConfig[typeName],fieldsConfig[fieldKey]);
        else
            return {};
    }
    validationState(field){
        if(field.error&&field.touched){
            return 'error';
        }else if(field.touched)
            return '';//success
    } 
    componentDidMount(){
        const {fields,formSection,type}=this.props; 
        if('create'==type){
            fields[formSection.name].addField();
        }
    }
    renderFields(tempField){ 
        const {formFields,fields,baseUrl,formSection,inlineFlag,type,service,employees,organizations}=this.props,self=this;  
        const tabularField=tempField;    
        return formFields.map((formField,index)=>{  
            switch(formField.type) {
                case 'text':  
                    return  <TextField key={index} fieldConfig={self.getFieldConfig('text',formField.name)} validateState={self.validationState(tabularField[formField.name])} type={type} field={tabularField[formField.name]}  formField={formField}/>;
                case 'mobile':    
                    return  <TextField key={index}  fieldConfig={self.getFieldConfig('mobile',formField.name)} validateState={self.validationState(tabularField[formField.name])} type={type} field={tabularField[formField.name]}  formField={formField}/>;
                case 'lookup':
                    return  <LookupField  employees={employees} organizations={organizations} service={service} allFields={fields} baseUrl={baseUrl}  key={index}  fieldConfig={self.getFieldConfig('lookup',formField.name+'_ID')} validateState={self.validationState(tabularField[formField.name+'_ID'])} type={type} field={tabularField[formField.name+'_ID']}  formField={formField}   />;
                case 'email':
                    return <EmailField key={index}  fieldConfig={self.getFieldConfig('email',formField.name)} validateState={self.validationState(tabularField[formField.name])} type={type} field={tabularField[formField.name]}  formField={formField} />;
                case 'date':
                case 'datetime':
                    return  <DateField key={index}  fieldConfig={self.getFieldConfig('date',formField.name)} validateState={self.validationState(tabularField[formField.name])} type={type} field={tabularField[formField.name]}   formField={formField}  />;
                case 'dropdown':
                    return <DropdownField key={index}  fieldConfig={self.getFieldConfig('dropdown',formField.name)} validateState={self.validationState(tabularField[formField.name])} type={type} field={tabularField[formField.name]}  formField={formField} />;
                case 'file_upload':    
                    return  <FileField key={index} fieldConfig={self.getFieldConfig('file_upload',formField.name)} validateState={self.validationState(fields[formField.name])} type={type} field={fields[formField.name]} formField={formField} />;    
                case 'number':    
                    return  <NumberField key={index} fieldConfig={self.getFieldConfig('number',formField.name)} validateState={self.validationState(fields[formField.name])} type={type} field={fields[formField.name]}  formField={formField} />;
                default:
                    return <TextField key={index}  fieldConfig={self.getFieldConfig('text',formField.name)} validateState={self.validationState(tabularField[formField.name])} type={type} field={tabularField[formField.name]}  formField={formField}/>;
            }
        }) 
    }    
    addTabularField(){
        const {formFields,fields,formSection,baseUrl}=this.props,self=this; 
        fields[formSection.name].addField();
    } 
    renderTabularWrap(){
        const {formFields,fields,baseUrl,formSection}=this.props,self=this; 
        const tabularFields=fields[formSection.name]; 
        return tabularFields.map((tempField,index)=>{ 
            return  (
                <div key={index} className={cx(s.tabular_wrap)}>
                    <Button className={cx(s.remvoe_btn,index==0?'hidden':'')} onClick={() => { tabularFields.removeField(index); }}    bsStyle="danger"><i className={cx('kr_icon','ts_icon_times')}>&#xe612;</i></Button>
                    {self.renderFields(tempField)}
                </div>
            )
        });
    }
    render(){  
    	const self=this; 
        const {column,formSection,hideSection}=this.props; 
        return( 
            <div className={cx(s.section_wrap,this.props.className)}> 
                {!hideSection&&
                    <h3 className={cx(s.section_title)}>
                        {formSection.display_name}
                        <Button className={cx('fr')} bsStyle="success" onClick={this.addTabularField}><i className={cx('kr_icon','ts_icon_plus')}>&#xe617;</i></Button>
                    </h3>
                }
                {hideSection?'':<hr className={cx(s.section_hr)}/>} 
                {this.renderTabularWrap()} 
            </div> 
        )
    }
}

 
export default TabularSection;