Section.js
8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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;