|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, shallowRef, toRef, useAttrs, watchEffect } from 'vue' |
| 3 | +import { useControllableState } from '../hooks/useControllableState.ts' |
| 4 | +import { Primitive } from '../primitive/index.ts' |
| 5 | +import { composeEventHandlers } from '../utils/composeEventHandlers.ts' |
| 6 | +import { type CheckboxEmits, type CheckboxProps, provideCheckboxContext } from './Checkbox.ts' |
| 7 | +import { getState, isIndeterminate } from './utils.ts' |
| 8 | +import BubbleInput from './BubbleInput.vue' |
| 9 | +
|
| 10 | +defineOptions({ |
| 11 | + name: 'Checkbox', |
| 12 | + inheritAttrs: false, |
| 13 | +}) |
| 14 | +
|
| 15 | +const props = withDefaults(defineProps<CheckboxProps>(), { |
| 16 | + checked: undefined, |
| 17 | + value: 'on', |
| 18 | + as: 'button', |
| 19 | +}) |
| 20 | +const emit = defineEmits<CheckboxEmits>() |
| 21 | +const attrs = useAttrs() |
| 22 | +const elRef = shallowRef<HTMLButtonElement>() |
| 23 | +
|
| 24 | +const hasConsumerStoppedPropagation = shallowRef(false) |
| 25 | +// We set this to true by default so that events bubble to forms without JS (SSR) |
| 26 | +const isFormControl = computed(() => elRef.value ? Boolean(elRef.value.closest('form')) : true) |
| 27 | +const checked = useControllableState(props, emit, 'checked', props.defaultChecked) |
| 28 | +
|
| 29 | +const initialCheckedStateRef = checked.value |
| 30 | +
|
| 31 | +watchEffect((onCleanup) => { |
| 32 | + const form = elRef.value?.form |
| 33 | + if (form) { |
| 34 | + const reset = () => { |
| 35 | + checked.value = initialCheckedStateRef |
| 36 | + } |
| 37 | +
|
| 38 | + form.addEventListener('reset', reset) |
| 39 | +
|
| 40 | + onCleanup(() => form.removeEventListener('reset', reset)) |
| 41 | + } |
| 42 | +}) |
| 43 | +
|
| 44 | +const onKeydown = composeEventHandlers<KeyboardEvent>((event) => { |
| 45 | + (attrs.onKeydown as Function | undefined)?.(event) |
| 46 | +}, (event) => { |
| 47 | + // According to WAI ARIA, Checkboxes don't activate on enter keypress |
| 48 | + if (event.key === 'Enter') |
| 49 | + event.preventDefault() |
| 50 | +}) |
| 51 | +
|
| 52 | +type CliclEvent = Event & { _stopPropagation: Event['stopPropagation'], _isPropagationStopped: boolean, isPropagationStopped: () => boolean } |
| 53 | +const onClick = composeEventHandlers<CliclEvent>((event) => { |
| 54 | + event._stopPropagation = event.stopPropagation |
| 55 | + event._isPropagationStopped = false |
| 56 | + event.stopPropagation = function stopPropagation() { |
| 57 | + this._isPropagationStopped = true |
| 58 | + event._stopPropagation() |
| 59 | + } |
| 60 | + event.isPropagationStopped = function isPropagationStopped() { |
| 61 | + return this._isPropagationStopped |
| 62 | + } |
| 63 | +
|
| 64 | + ;(attrs.onClick as Function | undefined)?.(event) |
| 65 | +}, (event) => { |
| 66 | + checked.value = isIndeterminate(checked.value) ? true : !checked.value |
| 67 | + if (isFormControl.value) { |
| 68 | + console.error('event.isPropagationStopped()', event.isPropagationStopped()) |
| 69 | + hasConsumerStoppedPropagation.value = event.isPropagationStopped() |
| 70 | + // if checkbox is in a form, stop propagation from the button so that we only propagate |
| 71 | + // one click event (from the input). We propagate changes from an input so that native |
| 72 | + // form validation works and form events reflect checkbox updates. |
| 73 | + if (!hasConsumerStoppedPropagation.value) |
| 74 | + event.stopPropagation() |
| 75 | + } |
| 76 | +}) |
| 77 | +
|
| 78 | +provideCheckboxContext({ |
| 79 | + disabled: toRef(props, 'disabled'), |
| 80 | + state: checked, |
| 81 | +}) |
| 82 | +
|
| 83 | +defineExpose({ |
| 84 | + $el: elRef, |
| 85 | +}) |
| 86 | +</script> |
| 87 | + |
| 88 | +<template> |
| 89 | + <Primitive |
| 90 | + :ref="(el: any) => elRef = el?.$el" |
| 91 | + :as="as" |
| 92 | + :as-child="asChild" |
| 93 | + type="button" |
| 94 | + role="checkbox" |
| 95 | + :aria-checked="isIndeterminate(checked) ? 'mixed' : checked" |
| 96 | + :aria-required="attrs.required" |
| 97 | + :data-state="getState(checked)" |
| 98 | + :data-disabled="disabled ? '' : undefined" |
| 99 | + :disabled="disabled" |
| 100 | + :value="value" |
| 101 | + v-bind="{ |
| 102 | + ...attrs, |
| 103 | + onKeydown, |
| 104 | + onClick, |
| 105 | + }" |
| 106 | + > |
| 107 | + <slot /> |
| 108 | + </Primitive> |
| 109 | + |
| 110 | + <BubbleInput |
| 111 | + v-if="isFormControl" |
| 112 | + :control="elRef" |
| 113 | + :bubbles="!hasConsumerStoppedPropagation" |
| 114 | + :name="name" |
| 115 | + :value="value" |
| 116 | + :checked="checked" |
| 117 | + :required="required" |
| 118 | + :disabled="disabled" |
| 119 | + :style="{ |
| 120 | + transform: 'translateX(-100%)', |
| 121 | + }" |
| 122 | + /> |
| 123 | +</template> |
0 commit comments