Skip to content

Commit ba9abc1

Browse files
Fix drop down default value
1 parent 4cbb76e commit ba9abc1

File tree

3 files changed

+223
-10
lines changed

3 files changed

+223
-10
lines changed

demo/data/dialog-data.json

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,211 @@
302302
}
303303
}
304304
]
305+
},
306+
{
307+
"href": "http://localhost:3001/api/service_templates/10000000000015/service_dialogs/10000000002379",
308+
"id": 10000000002379,
309+
"description": "Test Drop downs",
310+
"display": "edit",
311+
"created_at": "2017-06-16T19:29:28Z",
312+
"updated_at": "2017-06-16T19:29:28Z",
313+
"label": "Test drop downs",
314+
"dialog_tab_id": 10000000000987,
315+
"position": 1,
316+
"dialog_fields": [
317+
{
318+
"href": "http://localhost:3001/api/service_templates/10000000000015/service_dialogs/10000000007063",
319+
"id": 1,
320+
"name": "1",
321+
"data_type": "string",
322+
"display": "edit",
323+
"display_method_options": {},
324+
"required": true,
325+
"required_method_options": {},
326+
"default_value": "2",
327+
"values": [
328+
[
329+
"1",
330+
"1"
331+
],
332+
[
333+
"2",
334+
"2"
335+
],
336+
[
337+
"4",
338+
"4"
339+
],
340+
[
341+
null,
342+
"<Choose>"
343+
]
344+
],
345+
"values_method_options": {},
346+
"options": {
347+
"sort_by": "description",
348+
"sort_order": "ascending",
349+
"force_multi_value": false
350+
},
351+
"created_at": "2017-06-16T19:29:28Z",
352+
"updated_at": "2017-06-16T19:29:28Z",
353+
"label": "String Single Select",
354+
"dialog_group_id": 10000000002379,
355+
"position": 1,
356+
"dynamic": false,
357+
"read_only": false,
358+
"visible": true,
359+
"type": "DialogFieldDropDownList",
360+
"resource_action": {
361+
"resource_type": "DialogField",
362+
"ae_attributes": {}
363+
}
364+
},
365+
{
366+
"href": "http://localhost:3001/api/service_templates/10000000000015/service_dialogs/10000000007063",
367+
"id": 2,
368+
"name": "2",
369+
"data_type": "string",
370+
"display": "edit",
371+
"display_method_options": {},
372+
"required": true,
373+
"required_method_options": {},
374+
"default_value": ["1", "2"],
375+
"values": [
376+
[
377+
"1",
378+
"1"
379+
],
380+
[
381+
"2",
382+
"2"
383+
],
384+
[
385+
"4",
386+
"4"
387+
],
388+
[
389+
null,
390+
"<Choose>"
391+
]
392+
],
393+
"values_method_options": {},
394+
"options": {
395+
"sort_by": "description",
396+
"sort_order": "ascending",
397+
"force_multi_value": true
398+
},
399+
"created_at": "2017-06-16T19:29:28Z",
400+
"updated_at": "2017-06-16T19:29:28Z",
401+
"label": "String Multi Select",
402+
"dialog_group_id": 10000000002379,
403+
"position": 1,
404+
"dynamic": false,
405+
"read_only": false,
406+
"visible": true,
407+
"type": "DialogFieldDropDownList",
408+
"resource_action": {
409+
"resource_type": "DialogField",
410+
"ae_attributes": {}
411+
}
412+
},
413+
{
414+
"href": "http://localhost:3001/api/service_templates/10000000000015/service_dialogs/10000000007063",
415+
"id": 3,
416+
"name": "3",
417+
"data_type": "integer",
418+
"display": "edit",
419+
"display_method_options": {},
420+
"required": true,
421+
"required_method_options": {},
422+
"default_value": 2,
423+
"values": [
424+
[
425+
"1",
426+
1
427+
],
428+
[
429+
"2",
430+
2
431+
],
432+
[
433+
"4",
434+
4
435+
],
436+
[
437+
null,
438+
"<Choose>"
439+
]
440+
],
441+
"values_method_options": {},
442+
"options": {
443+
"sort_by": "description",
444+
"sort_order": "ascending",
445+
"force_multi_value": false
446+
},
447+
"created_at": "2017-06-16T19:29:28Z",
448+
"updated_at": "2017-06-16T19:29:28Z",
449+
"label": "Integer Single Select",
450+
"dialog_group_id": 10000000002379,
451+
"position": 1,
452+
"dynamic": false,
453+
"read_only": false,
454+
"visible": true,
455+
"type": "DialogFieldDropDownList",
456+
"resource_action": {
457+
"resource_type": "DialogField",
458+
"ae_attributes": {}
459+
}
460+
},
461+
{
462+
"href": "http://localhost:3001/api/service_templates/10000000000015/service_dialogs/10000000007063",
463+
"id": 4,
464+
"name": "4",
465+
"data_type": "integer",
466+
"display": "edit",
467+
"display_method_options": {},
468+
"required": true,
469+
"required_method_options": {},
470+
"default_value": ["1", "2"],
471+
"values": [
472+
[
473+
"1",
474+
1
475+
],
476+
[
477+
"2",
478+
2
479+
],
480+
[
481+
"4",
482+
4
483+
],
484+
[
485+
null,
486+
"<Choose>"
487+
]
488+
],
489+
"values_method_options": {},
490+
"options": {
491+
"sort_by": "description",
492+
"sort_order": "ascending",
493+
"force_multi_value": true
494+
},
495+
"created_at": "2017-06-16T19:29:28Z",
496+
"updated_at": "2017-06-16T19:29:28Z",
497+
"label": "Integer Multi Select",
498+
"dialog_group_id": 10000000002379,
499+
"position": 1,
500+
"dynamic": false,
501+
"read_only": false,
502+
"visible": true,
503+
"type": "DialogFieldDropDownList",
504+
"resource_action": {
505+
"resource_type": "DialogField",
506+
"ae_attributes": {}
507+
}
508+
}
509+
]
305510
}
306511
]
307512
}

