Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change event listener for SlideToggle #562

Closed
dumtux opened this issue Nov 16, 2022 · 4 comments
Closed

Change event listener for SlideToggle #562

dumtux opened this issue Nov 16, 2022 · 4 comments
Labels
feature request Request a feature or introduce and update to the project.

Comments

@dumtux
Copy link

dumtux commented Nov 16, 2022

Describe what feature you'd like. Pseudo-code, mockups, or screenshots of similar solutions are encouraged!

Is it possible to have event listener of SlideToggle like this?

<script lang="ts">
	import { SlideToggle } from '@brainandbones/skeleton';
	let is_checked: boolean = false;

	function onChange() {
		console.log(is_checked);
	}
</script>

<div class="page-container">
	<SlideToggle bind:checked={is_monitoring} on:change={() => {onChange()}}>{is_checked ? 'Activated' : 'Disabled'}</SlideToggle>
</div>

How can I get on-change event instead of polling is_checked status manually?

What type of pull request would this be?

No response

Any links to similar examples or other references we should review?

No response

@dumtux dumtux added the feature request Request a feature or introduce and update to the project. label Nov 16, 2022
@endigo9740
Copy link
Contributor

endigo9740 commented Nov 16, 2022

Hey @dumtux I'd recommend checking the source code to see how we forward events for this component. Most events are attached to the hidden checkbox input within the component template:

https://github.com/Brain-Bones/skeleton/blob/dev/src/lib/components/SlideToggle/SlideToggle.svelte#L75

This is also included in the component documentation:

Screen Shot 2022-11-16 at 12 53 09 PM

So an on:change event handler should work but you'll need to reformat it a bit:

<SlideToggle bind:checked={is_monitoring} on:change={onChange}</SlideToggle>

Then your console.log in the function should return the expected value. If for some reason you wish to obtain the state directly from the event, you would modify your onChange function like so:

function onChange(event: any) {
    console.log(event.target.checked);
}

NOTE: I think the type would be MouseEvent, if you want to replace any, but that's untested.

Let me know if this works for you!

@endigo9740
Copy link
Contributor

Note, small issue above that I've corrected. It should be event.target.checked not value!

@dumtux
Copy link
Author

dumtux commented Nov 16, 2022

Yes, this worked perfectly.
I had to check the documentation in more detail.

@dumtux dumtux closed this as completed Nov 16, 2022
@endigo9740
Copy link
Contributor

@dumtux No worries, glad it worked. Note we're still in the process of adding and refining events in the docs, so some components may lack desired events.

We have a ticket to address this in the near future though:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Request a feature or introduce and update to the project.
Projects
None yet
Development

No branches or pull requests

2 participants