When trying to create a Dropdown for my plugin’s settings I found a nasty little visual bug with the API.
Steps to reproduce
Simply try to call .addOptions()
after .setValue()
when creating a Dropdown in the settings.
Expected result
The dropdown to have the correct value when “re-opening” the settings.
Actual result
When the settings are closed and then re-opened, the value of the dropdown is wrong.
The first option in the Record<string, string>
passed to .addOptions()
will override the value that was set.
Environment
- Operating system: Windows (WSL2)
- Obsidian version: v0.12.15
Additional information
Here is a larger bit of code to help contextualize the issue:
dropdownSetting(name: string, desc: string, options: Record<string, string>) {
const key = lodash.camelCase(name);
new Setting(this.containerEl)
.setName(name)
.setDesc(desc)
.addDropdown((dropdown: DropdownComponent) => {
dropdown
// These 2 lines 👇
.addOptions(options)
.setValue(this.settings[key])
// These 2 lines ☝
.onChange(this.onSettingChange(key));
});
}
The problem only occurs when those 2 line’s order is reversed.