Skip to content

Commit

Permalink
Allow changing placeholder option.
Browse files Browse the repository at this point in the history
  • Loading branch information
atmgrifter00 committed Feb 26, 2024
1 parent b858136 commit 3ba52a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/nimble-components/src/select/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,16 @@ export class Select
* @override
*/
public override handleChange(source: unknown, propertyName: string): void {
super.handleChange(source, propertyName);
// don't call super.handleChange so hidden options can be selected programmatically
if (propertyName === 'value') {
this.updateValue();
}
if (propertyName === 'selected') {
if (isListboxOption(source as Element)) {
this.selectedIndex = this.options.indexOf(source as ListboxOption);
}
this.setSelectedOptions();
}
}

/**
Expand Down Expand Up @@ -719,7 +725,7 @@ export class Select
*/
protected override setDefaultSelectedOption(): void {
const options: ListboxOption[] = this.options
?? Array.from(this.children).filter(o => isListboxOption(o as HTMLElement));
?? Array.from(this.children).filter(o => isListboxOption(o));

const optionIsSelected = (option: ListboxOption): boolean => {
return option.hasAttribute('selected') || option.selected;
Expand Down
13 changes: 13 additions & 0 deletions packages/nimble-components/src/select/tests/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,5 +710,18 @@ describe('Select', () => {

expect(element.displayValue).toBe('Two');
});

it('placeholder can be changed to another option programmatically', async () => {
await waitForUpdatesAsync();
element.options[0]!.hidden = false;
element.options[1]!.hidden = true;
element.options[1]!.disabled = true;
element.options[1]!.selected = true;

expect(element.displayValue).toBe('Two');
expect(element.value).toBe('two');
await clickAndWaitForOpen(element);
expect(pageObject.isOptionVisible(1)).toBeFalse();
});
});
});

0 comments on commit 3ba52a6

Please sign in to comment.