Skip to content

Is it possible to add support for x-model for other elements (context: shoelace form controls) #4413

Answered by rikbosch
rikbosch asked this question in 1. Help
Discussion options

You must be logged in to vote

Looking at the source of the x-model directive I don't think there is an extension point for this. But I managed to get model binding working with the following code (maybe this can help others as well).

    document.addEventListener("sl-change", (e) => {

        const el = e.target as ElementWithXAttributes;

        if (!el._x_model) {
            // no x-model defined on this element
            return null;
        }

        const setValue = el._x_model.set;

        if (el instanceof SlCheckbox) {
            setValue(el.checked);
        }
        if (el instanceof SlSelect) {
            setValue( el.value);
        }
        if (el instanceof SlRadioGroup) {
            setValue( 

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by rikbosch
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants