sdpi-components icon indicating copy to clipboard operation
sdpi-components copied to clipboard

Select doesn't properly support onchange event.

Open SENTINELITE opened this issue 2 years ago • 3 comments

I was perusing this as a potential overhaul for my PI, but I currently use the onchange="funcXYZ(this.value)" to react to changes when a new selection is made.

I can't seem to get it to work?

SENTINELITE avatar Jul 01 '23 22:07 SENTINELITE

Hey @SENTINELITE, thanks for expressing interest in the components.

Each component currently exposes a custom valuechange event which is invoked when any of the following occur:

  • When the value first loads in the PI, i.e. the component reads the settings and populates the component.
  • When the value is changed by the user.
  • When the value is changed due to the PI receiving didReceiveSettings, i.e. the plugin changed the settings and the PI is synchronizing.

Here is a usage example.

<!DOCTYPE html>
<html>

<head lang="en-gb">
    <meta charset="utf-8" />
    <script src="https://cdn.jsdelivr.net/gh/geekyeggo/sdpi-components@v2/dist/sdpi-components.js"></script>
</head>

<body>
    <sdpi-item label="Select">
        <sdpi-select>
            <option value="1">One</option>
            <option value="2">Two</option>
            <option value="3">Three</option>
        </sdpi-select>
    </sdpi-item>

    <script type="text/javascript">
        document.querySelector("sdpi-select").addEventListener('valuechange', function (ev) {
            console.log(ev.target.value); // 1, 2, or 3
        });
    </script>
</body>

</html>

Please let me know if you have questions, and any feedback is appreciated.

GeekyEggo avatar Jul 03 '23 18:07 GeekyEggo

I've also noticed that this event isn't documented anywhere on https://sdpi-components.dev, so I'll be sure to make a note to get that updated. 👍

GeekyEggo avatar Jul 03 '23 18:07 GeekyEggo

Awesome, thanks for pointing that out, Geeky! I'll give it a go next week!

SENTINELITE avatar Jul 07 '23 20:07 SENTINELITE