LookupField.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
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
import React,{PropTypes} from 'react';
import ReactDOM from 'react-dom';
import LookupPicker from '../LookupPicker';
import cx from 'classnames';
import s from '../FormRender.scss';
import {post} from '../../../utils/fetch';
import {buildQuery,queryRecord,listRecord} from '../../../utils/formUtil';
class LookupField extends React.Component {
constructor(props) {
super(props);
this.changeChose=this.changeChose.bind(this);
this.getOptions=this.getOptions.bind(this);
this.loadOptions=this.loadOptions.bind(this);
this.onFocus=this.onFocus.bind(this);
this.state={
options:[],
choseNode:[],
needLoad:true
}
}
static propTypes = {
formField:PropTypes.object,
field:PropTypes.object,
validateState:PropTypes.string,
inlineFlag:PropTypes.bool,
baseUrl:PropTypes.string,
allFields:PropTypes.object,
organizations:PropTypes.array,
employees:PropTypes.array
}
static defaultProps={
organizations:[],
employees:[]
}
loadOptions(){
const {formField,fieldsConfig,field,allFields,service}=this.props,self=this;
const {needLoad}=this.state;
let filterCons=[];
if(formField&&formField.related_form_name!='employee'&&formField.related_form_name!='organization'&&service){
if(formField.related_lookup_key&&formField.related_lookup_key.length>0&&formField.related_lookup_value_from&&allFields[formField.related_lookup_value_from+'_ID'].value){
filterCons=[{'key':formField.related_lookup_key+'_ID','value':allFields[formField.related_lookup_value_from+'_ID'].value}];
}
if(fieldsConfig){
}
if(filterCons.length<1&&needLoad){
listRecord(service,formField.related_form_name).then(function(data){
self.setState({
needLoad:false,
options:data.items
});
}).catch(function(error){
});
}else if(needLoad){
queryRecord(service,formField.related_form_name,buildQuery(filterCons))
.then(function(data){
self.setState({
needLoad:false,
options:data.items
});
}).catch(function(error){
});
}
}
}
getOptions(){
const {formField,organizations,employees}=this.props;
let options=[];
if(formField.related_form_name=='organization'){
options=organizations;
}else if(formField.related_form_name=='employee'){
options=employees;
}else{
options=this.state.options;
}
return options;
}
componentDidMount(){
}
componentWillReceiveProps(nextProps){
const {formField,fieldsConfig,field,allFields,service}=nextProps;
if(formField&&formField.related_lookup_value_from&&allFields[formField.related_lookup_value_from+'_ID'].value!=this.props.allFields[formField.related_lookup_value_from+'_ID'].value){
this.setState({
needLoad:true
})
}
}
onFocus(e){
const {needLoad}=this.state;
const {formField,organizations,employees}=this.props;
if(needLoad&&formField&&formField.related_form_name!='organization'&&formField.related_form_name!='employee'){
this.changeChose('',[]);
this.loadOptions();
}
}
changeChose(value,values){
const {field}=this.props;
this.setState({
choseNode:values
});
field.onChange(value);
}
renderSubField(){
const {formField,inlineFlag}=this.props;
const {choseNode}=this.state;
return Object.keys(formField.related_sub_fields).map((key,i)=>{
return (
<div key={i} className={cx(s.field_wrap,'form-group',{
'row':inlineFlag
})}>
<label className={cx('control-label',{
'col-sm-3':inlineFlag
})}> {formField.related_sub_fields[key]['field_display_name']}</label>
<div className={cx(s.input_wrap,{
'col-sm-9':inlineFlag
})} >
<div className={cx(s.placeholder_wrap,'form-control',s.sub_fields)}>
{choseNode[0][key]}
</div>
</div>
</div>
)
});
}
render(){
const {formField,validateState,inlineFlag,field,fieldsConfig,type,fieldConfig}=this.props,self=this;
const options=JSON.parse(formField.options); console.log("@@@@@"); console.log(fieldConfig);
const {choseNode}=this.state;
return(
<div>
<div className={cx(s.field_wrap,'form-group',{
'has-success':validateState=='success',
'has-warning':validateState=='warning',
'has-error':validateState=='error',
'row':inlineFlag
})}>
<label className={cx('control-label',{
'col-sm-3':inlineFlag
})}>{formField.display_name}</label>
<div className={cx(s.input_wrap,{
'col-sm-9':inlineFlag
})} >
<LookupPicker name={formField.name} value={field.value} choices={this.getOptions()}
placeholder={options.tooltip} onChange={this.changeChose} onFocus={this.onFocus}
multi={formField.display_type?formField.display_type=='multiselect':false}
labelKey={formField.related_display_field_name} valueKey='uuid'
error={validateState=='error'} disabled={fieldConfig.readOnly||'readOnly'==type}/>
<span className={cx(s.input_help,'help-block')}><small>{field.touched?field.error:''}</small></span>
</div>
</div>
{(formField.related_sub_fields&&choseNode.length>0)&&this.renderSubField()}
</div>
)
}
}
export default LookupField;