|
@@ -27,39 +27,90 @@ class ThingsboardRcSelect extends React.Component { |
|
@@ -27,39 +27,90 @@ class ThingsboardRcSelect extends React.Component { |
27
|
this.onDeselect = this.onDeselect.bind(this);
|
27
|
this.onDeselect = this.onDeselect.bind(this);
|
28
|
this.onBlur = this.onBlur.bind(this);
|
28
|
this.onBlur = this.onBlur.bind(this);
|
29
|
this.onFocus = this.onFocus.bind(this);
|
29
|
this.onFocus = this.onFocus.bind(this);
|
30
|
- let emptyValue = this.props.form.schema.type === 'array'? [] : null;
|
|
|
31
|
this.state = {
|
30
|
this.state = {
|
32
|
- currentValue: this.props.value || emptyValue,
|
31
|
+ currentValue: this.keyToCurrentValue(this.props.value, this.props.form.schema.type === 'array'),
|
33
|
items: this.props.form.items,
|
32
|
items: this.props.form.items,
|
34
|
focused: false
|
33
|
focused: false
|
35
|
};
|
34
|
};
|
36
|
}
|
35
|
}
|
37
|
|
36
|
|
|
|
37
|
+ keyToCurrentValue(key, isArray) {
|
|
|
38
|
+ var currentValue = isArray ? [] : null;
|
|
|
39
|
+ if (isArray) {
|
|
|
40
|
+ var keys = key;
|
|
|
41
|
+ if (keys) {
|
|
|
42
|
+ for (var i = 0; i < keys.length; i++) {
|
|
|
43
|
+ currentValue.push({key: keys[i], label: this.labelFromKey(keys[i])});
|
|
|
44
|
+ }
|
|
|
45
|
+ }
|
|
|
46
|
+ } else {
|
|
|
47
|
+ currentValue = {key: key, label: this.labelFromKey(key)};
|
|
|
48
|
+ }
|
|
|
49
|
+ return currentValue;
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ labelFromKey(key) {
|
|
|
53
|
+ let label = key || '';
|
|
|
54
|
+ if (key) {
|
|
|
55
|
+ for (var i=0;i<this.props.form.items.length;i++) {
|
|
|
56
|
+ var item = this.props.form.items[i];
|
|
|
57
|
+ if (item.value === key) {
|
|
|
58
|
+ label = item.label;
|
|
|
59
|
+ break;
|
|
|
60
|
+ }
|
|
|
61
|
+ }
|
|
|
62
|
+ }
|
|
|
63
|
+ return label;
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ arrayValues(items) {
|
|
|
67
|
+ var v = [];
|
|
|
68
|
+ if (items) {
|
|
|
69
|
+ for (var i = 0; i < items.length; i++) {
|
|
|
70
|
+ v.push(items[i].key);
|
|
|
71
|
+ }
|
|
|
72
|
+ }
|
|
|
73
|
+ return v;
|
|
|
74
|
+ }
|
|
|
75
|
+
|
|
|
76
|
+ keyIndex(values, key) {
|
|
|
77
|
+ var index = -1;
|
|
|
78
|
+ if (values) {
|
|
|
79
|
+ for (var i = 0; i < values.length; i++) {
|
|
|
80
|
+ if (values[i].key === key) {
|
|
|
81
|
+ index = i;
|
|
|
82
|
+ break;
|
|
|
83
|
+ }
|
|
|
84
|
+ }
|
|
|
85
|
+ }
|
|
|
86
|
+ return index;
|
|
|
87
|
+ }
|
|
|
88
|
+
|
38
|
onSelect(value, option) {
|
89
|
onSelect(value, option) {
|
39
|
if(this.props.form.schema.type === 'array') {
|
90
|
if(this.props.form.schema.type === 'array') {
|
40
|
let v = this.state.currentValue;
|
91
|
let v = this.state.currentValue;
|
41
|
- v.push(value);
|
92
|
+ v.push(this.keyToCurrentValue(value.key, false));
|
42
|
this.setState({
|
93
|
this.setState({
|
43
|
currentValue: v
|
94
|
currentValue: v
|
44
|
});
|
95
|
});
|
45
|
- this.props.onChangeValidate(v);
|
96
|
+ this.props.onChangeValidate(this.arrayValues(v));
|
46
|
} else {
|
97
|
} else {
|
47
|
- this.setState({currentValue: value});
|
|
|
48
|
- this.props.onChangeValidate({target: {value: value}});
|
98
|
+ this.setState({currentValue: this.keyToCurrentValue(value.key, false)});
|
|
|
99
|
+ this.props.onChangeValidate({target: {value: value.key}});
|
49
|
}
|
100
|
}
|
50
|
}
|
101
|
}
|
51
|
|
102
|
|
52
|
onDeselect(value, option) {
|
103
|
onDeselect(value, option) {
|
53
|
if (this.props.form.schema.type === 'array') {
|
104
|
if (this.props.form.schema.type === 'array') {
|
54
|
let v = this.state.currentValue;
|
105
|
let v = this.state.currentValue;
|
55
|
- let index = v.indexOf(value);
|
106
|
+ let index = this.keyIndex(v, value.key);
|
56
|
if (index > -1) {
|
107
|
if (index > -1) {
|
57
|
v.splice(index, 1);
|
108
|
v.splice(index, 1);
|
58
|
}
|
109
|
}
|
59
|
this.setState({
|
110
|
this.setState({
|
60
|
currentValue: v
|
111
|
currentValue: v
|
61
|
});
|
112
|
});
|
62
|
- this.props.onChangeValidate(v);
|
113
|
+ this.props.onChangeValidate(this.arrayValues(v));
|
63
|
}
|
114
|
}
|
64
|
}
|
115
|
}
|
65
|
|
116
|
|
|
@@ -105,6 +156,7 @@ class ThingsboardRcSelect extends React.Component { |
|
@@ -105,6 +156,7 @@ class ThingsboardRcSelect extends React.Component { |
105
|
combobox={this.props.form.combobox}
|
156
|
combobox={this.props.form.combobox}
|
106
|
disabled={this.props.form.readonly}
|
157
|
disabled={this.props.form.readonly}
|
107
|
value={this.state.currentValue}
|
158
|
value={this.state.currentValue}
|
|
|
159
|
+ labelInValue={true}
|
108
|
onSelect={this.onSelect}
|
160
|
onSelect={this.onSelect}
|
109
|
onDeselect={this.onDeselect}
|
161
|
onDeselect={this.onDeselect}
|
110
|
onFocus={this.onFocus}
|
162
|
onFocus={this.onFocus}
|