[MDCSelect] arrow navigation error when initializing more than once
Bug report
Initializing multiple times a MDCSelect (new MDCSelect(element)) and trying to navigate the list using the arrow keyboards is moving multiple times.
Also, the change event is triggered multiple times.
Steps to reproduce
https://codepen.io/jkevingutierrez/pen/XWjOQNv
- Follow https://material.io/develop/web/components/input-controls/select-menus but Initialize more than once the MDCSelect in Javascript
- Open the MDCSelect
- Navigate using the arrow keyboards
- See navigation error
- Select some element in the list
- See on change event being triggered multiple times
const select = document.querySelector('.mdc-select')
let mdcSelect = new MDCSelect(select);
mdcSelect = new MDCSelect(select);
mdcSelect.listen('MDCSelect:change', () => {
alert(`Selected option at index ${mdcSelect.selectedIndex} with value "${mdcSelect.value}"`);
});
Actual behavior
The arrow keyboards move x number of elements, being x the number of times that the element have been initialized (new MDCSelect(element)).
Also the change element is triggered x times.
Expected behavior
The arrow keyboards should always move only one element.
The change element should triggered just once.
Screenshots
Your Environment:
| Software | Version(s) |
|---|---|
| MDC Web | 10.0 |
| Browser | Chrome Version 87.0.4280.141 (Official Build) (x86_64) |
| Operating System | macOS BigSur |
Additional context
Possible solution
- Remove listeners on the element every time that it is initialized.
- Another alternative is to provide a function to "destroy" or "unmount" the component, so it can be initialized again.
Hi there, thank you for reporting this. In the documentation example, the select component is only initialized once and most use cases follow that pattern. Can you provide some context why you need to initialize the select component more than once?
Closing due to inactivity. Please feel to reopen with the context requested above.
I am using the pattern of initializing multiple times and can give some context around that. I dynamically load some of my selects to include content that results from an external query to a remote service at a time after the initial page load. So, I initialize the select on page load, so that the styles behave properly, and then initialize it again later after the remote service provides the option values to me. I tried loading the new values into the existing instance, but couldn't get it to behave properly, so, in order to keep the state correct, I re-create the select after I load it. I have other cases that are similar where the values in the select depend upon the value selected in another select. In all cases, though, it is driven by a need to populate the options some time after page load.
@joyzhong can this be opened again 🙏 my use case is similar to the one mentioned by @somboo. I'm initializing the select with some initial values in the list. After making some requests and loading some endpoints, we need to initialize it again with more data in the list.