-
Notifications
You must be signed in to change notification settings - Fork 11
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
Toggle-Group to set Toggle Radio exclusively #38
Comments
Hey Erik, do you mean like this?
<template>
<div id="app">
<Menus refresh debug />
<Panel>
<Toggle
radio
v-model="stateA"
label="Some option"
@update="updateState($event, 'A')"
:readOnly="stateA"
/>
<Toggle
radio
v-model="stateB"
label="Some option"
@update="updateState($event, 'B')"
:readOnly="stateB"
/>
<Toggle
radio
v-model="stateC"
label="Some option"
@update="updateState($event, 'C')"
:readOnly="stateC"
/>
</Panel>
</div>
</template>
<script>
export default {
data: () => ({
stateA: true,
stateB: false,
stateC: false,
}),
computed: {
/**
* If you find yourself needing an Array like [true, false, false], you could
* use a watcher on this array and trigger some handler function so the data
* is Array-based like Button-Group instead of singular like in this example
*/
selection() {
return ["A", "B", "C"]
.map((id) => this[`state${id}`])
.filter((val) => val);
},
},
methods: {
updateState(state, ID) {
// First gather up all our targets
const groupList = ["A", "B", "C"];
// Filter out the id currently being toggled
let siblings = groupList.filter((id) => id !== ID);
// If this id has been toggled true, then set all siblings to false
if (state)
siblings.forEach((sibling) => {
this[`state${sibling}`] = false;
});
else {
/**
* This should never trigger because Toggles are set to readOnly when true.
* The reasoning behind this is to prevent a user from toggling the only true
* radio button back to false, leaving us without any option selected.
*/
}
},
},
};
</script> Computed in the above isn't needed, just an example of an alternate way to collect the data in line with While it's relatively easy to write out some manual mock of this, I'm not sure that it would be as simple to create a dynamic parent component like |
Apparently the brutalism documentation doesn't include |
Hi Tom, thank you for this great example, this is the functionality I was looking for, very clear. No need for apologies at all, I now see it was all there. I'm peeking around vigorously, but there is a lot plus Vue is still new to me, so I got a little lost there. As a solution I also tried a |
A radiobutton is usually used in a group to make an exclusive option, the same way as buttons in
<Button-Group exclusive>
would behave. But there is no<Toggle-Group>
for this.As a workaround I wanted to set the states by script, but it seems a
<toggle>
always visually changes between true/false when being clicked on. I was expecting this toggle to visually stay false (radiobox-blank
) while repeatedly clicking, but it keeps on switching betweenradiobox-blank
andradiobox-marked
while the valuetoggleState
stays false.Is there another workaround or would a
<Toggle-Group>
be useful because radiobuttons always perform with more than one in a group anyway? Many thanks!The text was updated successfully, but these errors were encountered: