react-klasha
Version:
This is an reactJS library for implementing klasha payment gateway
34 lines (33 loc) • 982 B
JavaScript
export const watchForOptions = (containerEl, tagName, onChange) => {
/* tslint:disable-next-line */
if (typeof MutationObserver === 'undefined') {
return;
}
const mutation = new MutationObserver(mutationList => {
onChange(getSelectedOption(mutationList, tagName));
});
mutation.observe(containerEl, {
childList: true,
subtree: true
});
return mutation;
};
const getSelectedOption = (mutationList, tagName) => {
let newOption;
mutationList.forEach(mut => {
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < mut.addedNodes.length; i++) {
newOption = findCheckedOption(mut.addedNodes[i], tagName) || newOption;
}
});
return newOption;
};
export const findCheckedOption = (el, tagName) => {
if (el.nodeType !== 1) {
return undefined;
}
const options = (el.tagName === tagName.toUpperCase())
? [el]
: Array.from(el.querySelectorAll(tagName));
return options.find((o) => o.value === el.value);
};