mirror of
https://github.com/google/blockly.git
synced 2026-01-08 01:20:12 +01:00
Feature: Add a dropdown options setter (#8833)
* Feature: Add a setOptions method to field_dropdown * add test for changing droopdown options * split out setOptions tests into their own test suite * Add additional tests * auto format files
This commit is contained in:
@@ -136,26 +136,11 @@ export class FieldDropdown extends Field<string> {
|
|||||||
// If we pass SKIP_SETUP, don't do *anything* with the menu generator.
|
// If we pass SKIP_SETUP, don't do *anything* with the menu generator.
|
||||||
if (menuGenerator === Field.SKIP_SETUP) return;
|
if (menuGenerator === Field.SKIP_SETUP) return;
|
||||||
|
|
||||||
if (Array.isArray(menuGenerator)) {
|
this.setOptions(menuGenerator);
|
||||||
this.validateOptions(menuGenerator);
|
|
||||||
const trimmed = this.trimOptions(menuGenerator);
|
|
||||||
this.menuGenerator_ = trimmed.options;
|
|
||||||
this.prefixField = trimmed.prefix || null;
|
|
||||||
this.suffixField = trimmed.suffix || null;
|
|
||||||
} else {
|
|
||||||
this.menuGenerator_ = menuGenerator;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The currently selected option. The field is initialized with the
|
|
||||||
* first option selected.
|
|
||||||
*/
|
|
||||||
this.selectedOption = this.getOptions(false)[0];
|
|
||||||
|
|
||||||
if (config) {
|
if (config) {
|
||||||
this.configure_(config);
|
this.configure_(config);
|
||||||
}
|
}
|
||||||
this.setValue(this.selectedOption[1]);
|
|
||||||
if (validator) {
|
if (validator) {
|
||||||
this.setValidator(validator);
|
this.setValidator(validator);
|
||||||
}
|
}
|
||||||
@@ -414,6 +399,28 @@ export class FieldDropdown extends Field<string> {
|
|||||||
return this.generatedOptions;
|
return this.generatedOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the options on this dropdown. This will reset the selected item to
|
||||||
|
* the first item in the list.
|
||||||
|
*
|
||||||
|
* @param menuGenerator The array of options or a generator function.
|
||||||
|
*/
|
||||||
|
setOptions(menuGenerator: MenuGenerator) {
|
||||||
|
if (Array.isArray(menuGenerator)) {
|
||||||
|
this.validateOptions(menuGenerator);
|
||||||
|
const trimmed = this.trimOptions(menuGenerator);
|
||||||
|
this.menuGenerator_ = trimmed.options;
|
||||||
|
this.prefixField = trimmed.prefix || null;
|
||||||
|
this.suffixField = trimmed.suffix || null;
|
||||||
|
} else {
|
||||||
|
this.menuGenerator_ = menuGenerator;
|
||||||
|
}
|
||||||
|
// The currently selected option. The field is initialized with the
|
||||||
|
// first option selected.
|
||||||
|
this.selectedOption = this.getOptions(false)[0];
|
||||||
|
this.setValue(this.selectedOption[1]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure that the input value is a valid language-neutral option.
|
* Ensure that the input value is a valid language-neutral option.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -195,6 +195,52 @@ suite('Dropdown Fields', function () {
|
|||||||
assertFieldValue(this.field, 'B', 'b');
|
assertFieldValue(this.field, 'B', 'b');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
suite('setOptions', function () {
|
||||||
|
setup(function () {
|
||||||
|
this.field = new Blockly.FieldDropdown([
|
||||||
|
['a', 'A'],
|
||||||
|
['b', 'B'],
|
||||||
|
['c', 'C'],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
test('With array updates options', function () {
|
||||||
|
this.field.setOptions([
|
||||||
|
['d', 'D'],
|
||||||
|
['e', 'E'],
|
||||||
|
['f', 'F'],
|
||||||
|
]);
|
||||||
|
assertFieldValue(this.field, 'D', 'd');
|
||||||
|
});
|
||||||
|
test('With generator updates options', function () {
|
||||||
|
this.field.setOptions(function () {
|
||||||
|
return [
|
||||||
|
['d', 'D'],
|
||||||
|
['e', 'E'],
|
||||||
|
['f', 'F'],
|
||||||
|
];
|
||||||
|
});
|
||||||
|
assertFieldValue(this.field, 'D', 'd');
|
||||||
|
});
|
||||||
|
test('With trimmable options gets trimmed', function () {
|
||||||
|
this.field.setOptions([
|
||||||
|
['a d b', 'D'],
|
||||||
|
['a e b', 'E'],
|
||||||
|
['a f b', 'F'],
|
||||||
|
]);
|
||||||
|
assert.deepEqual(this.field.prefixField, 'a');
|
||||||
|
assert.deepEqual(this.field.suffixField, 'b');
|
||||||
|
assert.deepEqual(this.field.getOptions(), [
|
||||||
|
['d', 'D'],
|
||||||
|
['e', 'E'],
|
||||||
|
['f', 'F'],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
test('With an empty array of options throws', function () {
|
||||||
|
assert.throws(function () {
|
||||||
|
this.field.setOptions([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
suite('Validators', function () {
|
suite('Validators', function () {
|
||||||
setup(function () {
|
setup(function () {
|
||||||
|
|||||||
Reference in New Issue
Block a user