ny
22 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<script lang="ts" setup>
import { computed, ref, unref, watch } from 'vue';
 
import { useAttrs } from '@jnpf/hooks';
 
import { Checkbox } from 'ant-design-vue';
import { omit } from 'lodash-es';
 
defineOptions({ inheritAttrs: false, name: 'JnpfCheckboxSingle' });
const props = defineProps({
  checkedValue: { default: 1, type: [Number, Boolean, String] },
  label: { default: '', type: String },
  unCheckedValue: { default: 0, type: [Number, Boolean, String] },
  value: { type: [Number, Boolean, String] },
});
const emit = defineEmits(['update:value', 'change']);
const attrs = useAttrs({ excludeDefaultKeys: false });
const innerValue = ref(false);
 
const getBindValue = computed(() => ({ ...unref(attrs), ...omit(props, ['value']) }));
 
watch(
  () => unref(props.value),
  (val) => {
    setValue(val);
  },
  { immediate: true },
);
 
function setValue(value) {
  innerValue.value = value === props.checkedValue;
}
function onChange(e) {
  const value = e.target.checked ? props.checkedValue : props.unCheckedValue;
  emit('update:value', value);
  emit('change', value);
}
</script>
 
<template>
  <Checkbox v-model:checked="innerValue" v-bind="getBindValue" @change="onChange">{{ label }}</Checkbox>
</template>