|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, defineComponent } from 'vue' |
| 3 | +import classNames from 'classnames' |
| 4 | +import type { VariantJSWithClassesListProps } from '@/utils/getVariantProps' |
| 5 | +import { getVariantPropsWithClassesList } from '@/utils/getVariantProps' |
| 6 | +import type { UCheckbox } from '@/Types/componentsTypes/components' |
| 7 | +import { Components } from '@/Types/enums/Components' |
| 8 | +import { useFormEvents } from '@/composables/useFormEvents' |
| 9 | +import { useVariants } from '@/composables/useVariants' |
| 10 | +import nuxtLabsTheme from '@/theme/nuxtLabsTheme' |
| 11 | +
|
| 12 | +const props = defineProps({ |
| 13 | + ...getVariantPropsWithClassesList<UCheckbox>(), |
| 14 | + value: { |
| 15 | + type: [String, Number, Boolean, Object], |
| 16 | + default: null, |
| 17 | + }, |
| 18 | + modelValue: { |
| 19 | + type: [Boolean, Array], |
| 20 | + default: null, |
| 21 | + }, |
| 22 | + name: { |
| 23 | + type: String, |
| 24 | + default: null, |
| 25 | + }, |
| 26 | + disabled: { |
| 27 | + type: Boolean, |
| 28 | + default: false, |
| 29 | + }, |
| 30 | + checked: { |
| 31 | + type: Boolean, |
| 32 | + default: false, |
| 33 | + }, |
| 34 | + indeterminate: { |
| 35 | + type: Boolean, |
| 36 | + default: false, |
| 37 | + }, |
| 38 | + help: { |
| 39 | + type: String, |
| 40 | + default: null, |
| 41 | + }, |
| 42 | + label: { |
| 43 | + type: String, |
| 44 | + default: null, |
| 45 | + }, |
| 46 | + required: { |
| 47 | + type: Boolean, |
| 48 | + default: false, |
| 49 | + }, |
| 50 | + color: { |
| 51 | + type: String, |
| 52 | + default: () => nuxtLabsTheme.UCheckbox.base.default.color, |
| 53 | + }, |
| 54 | +}) |
| 55 | +
|
| 56 | +const emits = defineEmits<{ |
| 57 | + (event: 'update:modelValue', value: any): void |
| 58 | + (event: 'change', value: any): void |
| 59 | +}>() |
| 60 | +const variant = computed(() => { |
| 61 | + const customProps = { |
| 62 | + ...props, |
| 63 | + variant: props.variant, |
| 64 | + } |
| 65 | + return useVariants<UCheckbox>( |
| 66 | + Components.UCheckbox, |
| 67 | + customProps as VariantJSWithClassesListProps<UCheckbox>, |
| 68 | + ) |
| 69 | +}) |
| 70 | +const { emitFormBlur } = useFormEvents() |
| 71 | +
|
| 72 | +const toggle = computed({ |
| 73 | + get() { |
| 74 | + return props.modelValue |
| 75 | + }, |
| 76 | + set(value) { |
| 77 | + emits('update:modelValue', value) |
| 78 | + }, |
| 79 | +}) |
| 80 | +
|
| 81 | +function onChange(event: Event) { |
| 82 | + emits('change', event) |
| 83 | + emitFormBlur() |
| 84 | +} |
| 85 | +
|
| 86 | +const inputClass = computed(() => { |
| 87 | + return classNames( |
| 88 | + variant.value.base, |
| 89 | + variant.value.rounded, |
| 90 | + variant.value.background, |
| 91 | + variant.value.border, |
| 92 | + nuxtLabsTheme.UCheckbox.base.ring.replaceAll('{color}', props.color), |
| 93 | + nuxtLabsTheme.UCheckbox.base.color.replaceAll('{color}', props.color), |
| 94 | + ) |
| 95 | +}) |
| 96 | +</script> |
| 97 | + |
| 98 | +<script lang="ts"> |
| 99 | +export default defineComponent({ |
| 100 | + name: Components.UCheckbox, |
| 101 | + inheritAttrs: false, |
| 102 | +}) |
| 103 | +</script> |
| 104 | + |
| 105 | +<template> |
| 106 | + <div :class="variant.root"> |
| 107 | + <div class="flex items-center h-5"> |
| 108 | + <input |
| 109 | + :id="name" |
| 110 | + v-model="toggle" |
| 111 | + :name="name" |
| 112 | + :required="required" |
| 113 | + :value="value" |
| 114 | + :disabled="disabled" |
| 115 | + :checked="checked" |
| 116 | + :indeterminate="indeterminate" |
| 117 | + type="checkbox" |
| 118 | + class="form-checkbox" |
| 119 | + :class="inputClass" |
| 120 | + v-bind="$attrs" |
| 121 | + @change="onChange" |
| 122 | + > |
| 123 | + </div> |
| 124 | + <div v-if="label || $slots.label" class="ms-3 text-sm"> |
| 125 | + <label :for="name" :class="variant.label"> |
| 126 | + <slot name="label">{{ label }}</slot> |
| 127 | + <span v-if="required" :class="variant.required">*</span> |
| 128 | + </label> |
| 129 | + <p v-if="help" :class="variant.help"> |
| 130 | + {{ help }} |
| 131 | + </p> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | +</template> |
0 commit comments