( containerEl: HTMLElement, tagName: string, onChange: (el: T | undefined) => void )
| 1 | export const watchForOptions = <T extends HTMLElement>( |
| 2 | containerEl: HTMLElement, |
| 3 | tagName: string, |
| 4 | onChange: (el: T | undefined) => void |
| 5 | ) => { |
| 6 | if (typeof MutationObserver === 'undefined') { |
| 7 | return; |
| 8 | } |
| 9 | |
| 10 | const mutation = new MutationObserver((mutationList) => { |
| 11 | onChange(getSelectedOption<T>(mutationList, tagName)); |
| 12 | }); |
| 13 | mutation.observe(containerEl, { |
| 14 | childList: true, |
| 15 | subtree: true, |
| 16 | }); |
| 17 | return mutation; |
| 18 | }; |
| 19 | |
| 20 | const getSelectedOption = <T extends HTMLElement>(mutationList: MutationRecord[], tagName: string): T | undefined => { |
| 21 | let newOption: T | undefined; |
no test coverage detected