|
1 | | -<template> |
2 | | - <label |
3 | | - :class="[ |
4 | | - 'nt-checkbox', |
5 | | - indeterminate ? 'nt-checkbox--indeterminate' : '', |
6 | | - disabled ? 'nt-checkbox--disabled' : '', |
7 | | - type === 'button' ? 'nt-checkbox--button' : '', |
8 | | - ]" |
9 | | - > |
10 | | - <input |
11 | | - type="checkbox" |
12 | | - class="nt-checkbox__input" |
13 | | - :name="name" |
14 | | - :checked="isChecked" |
15 | | - @change="handleChange" |
16 | | - :disabled="disabled" |
17 | | - :value="value" |
18 | | - /> |
19 | | - <span v-if="type !== 'button'" class="nt-checkbox__inner"></span> |
20 | | - <span class="nt-checkbox__label">{{ label }}</span> |
21 | | - </label> |
22 | | -</template> |
23 | | -<script setup lang="ts"> |
24 | | -import { ref, inject } from 'vue'; |
| 1 | +<template></template> |
| 2 | +<script lang="ts"> |
| 3 | +import { inject, computed, defineComponent, h } from 'vue'; |
| 4 | +import type { ModelRef, PropType } from 'vue'; |
25 | 5 |
|
26 | | -const checkedModel = defineModel(); |
27 | | -
|
28 | | -const props = withDefaults( |
29 | | - defineProps<{ |
| 6 | +export default defineComponent({ |
| 7 | + props: { |
30 | 8 | /** 设置不确定状态,仅负责样式控制 */ |
31 | | - indeterminate?: boolean; |
32 | | - /** 原生 name 属性 */ |
33 | | - name?: string; |
| 9 | + indeterminate: { |
| 10 | + type: Boolean, |
| 11 | + default: false, |
| 12 | + }, |
| 13 | + disabled: { |
| 14 | + type: Boolean, |
| 15 | + default: false, |
| 16 | + }, |
34 | 17 | /** 显示的 label 文本 */ |
35 | | - label?: string; |
36 | | - disabled?: boolean; |
37 | | - value?: string | number | boolean; |
38 | | - type?: 'button'; |
39 | | - }>(), |
40 | | - { |
41 | | - indeterminate: false, |
42 | | - disabled: false, |
43 | | - value: undefined, |
| 18 | + label: String, |
| 19 | + value: { |
| 20 | + type: [String, Number, Boolean], |
| 21 | + default: undefined, |
| 22 | + }, |
| 23 | + type: String as PropType<'button'>, |
| 24 | + checked: { type: Boolean, default: undefined }, |
| 25 | + /** 原生 name 属性 */ |
| 26 | + name: String, |
| 27 | + modelValue: { |
| 28 | + type: Boolean, |
| 29 | + default: false, |
| 30 | + }, |
44 | 31 | }, |
45 | | -); |
| 32 | + emits: ['change', 'update:modelValue'], |
| 33 | + setup(props, { emit, slots }) { |
| 34 | + const { checkList, updateCheck } = inject<{ |
| 35 | + checkList: null | ModelRef<(string | number)[], string>; |
| 36 | + updateCheck: null | ((value: any) => void); |
| 37 | + }>('nt-checkbox-group-check', { |
| 38 | + checkList: null, |
| 39 | + updateCheck: null, |
| 40 | + }); |
46 | 41 |
|
47 | | -const emits = defineEmits(['change']); |
| 42 | + const isChecked = computed(() => { |
| 43 | + if (checkList != null) { |
| 44 | + return checkList.value.includes(props.value as string); |
| 45 | + } |
| 46 | + if (props.value != null) { |
| 47 | + return props.modelValue === props.value; |
| 48 | + } |
| 49 | + if (props.checked != null) { |
| 50 | + return props.checked; |
| 51 | + } |
| 52 | + return props.modelValue as boolean; |
| 53 | + }); |
48 | 54 |
|
49 | | -const { checkList, updateCheck } = inject<{ |
50 | | - checkList: null | (string | number)[]; |
51 | | - updateCheck: null | ((value: any) => void); |
52 | | -}>('nt-checkbox-group-check', { |
53 | | - checkList: null, |
54 | | - updateCheck: null, |
| 55 | + function handleChange(e: Event) { |
| 56 | + const target = e.target as HTMLInputElement; |
| 57 | + const checked = target.checked; |
| 58 | + if (updateCheck != null) { |
| 59 | + updateCheck(props.value); |
| 60 | + } |
| 61 | + const value = props.value == null ? checked : props.value; |
| 62 | + emit('update:modelValue', value); |
| 63 | + emit('change', value); |
| 64 | + } |
| 65 | + return () => |
| 66 | + h( |
| 67 | + 'label', |
| 68 | + { |
| 69 | + class: [ |
| 70 | + 'nt-checkbox', |
| 71 | + props.indeterminate ? 'nt-checkbox--indeterminate' : '', |
| 72 | + props.disabled ? 'nt-checkbox--disabled' : '', |
| 73 | + props.type === 'button' ? 'nt-checkbox--button' : '', |
| 74 | + ], |
| 75 | + }, |
| 76 | + [ |
| 77 | + h('input', { |
| 78 | + type: 'checkbox', |
| 79 | + class: 'nt-checkbox__input', |
| 80 | + name: props.name, |
| 81 | + checked: isChecked.value, |
| 82 | + onChange: handleChange, |
| 83 | + disabled: props.disabled, |
| 84 | + value: props.value, |
| 85 | + }), |
| 86 | + props.type !== 'button' |
| 87 | + ? h('span', { class: 'nt-checkbox__inner' }) |
| 88 | + : null, |
| 89 | + slots.default != null || props.label != null |
| 90 | + ? h( |
| 91 | + 'span', |
| 92 | + { class: 'nt-checkbox__label' }, |
| 93 | + slots.default != null ? slots.default() : props.label, |
| 94 | + ) |
| 95 | + : null, |
| 96 | + ], |
| 97 | + ); |
| 98 | + }, |
55 | 99 | }); |
56 | | -
|
57 | | -function initIsChecked(): boolean { |
58 | | - if (checkList != null) { |
59 | | - return checkList.includes(props.value as string); |
60 | | - } |
61 | | - if (props.value != null) { |
62 | | - return checkedModel.value === props.value; |
63 | | - } |
64 | | - return checkedModel.value as boolean; |
65 | | -} |
66 | | -const isChecked = ref<boolean>(initIsChecked()); |
67 | | -
|
68 | | -function handleChange(e: Event) { |
69 | | - const target = e.target as HTMLInputElement; |
70 | | - const checked = target.checked; |
71 | | - if (updateCheck != null) { |
72 | | - updateCheck(props.value); |
73 | | - } |
74 | | - const value = props.value == null ? checked : props.value; |
75 | | - checkedModel.value = value; |
76 | | - emits('change', value); |
77 | | -} |
78 | 100 | </script> |
0 commit comments