select method
Triggers a change
and input
event once all the provided options have been selected.
If there's no <select>
element matching selector
, the method throws an error.
await handle.select(['blue']); // single selection
await handle.select(['red', 'green', 'blue']); // multiple selections
Parameters:
values
: Values of options to select. If the<select>
has themultiple
attribute, all values are considered, otherwise only the first one is taken into account.
Returns: A list of option values that have been successfully selected.
Implementation
Future<List<String>> select(List<String> values) async {
return evaluate<List<dynamic>>(r'''(element, values) => {
if (element.nodeName.toLowerCase() !== 'select')
throw new Error('Element is not a <select> element.');
const options = Array.from(element.options);
element.value = undefined;
for (const option of options) {
option.selected = values.includes(option.value);
if (option.selected && !element.multiple)
break;
}
element.dispatchEvent(new Event('input', { 'bubbles': true }));
element.dispatchEvent(new Event('change', { 'bubbles': true }));
return options.filter(option => option.selected).map(option => option.value);
}''', args: [values]).then((result) => result!.cast<String>());
}