Fix the select box issue (#9508)

* Fix the select box issue

* map => forEach

* Fix tests and a product issue

* Maybe another potential fix

* Fix a few more issues

* Change backendValue to value

* localization

* Use Array.isArray
This commit is contained in:
Amir Omidi
2020-03-27 16:59:29 -07:00
committed by GitHub
parent 626fc7894b
commit a8fd578d42
5 changed files with 78 additions and 57 deletions

View File

@@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import * as DialogHelper from './dialogHelper';
import { SelectBox } from 'sql/base/browser/ui/selectBox/selectBox';
import { SelectBox, SelectOptionItemSQL } from 'sql/base/browser/ui/selectBox/selectBox';
import { MessageType } from 'vs/base/browser/ui/inputbox/inputBox';
import { IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { InputBox } from 'sql/base/browser/ui/inputBox/inputBox';
@@ -22,7 +22,7 @@ export interface IOptionElement {
export function createOptionElement(option: azdata.ServiceOption, rowContainer: HTMLElement, options: { [name: string]: any },
optionsMap: { [optionName: string]: IOptionElement }, contextViewService: IContextViewService, onFocus: (name) => void): IOptionElement {
let possibleInputs: string[] = [];
let possibleInputs: SelectOptionItemSQL[] = [];
let optionValue = getOptionValueAndCategoryValues(option, options, possibleInputs);
let optionWidget: any;
let inputElement: HTMLElement;
@@ -69,7 +69,7 @@ export function createOptionElement(option: azdata.ServiceOption, rowContainer:
return optionElement;
}
export function getOptionValueAndCategoryValues(option: azdata.ServiceOption, options: { [optionName: string]: any }, possibleInputs: string[]): any {
export function getOptionValueAndCategoryValues(option: azdata.ServiceOption, options: { [optionName: string]: any }, possibleInputs: SelectOptionItemSQL[]): any {
let optionValue = option.defaultValue;
if (options[option.name] !== undefined) {
// if the value type is boolean, the option value can be either boolean or string
@@ -87,18 +87,18 @@ export function getOptionValueAndCategoryValues(option: azdata.ServiceOption, op
if (option.valueType === ServiceOptionType.boolean || option.valueType === ServiceOptionType.category) {
// If the option is not required, the empty string should be add at the top of possible choices
if (!option.isRequired) {
possibleInputs.push('');
possibleInputs.push({ text: '', value: '' });
}
if (option.valueType === ServiceOptionType.boolean) {
possibleInputs.push(trueInputValue, falseInputValue);
possibleInputs.push({ text: trueInputValue, value: trueInputValue }, { text: falseInputValue, value: falseInputValue });
} else {
option.categoryValues.map(c => possibleInputs.push(c.name));
option.categoryValues.forEach(c => possibleInputs.push({ text: c.displayName, value: c.name }));
}
// If the option value is not set and default value is null, the option value should be set to the first possible input.
if (optionValue === null || optionValue === undefined) {
optionValue = possibleInputs[0];
optionValue = possibleInputs[0].text;
}
}
return optionValue;

View File

@@ -10,9 +10,10 @@ import * as TypeMoq from 'typemoq';
import * as assert from 'assert';
import { ServiceOptionType } from 'sql/workbench/api/common/sqlExtHostTypes';
import { $ } from 'vs/base/browser/dom';
import { SelectOptionItemSQL } from 'sql/base/browser/ui/selectBox/selectBox';
suite('Advanced options helper tests', () => {
let possibleInputs: string[];
let possibleInputs: SelectOptionItemSQL[];
let options: { [name: string]: any };
let categoryOption: azdata.ServiceOption;
let booleanOption: azdata.ServiceOption;
@@ -35,8 +36,8 @@ suite('Advanced options helper tests', () => {
description: 'Declares the application workload type when connecting to a server',
groupName: 'Initialization',
categoryValues: [
{ displayName: 'ReadWrite', name: 'ReadWrite' },
{ displayName: 'ReadOnly', name: 'ReadOnly' }
{ displayName: 'ReadWrite', name: 'RW' },
{ displayName: 'ReadOnly', name: 'RO' }
],
defaultValue: null,
isRequired: false,
@@ -110,9 +111,12 @@ suite('Advanced options helper tests', () => {
let optionValue = OptionsDialogHelper.getOptionValueAndCategoryValues(categoryOption, options, possibleInputs);
assert.equal(optionValue, 'ReadWrite');
assert.equal(possibleInputs.length, 3);
assert.equal(possibleInputs[0], '');
assert.equal(possibleInputs[1], 'ReadWrite');
assert.equal(possibleInputs[2], 'ReadOnly');
assert.equal(possibleInputs[0].text, '');
assert.equal(possibleInputs[1].text, 'ReadWrite');
assert.equal(possibleInputs[2].text, 'ReadOnly');
assert.equal(possibleInputs[0].value, '');
assert.equal(possibleInputs[1].value, 'RW');
assert.equal(possibleInputs[2].value, 'RO');
});
test('create default and required category options should set the option value and possible inputs correctly', () => {
@@ -122,8 +126,8 @@ suite('Advanced options helper tests', () => {
let optionValue = OptionsDialogHelper.getOptionValueAndCategoryValues(categoryOption, options, possibleInputs);
assert.equal(optionValue, 'ReadWrite');
assert.equal(possibleInputs.length, 2);
assert.equal(possibleInputs[0], 'ReadWrite');
assert.equal(possibleInputs[1], 'ReadOnly');
assert.equal(possibleInputs[0].text, 'ReadWrite');
assert.equal(possibleInputs[1].text, 'ReadOnly');
});
test('create no default and not required category options should set the option value and possible inputs correctly', () => {
@@ -133,9 +137,9 @@ suite('Advanced options helper tests', () => {
let optionValue = OptionsDialogHelper.getOptionValueAndCategoryValues(categoryOption, options, possibleInputs);
assert.equal(optionValue, '');
assert.equal(possibleInputs.length, 3);
assert.equal(possibleInputs[0], '');
assert.equal(possibleInputs[1], 'ReadWrite');
assert.equal(possibleInputs[2], 'ReadOnly');
assert.equal(possibleInputs[0].text, '');
assert.equal(possibleInputs[1].text, 'ReadWrite');
assert.equal(possibleInputs[2].text, 'ReadOnly');
});
test('create no default but required category options should set the option value and possible inputs correctly', () => {
@@ -145,8 +149,8 @@ suite('Advanced options helper tests', () => {
let optionValue = OptionsDialogHelper.getOptionValueAndCategoryValues(categoryOption, options, possibleInputs);
assert.equal(optionValue, 'ReadWrite');
assert.equal(possibleInputs.length, 2);
assert.equal(possibleInputs[0], 'ReadWrite');
assert.equal(possibleInputs[1], 'ReadOnly');
assert.equal(possibleInputs[0].text, 'ReadWrite');
assert.equal(possibleInputs[1].text, 'ReadOnly');
});
test('create not required category options with option value should set the option value and possible inputs correctly', () => {
@@ -157,9 +161,9 @@ suite('Advanced options helper tests', () => {
let optionValue = OptionsDialogHelper.getOptionValueAndCategoryValues(categoryOption, options, possibleInputs);
assert.equal(optionValue, 'ReadOnly');
assert.equal(possibleInputs.length, 3);
assert.equal(possibleInputs[0], '');
assert.equal(possibleInputs[1], 'ReadWrite');
assert.equal(possibleInputs[2], 'ReadOnly');
assert.equal(possibleInputs[0].text, '');
assert.equal(possibleInputs[1].text, 'ReadWrite');
assert.equal(possibleInputs[2].text, 'ReadOnly');
});
test('create required category options with option value should set the option value and possible inputs correctly', () => {
@@ -170,8 +174,8 @@ suite('Advanced options helper tests', () => {
let optionValue = OptionsDialogHelper.getOptionValueAndCategoryValues(categoryOption, options, possibleInputs);
assert.equal(optionValue, 'ReadOnly');
assert.equal(possibleInputs.length, 2);
assert.equal(possibleInputs[0], 'ReadWrite');
assert.equal(possibleInputs[1], 'ReadOnly');
assert.equal(possibleInputs[0].text, 'ReadWrite');
assert.equal(possibleInputs[1].text, 'ReadOnly');
});
test('create default but not required boolean options should set the option value and possible inputs correctly', () => {
@@ -181,9 +185,9 @@ suite('Advanced options helper tests', () => {
let optionValue = OptionsDialogHelper.getOptionValueAndCategoryValues(booleanOption, options, possibleInputs);
assert.equal(optionValue, 'False');
assert.equal(possibleInputs.length, 3);
assert.equal(possibleInputs[0], '');
assert.equal(possibleInputs[1], 'True');
assert.equal(possibleInputs[2], 'False');
assert.equal(possibleInputs[0].text, '');
assert.equal(possibleInputs[1].text, 'True');
assert.equal(possibleInputs[2].text, 'False');
});
test('create default and required boolean options should set the option value and possible inputs correctly', () => {
@@ -193,8 +197,8 @@ suite('Advanced options helper tests', () => {
let optionValue = OptionsDialogHelper.getOptionValueAndCategoryValues(booleanOption, options, possibleInputs);
assert.equal(optionValue, 'False');
assert.equal(possibleInputs.length, 2);
assert.equal(possibleInputs[0], 'True');
assert.equal(possibleInputs[1], 'False');
assert.equal(possibleInputs[0].text, 'True');
assert.equal(possibleInputs[1].text, 'False');
});
test('create no default and not required boolean options should set the option value and possible inputs correctly', () => {
@@ -204,9 +208,9 @@ suite('Advanced options helper tests', () => {
let optionValue = OptionsDialogHelper.getOptionValueAndCategoryValues(booleanOption, options, possibleInputs);
assert.equal(optionValue, '');
assert.equal(possibleInputs.length, 3);
assert.equal(possibleInputs[0], '');
assert.equal(possibleInputs[1], 'True');
assert.equal(possibleInputs[2], 'False');
assert.equal(possibleInputs[0].text, '');
assert.equal(possibleInputs[1].text, 'True');
assert.equal(possibleInputs[2].text, 'False');
});
test('create no default but required boolean options should set the option value and possible inputs correctly', () => {
@@ -216,8 +220,8 @@ suite('Advanced options helper tests', () => {
let optionValue = OptionsDialogHelper.getOptionValueAndCategoryValues(booleanOption, options, possibleInputs);
assert.equal(optionValue, 'True');
assert.equal(possibleInputs.length, 2);
assert.equal(possibleInputs[0], 'True');
assert.equal(possibleInputs[1], 'False');
assert.equal(possibleInputs[0].text, 'True');
assert.equal(possibleInputs[1].text, 'False');
});
test('create not required boolean options with option value should set the option value and possible inputs correctly', () => {
@@ -228,9 +232,9 @@ suite('Advanced options helper tests', () => {
let optionValue = OptionsDialogHelper.getOptionValueAndCategoryValues(booleanOption, options, possibleInputs);
assert.equal(optionValue, 'True');
assert.equal(possibleInputs.length, 3);
assert.equal(possibleInputs[0], '');
assert.equal(possibleInputs[1], 'True');
assert.equal(possibleInputs[2], 'False');
assert.equal(possibleInputs[0].text, '');
assert.equal(possibleInputs[1].text, 'True');
assert.equal(possibleInputs[2].text, 'False');
});
test('create required boolean options with option value should set the option value and possible inputs correctly', () => {
@@ -241,8 +245,8 @@ suite('Advanced options helper tests', () => {
let optionValue = OptionsDialogHelper.getOptionValueAndCategoryValues(booleanOption, options, possibleInputs);
assert.equal(optionValue, 'False');
assert.equal(possibleInputs.length, 2);
assert.equal(possibleInputs[0], 'True');
assert.equal(possibleInputs[1], 'False');
assert.equal(possibleInputs[0].text, 'True');
assert.equal(possibleInputs[1].text, 'False');
});
test('create default number options should set the option value and possible inputs correctly', () => {