|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, shallowRef, useAttrs } from 'vue' |
| 3 | +import { Primitive } from '../primitive/index.ts' |
| 4 | +import { composeEventHandlers } from '../utils/composeEventHandlers.ts' |
| 5 | +import { isFunction } from '../utils/is.ts' |
| 6 | +import { type RadioEmits, type RadioProps, getState, provideRadioContext } from './Radio.ts' |
| 7 | +import BubbleInput from './BubbleInput.vue' |
| 8 | +
|
| 9 | +defineOptions({ |
| 10 | + name: 'Radio', |
| 11 | + inheritAttrs: false, |
| 12 | +}) |
| 13 | +
|
| 14 | +const props = withDefaults(defineProps<RadioProps>(), { |
| 15 | + as: 'button', |
| 16 | + value: 'on', |
| 17 | + checked: false, |
| 18 | +}) |
| 19 | +const emit = defineEmits<RadioEmits>() |
| 20 | +const attrs = useAttrs() |
| 21 | +const elRef = shallowRef<HTMLButtonElement>() |
| 22 | +
|
| 23 | +const hasConsumerStoppedPropagation = shallowRef(false) |
| 24 | +// We set this to true by default so that events bubble to forms without JS (SSR) |
| 25 | +const isFormControl = computed(() => elRef.value ? Boolean(elRef.value.closest('form')) : true) |
| 26 | +
|
| 27 | +type CliclEvent = Event & { _stopPropagation: Event['stopPropagation'], _isPropagationStopped: boolean, isPropagationStopped: () => boolean } |
| 28 | +const onClick = composeEventHandlers<CliclEvent>((event) => { |
| 29 | + event._stopPropagation = event.stopPropagation |
| 30 | + event._isPropagationStopped = false |
| 31 | + event.stopPropagation = function stopPropagation() { |
| 32 | + this._isPropagationStopped = true |
| 33 | + event._stopPropagation() |
| 34 | + } |
| 35 | + event.isPropagationStopped = function isPropagationStopped() { |
| 36 | + return this._isPropagationStopped |
| 37 | + } |
| 38 | +
|
| 39 | + isFunction(attrs.onClick) && attrs.onClick(event) |
| 40 | +}, (event) => { |
| 41 | + // radios cannot be unchecked so we only communicate a checked state |
| 42 | + if (!props.checked) |
| 43 | + emit('update:checked', true) |
| 44 | + if (isFormControl.value) { |
| 45 | + hasConsumerStoppedPropagation.value = event.isPropagationStopped() |
| 46 | + // if radio is in a form, stop propagation from the button so that we only propagate |
| 47 | + // one click event (from the input). We propagate changes from an input so that native |
| 48 | + // form validation works and form events reflect radio updates. |
| 49 | + if (!hasConsumerStoppedPropagation.value) |
| 50 | + event.stopPropagation() |
| 51 | + } |
| 52 | +}) |
| 53 | +
|
| 54 | +provideRadioContext({ |
| 55 | + checked() { |
| 56 | + return props.checked |
| 57 | + }, |
| 58 | + disabled() { |
| 59 | + return props.disabled |
| 60 | + }, |
| 61 | +}) |
| 62 | +
|
| 63 | +defineExpose({ |
| 64 | + $el: elRef, |
| 65 | +}) |
| 66 | +</script> |
| 67 | + |
| 68 | +<template> |
| 69 | + <Primitive |
| 70 | + :ref="(el: any) => elRef = el?.$el" |
| 71 | + :as="as" |
| 72 | + :as-child="asChild" |
| 73 | + type="button" |
| 74 | + role="radio" |
| 75 | + :aria-checked="checked" |
| 76 | + :data-state="getState(checked)" |
| 77 | + :data-disabled="disabled ? '' : undefined" |
| 78 | + :disabled="disabled" |
| 79 | + :value="value" |
| 80 | + v-bind="{ |
| 81 | + ...attrs, |
| 82 | + onClick, |
| 83 | + }" |
| 84 | + > |
| 85 | + <slot /> |
| 86 | + </Primitive> |
| 87 | + |
| 88 | + <BubbleInput |
| 89 | + v-if="isFormControl" |
| 90 | + :control="elRef" |
| 91 | + :bubbles="!hasConsumerStoppedPropagation" |
| 92 | + :name="name" |
| 93 | + :value="value" |
| 94 | + :checked="checked" |
| 95 | + :required="required" |
| 96 | + :disabled="disabled" |
| 97 | + :style="{ |
| 98 | + transform: 'translateX(-100%)', |
| 99 | + }" |
| 100 | + /> |
| 101 | +</template> |
0 commit comments