Yes, there are several issues with the implementation of your custom <x-input> web component that need to be addressed:
Missing Observed Attribute (readonly):
Problem: In your attributeChangedCallback, you're handling changes to the readonly attribute, but you've not included readonly in the observedAttributes array. This means changes to the readonly attribute won't trigger attributeChangedCallback.
Solution: Include 'readonly' in the observedAttributes array.
Problem: You're using logical && in template literals to conditionally render elements and attributes. When the condition is falsy, JavaScript will insert false into the template, which can cause "false" text or attributes to appear in your rendered HTML.
Solution: Use the ternary operator instead to ensure that nothing is inserted when the condition is falsy.
Problem: In your setter for value, you're dispatching a 'change' event whenever the value is set programmatically. This is not standard behavior and can lead to unexpected results, as native HTML inputs do not emit 'change' events when their value is set via JavaScript.
Solution: Remove the dispatch of the 'change' event from the setter. Events should only be dispatched in response to user interactions, not programmatic changes.
javascript
set value(val) {
if (this.#connected) {
this.shadowRoot.querySelector('input').value = val;
// Do not dispatch 'change' event here
}
this.#value = val;
}
Handling DOM Availability:
Problem: In your index.js, you're selecting elements and adding event listeners before the DOM is fully loaded. This can result in null references, as the elements may not exist yet when the script runs.
Solution: Wrap your code in a DOMContentLoaded event listener or place your script tag at the end of the <body> to ensure the DOM is ready.
javascript
document.addEventListener('DOMContentLoaded', () => {
let input = document.querySelector('x-input');
input.addEventListener('change', e => {
console.log(e.detail.value);
});
$('x-input').on('input', e => {
console.log(e.originalEvent.detail.value);
});
});
Alternatively, move your <script> tag to the end of the <body> in index.html:
html
<!-- Place your script tags just before the closing </body> tag -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script type="module" src="./index.js"></script>
</body>
</html>
Ensure CSS is Properly Included:
Problem: Your component references input.min.css, but only input.scss is provided. The CSS file may not exist or may not be correctly compiled from SCSS.
Solution: Make sure to compile input.scss to input.min.css and place it in the correct directory so that it's available at ./components/input/input.min.css.
Event Propagation Considerations:
Observation: You're stopping propagation of the original 'input' event within your shadow DOM and re-dispatching a custom event from the custom element. This is acceptable, but ensure that this behavior is intentional and documented.
Note: In your 'change' event handler (#handleChange), you might also consider stopping propagation if you don't want the original event to bubble up from the shadow DOM.
javascript
#handleChange = (e) => {
e.stopPropagation(); // Add this line if needed
this.#value = e.target.value;
this.dispatchBubbledEvent('change', this.#value);
};
Consistent Use of Private Fields:
Observation: You're using private class fields (e.g., #value), which is good for encapsulation. Just ensure that you're consistent and aware of browser support for this feature. Most modern browsers support it, but if you need to support older browsers, you may need to transpile your code with tools like Babel.
General Code Cleanup and Best Practices:
Make sure to follow consistent coding standards, such as using semicolons consistently and conforming to standard JavaScript naming conventions.
Consider adding error handling or input validation where appropriate.
Add comments to explain complex logic, especially around custom event handling and component lifecycle methods.
By addressing these issues, your custom <x-input> component should function more reliably and predictably, aligning more closely with standard web component and DOM behavior.