Section.js 8.6 KB
import React,{PropTypes} from 'react';
import ReactDOM from 'react-dom'; 

import TextField from './TextField'; 
import LookupField from './LookupField'; 
import EmailField from './EmailField'; 
import DropdownField from './DropdownField';
import DateField from './DateField';
import DateTimeField from './DateTimeField';
import FileField from './FileField';
import NumberField from './NumberField';

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


class Section extends React.Component {
	constructor(props) {
        super(props);
        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&&field.error&&field.touched){
            return 'error';
        }else if(field&&field.touched)
            return '';//success
    } 
    renderFields(){  
        const {formFields,fields,baseUrl,formSection,inlineFlag,type,service,employees,organizations}=this.props,self=this; 
        return formFields.map((formField,index)=>{ 
            if(formField.is_invisible_create)
                return null;
            switch(formField.type) {
                case 'text':  
                    return  <TextField key={index} fieldConfig={self.getFieldConfig('text',formField.name)} validateState={self.validationState(fields[formField.name])} type={type} field={fields[formField.name]} formField={formField} />;
                case 'mobile':    
                    return  <TextField key={index}  fieldConfig={self.getFieldConfig('mobile',formField.name)} validateState={self.validationState(fields[formField.name])} type={type} field={fields[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(fields[formField.name+'_ID'])} type={type} field={fields[formField.name+'_ID']}  formField={formField}   />;
                case 'email':
                    return <EmailField key={index}  fieldConfig={self.getFieldConfig('email',formField.name)} validateState={self.validationState(fields[formField.name])} type={type} field={fields[formField.name]}  formField={formField} />;
                case 'date':
                    return  <DateField key={index}  fieldConfig={self.getFieldConfig('date',formField.name)} validateState={self.validationState(fields[formField.name])} type={type} field={fields[formField.name]}   formField={formField} />;
                case 'datetime':
                    return  <DateTimeField key={index}  fieldConfig={self.getFieldConfig('date',formField.name)} validateState={self.validationState(fields[formField.name])} type={type} field={fields[formField.name]}   formField={formField} />;
                case 'dropdown':
                    return <DropdownField key={index}  fieldConfig={self.getFieldConfig('dropdown',formField.name)} validateState={self.validationState(fields[formField.name])} type={type} field={fields[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(fields[formField.name])} type={type} field={fields[formField.name]}  formField={formField}/>;
            }
        })
    }  
    renderField(formField){   
        const {formFields,fields,baseUrl,formSection,inlineFlag,type,service,employees,organizations}=this.props,self=this;
        if(formField.is_invisible_create)
            return null;
        switch(formField.type) {
            case 'text':    
                return  <TextField  fieldConfig={self.getFieldConfig('text',formField.name)} validateState={self.validationState(fields[formField.name])} type={type}  field={fields[formField.name]} formField={formField} />;
            case 'mobile':    
                return  <TextField  fieldConfig={self.getFieldConfig('mobile',formField.name)} validateState={self.validationState(fields[formField.name])} type={type}  field={fields[formField.name]}  formField={formField} />;
            case 'lookup':
                return  <LookupField  employees={employees} organizations={organizations}  service={service}  allFields={fields} baseUrl={baseUrl} fieldConfig={self.getFieldConfig('lookup',formField.name+'_ID')} validateState={self.validationState(fields[formField.name+'_ID'])} field={fields[formField.name+'_ID']}   formField={formField}   />;
            case 'email':
                return <EmailField  fieldConfig={self.getFieldConfig('email',formField.name)} validateState={self.validationState(fields[formField.name])} type={type}  field={fields[formField.name]}   formField={formField} />;
            case 'date':
                return <DateField  fieldConfig={self.getFieldConfig('date',formField.name)}  validateState={self.validationState(fields[formField.name])} type={type}  field={fields[formField.name]}   formField={formField} />;
            case 'datetime':
                return  <DateTimeField  fieldConfig={self.getFieldConfig('date',formField.name)}  validateState={self.validationState(fields[formField.name])} type={type}  field={fields[formField.name]}   formField={formField} />;
            case 'dropdown':
                return <DropdownField  fieldConfig={self.getFieldConfig('dropdown',formField.name)} validateState={self.validationState(fields[formField.name])} type={type}  field={fields[formField.name]}  formField={formField} />;
            case 'file_upload':    
                return  <FileField  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  fieldConfig={self.getFieldConfig('number',formField.name)} validateState={self.validationState(fields[formField.name])} type={type}  field={fields[formField.name]} formField={formField} />;
            default:
                return <TextField  fieldConfig={self.getFieldConfig('text',formField.name)} validateState={self.validationState(fields[formField.name])} type={type}  field={fields[formField.name]} formField={formField} />;
        }
    } 
    renderSection(){
        const rows=this.getRows(),self=this;
        return rows.map((row,index)=>{
           return <div key={index} className={cx('row')}>
                {row.map((formField,i)=>{
                    return (
                        <div key={i} className={cx('col-sm-6')}>
                            {self.renderField(formField)}
                        </div>
                    )
                })}
            </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}</h3>}
                {hideSection?'':<hr className={cx(s.section_hr)}/>} 
                {column>1?this.renderSection():this.renderFields()} 
            </div> 
        )
    }
}

 
export default Section;