TabularSection.js
6.3 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
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')}></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')}></i></Button>
</h3>
}
{hideSection?'':<hr className={cx(s.section_hr)}/>}
{this.renderTabularWrap()}
</div>
)
}
}
export default TabularSection;