@@ -273,7 +273,7 @@ export default {
273273 :class =" [
274274 $props.class,
275275 {
276- ['checkbox-radio-switch-' + type ]: type ,
276+ ['checkbox-radio-switch-' + internalType ]: internalType ,
277277 'checkbox-radio-switch--checked': isChecked,
278278 'checkbox-radio-switch--disabled': disabled,
279279 'checkbox-radio-switch--indeterminate': hasIndeterminate ? indeterminate : false,
@@ -308,7 +308,7 @@ export default {
308308 class =" checkbox-radio-switch__content"
309309 icon-class =" checkbox-radio-switch__icon"
310310 text-class =" checkbox-radio-switch__text"
311- :type =" type "
311+ :type =" internalType "
312312 :indeterminate =" hasIndeterminate ? indeterminate : false"
313313 :button-variant =" buttonVariant"
314314 :is-checked =" isChecked"
@@ -337,9 +337,11 @@ export default {
337337</template >
338338
339339<script >
340+ import { computed } from ' vue'
340341import NcCheckboxContent , { TYPE_BUTTON , TYPE_CHECKBOX , TYPE_RADIO , TYPE_SWITCH } from ' ./NcCheckboxContent.vue'
341342import { n , t } from ' ../../l10n.ts'
342343import { createElementId } from ' ../../utils/createElementId.ts'
344+ import { useInsideRadioGroup } from ' ../NcRadioGroup/useNcRadioGroup.ts'
343345
344346export default {
345347 name: ' NcCheckboxRadioSwitch' ,
@@ -515,16 +517,42 @@ export default {
515517
516518 emits: [' update:modelValue' ],
517519
518- setup () {
520+ setup (props , { emit }) {
521+ const radioGroup = useInsideRadioGroup ()
522+
523+ const internalType = computed (() => radioGroup? .value ? TYPE_RADIO : props .type )
524+
525+ /**
526+ * A wrapper around the model value, if inside a radio group use the injected value otherwise use the prop.
527+ */
528+ const internalModelValue = computed ({
529+ get () {
530+ if (radioGroup? .value ) {
531+ return radioGroup .value .modelValue
532+ }
533+ return props .modelValue
534+ },
535+ set (value ) {
536+ if (radioGroup? .value ) {
537+ radioGroup .value .onUpdate (value)
538+ } else {
539+ emit (' update:modelValue' , value)
540+ }
541+ },
542+ })
543+
519544 return {
545+ internalType,
546+ internalModelValue,
547+
520548 labelId: createElementId (),
521549 descriptionId: createElementId (),
522550 }
523551 },
524552
525553 computed: {
526554 isButtonType () {
527- return this .type === TYPE_BUTTON
555+ return this .internalType === TYPE_BUTTON
528556 },
529557
530558 computedWrapperElement () {
@@ -549,7 +577,7 @@ export default {
549577 },
550578
551579 iconSize () {
552- return this .type === TYPE_SWITCH
580+ return this .internalType === TYPE_SWITCH
553581 ? 36
554582 : 20
555583 },
@@ -559,7 +587,7 @@ export default {
559587 },
560588
561589 cssIconHeight () {
562- return this .type === TYPE_SWITCH
590+ return this .internalType === TYPE_SWITCH
563591 ? ' 16px'
564592 : this .cssIconSize
565593 },
@@ -576,8 +604,8 @@ export default {
576604 TYPE_RADIO ,
577605 TYPE_BUTTON ,
578606 ]
579- if (nativeTypes .includes (this .type )) {
580- return this .type
607+ if (nativeTypes .includes (this .internalType )) {
608+ return this .internalType
581609 }
582610 return TYPE_CHECKBOX
583611 },
@@ -591,12 +619,12 @@ export default {
591619 */
592620 isChecked () {
593621 if (this .value !== null ) {
594- if (Array .isArray (this .modelValue )) {
595- return [... this .modelValue ].indexOf (this .value ) > - 1
622+ if (Array .isArray (this .internalModelValue )) {
623+ return [... this .internalModelValue ].indexOf (this .value ) > - 1
596624 }
597- return this .modelValue === this .value
625+ return this .internalModelValue === this .value
598626 }
599- return this .modelValue === true
627+ return this .internalModelValue === true
600628 },
601629
602630 hasIndeterminate () {
@@ -608,19 +636,19 @@ export default {
608636 },
609637
610638 mounted () {
611- if (this .name && this .type === TYPE_CHECKBOX ) {
612- if (! Array .isArray (this .modelValue )) {
639+ if (this .name && this .internalType === TYPE_CHECKBOX ) {
640+ if (! Array .isArray (this .internalModelValue )) {
613641 throw new Error (' When using groups of checkboxes, the updated value will be an array.' )
614642 }
615643 }
616644
617645 // https://material.io/components/checkboxes#usage
618- if (this .name && this .type === TYPE_SWITCH ) {
646+ if (this .name && this .internalType === TYPE_SWITCH ) {
619647 throw new Error (' Switches are not made to be used for data sets. Please use checkboxes instead.' )
620648 }
621649
622650 // https://material.io/components/checkboxes#usage
623- if (typeof this .modelValue !== ' boolean' && this .type === TYPE_SWITCH ) {
651+ if (typeof this .internalModelValue !== ' boolean' && this .internalType === TYPE_SWITCH ) {
624652 throw new Error (' Switches can only be used with boolean as modelValue prop.' )
625653 }
626654 },
@@ -635,20 +663,20 @@ export default {
635663 }
636664
637665 // If this is a radio, there can only be one value
638- if (this .type === TYPE_RADIO ) {
639- this .$emit ( ' update:modelValue ' , this .value )
666+ if (this .internalType === TYPE_RADIO ) {
667+ this .internalModelValue = this .value
640668 return
641669 }
642670
643671 // If this is a radio, there can only be one value
644- if (this .type === TYPE_SWITCH ) {
645- this .$emit ( ' update:modelValue ' , ! this .isChecked )
672+ if (this .internalType === TYPE_SWITCH ) {
673+ this .internalModelValue = ! this .isChecked
646674 return
647675 }
648676
649677 // If the initial value was a boolean, let's keep it that way
650- if (typeof this .modelValue === ' boolean' ) {
651- this .$emit ( ' update:modelValue ' , ! this .modelValue )
678+ if (typeof this .internalModelValue === ' boolean' ) {
679+ this .internalModelValue = ! this .internalModelValue
652680 return
653681 }
654682
@@ -658,9 +686,9 @@ export default {
658686 .map ((input ) => input .value )
659687
660688 if (values .includes (this .value )) {
661- this .$emit ( ' update:modelValue ' , values .filter ((v ) => v !== this .value ) )
689+ this .internalModelValue = values .filter ((v ) => v !== this .value )
662690 } else {
663- this .$emit ( ' update:modelValue ' , [... values, this .value ])
691+ this .internalModelValue = [... values, this .value ]
664692 }
665693 },
666694
0 commit comments