src/dialog-user/components/dialog-user/dialogUser.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Dialog test', () => {
2121
});
2222
it('should have some values set', () => {
2323
const expectedDialogs = ['option_1_cores_per_socket', 'option_1_vm_memory',
24-
'option_1_vm_name', 'service_name', 'tag_0_environment', 'tag_1_function'];
24+
'option_1_vm_name', 'service_name', 'tag_0_environment', 'tag_1_function', '1', '2', '3', '4'];
2525
const actualDialogs = Object.keys(dialogCtrl.dialogFields).sort();
2626
expect(expectedDialogs).toEqual(actualDialogs);
2727
});

src/dialog-user/services/dialogData.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,24 +109,32 @@ export default class DialogDataService {
109109

110110
// don't convert twice, FIXME: later refactor so that this doesn't get called twice for the same data
111111
if (data.type === 'DialogFieldDropDownList') {
112-
if (data.default_value && _.isString(data.default_value)) {
113-
if (data.options.force_multi_value) {
114-
// multi-select - convert value from JSON, assume right type
115-
defaultValue = JSON.parse(data.default_value).map((value) => this.convertDropdownValue(value, data.data_type));
116-
} else if (data.data_type === 'integer') {
117-
// single-select - convert value to the chosen default_type, API always returns string
118-
defaultValue = this.convertDropdownValue(data.default_value, data.data_type);
112+
if (data.default_value) {
113+
if (_.isString(data.default_value)) {
114+
if (data.options.force_multi_value) { // String data with array default_value
115+
defaultValue = JSON.parse(data.default_value).map((value) => this.convertDropdownValue(value, data.data_type));
116+
} else { // String data with string default_value
117+
defaultValue = this.convertDropdownValue(data.default_value, data.data_type);
118+
}
119+
} else {
120+
if (data.options.force_multi_value) { // Non-string data with array default_value
121+
defaultValue = data.default_value.map((value) => this.convertDropdownValue(value, data.data_type));
122+
} else { // Non-string data with string/integer default_value
123+
defaultValue = this.convertDropdownValue(data.default_value, data.data_type);
124+
}
119125
}
120126
} else if (data.default_value === null || data.default_value === '') {
121127
defaultValue = null;
122128
}
123129
}
124130

125131
if (data.type === 'DialogFieldTagControl') {
126-
if (data.options.force_single_value) {
132+
if (data.default_value === null || data.default_value === '') {
127133
defaultValue = null;
134+
} else if (data.options.force_single_value) {
135+
defaultValue = this.convertDropdownValue(data.default_value, data.data_type);
128136
} else {
129-
defaultValue = [];
137+
defaultValue = JSON.parse(data.default_value).map((value) => this.convertDropdownValue(value, data.data_type));
130138
}
131139
}
132140

0 commit comments

Comments
 (0)