TabularSection.js
7.5 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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&§ion){
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&§ion){
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)
}}></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)
}}></i>
</td>}
</tr>);
})}
</tbody>
</table>
)
}else{
return null;
}
}
renderReadOnlyTable(tableFields,section){
const {fields,addRowAction,readOnly,employee}=this.props;
let datas=[];
if(section&§ion.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;