Skip to content

Instantly share code, notes, and snippets.

@amponce
Last active March 21, 2023 21:13
Show Gist options
  • Save amponce/8afdbd854768b3ab4e1b7a8d9170ba5e to your computer and use it in GitHub Desktop.
Save amponce/8afdbd854768b3ab4e1b7a8d9170ba5e to your computer and use it in GitHub Desktop.
import React, { useEffect, useRef } from 'react';
import { VaNumberInput } from '@department-of-veterans-affairs/component-library/dist/react-bindings';
const CurrencyInput = (props) => {
const inputRef = useRef(null);
useEffect(() => {
if (inputRef.current) {
console.log('Shadow root:', inputRef.current.shadowRoot);
if (inputRef.current.shadowRoot) {
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length > 0) {
const inputElement = inputRef.current.shadowRoot.querySelector('input');
console.log('Input element:', inputElement);
if (inputElement) {
inputElement.style.paddingLeft = '20px';
const currencySymbol = document.createElement('span');
currencySymbol.textContent = '$';
currencySymbol.style.position = 'absolute';
currencySymbol.style.left = '8px';
currencySymbol.style.top = '78%';
currencySymbol.style.transform = 'translateY(-50%)';
currencySymbol.style.pointerEvents = 'none';
inputRef.current.shadowRoot.prepend(currencySymbol);
// Disconnect the observer once the input element is found and modified
observer.disconnect();
}
}
});
});
// Observe changes in the shadow root
observer.observe(inputRef.current.shadowRoot, { childList: true });
}
}
}, []);
return <VaNumberInput ref={inputRef} {...props} />;
};
export default CurrencyInput;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment