|
3 | 3 | class="nt-collapse" |
4 | 4 | :class="{ |
5 | 5 | 'nt-collapse--background': props.background, |
6 | | - 'nt-collapse--space': props.itemSpace != null |
| 6 | + 'nt-collapse--space': props.gap != null, |
7 | 7 | }" |
8 | 8 | :style="styles" |
9 | 9 | > |
10 | 10 | <slot></slot> |
11 | 11 | </div> |
12 | 12 | </template> |
13 | 13 | <script setup lang="ts"> |
14 | | -import { computed, provide, ref, watch } from 'vue' |
15 | | -import { collapseContext } from './constant' |
| 14 | +import { computed, provide, ref, watch } from 'vue'; |
| 15 | +import { collapseContext } from './constant'; |
16 | 16 |
|
17 | 17 | const props = withDefaults( |
18 | 18 | defineProps<{ |
19 | 19 | /** 是否手风琴模式 */ |
20 | | - accordion?: boolean |
| 20 | + accordion?: boolean; |
21 | 21 | /** 箭头位置 */ |
22 | | - arrowPlacement?: 'left' | 'right' |
| 22 | + arrowPlacement?: 'left' | 'right'; |
23 | 23 | /** 面板头部, 水平对齐方式 */ |
24 | | - headerJustify?: 'space-between' | 'flex-start' |
| 24 | + headerJustify?: 'space-between' | 'flex-start'; |
25 | 25 | /** 是否带有边框和背景 */ |
26 | | - background?: boolean |
| 26 | + background?: boolean; |
27 | 27 | /** 面板之间的间隔 */ |
28 | | - itemSpace?: string |
| 28 | + gap?: string; |
29 | 29 | /** 默认展开的面板 */ |
30 | | - defaultExpandedNames?: (string | number)[] |
| 30 | + defaultExpandedNames?: (string | number)[]; |
31 | 31 | /** 边框圆角 */ |
32 | | - borderRadius?: string |
33 | | - modelValue?: (string | number)[] |
| 32 | + borderRadius?: string; |
| 33 | + modelValue?: (string | number)[]; |
34 | 34 | }>(), |
35 | 35 | { |
36 | 36 | accordion: false, |
37 | 37 | arrowPlacement: 'left', |
38 | 38 | headerJustify: undefined, |
39 | 39 | background: false, |
40 | | - itemSpace: undefined, |
| 40 | + gap: undefined, |
41 | 41 | defaultExpandedNames: () => [], |
42 | | - modelValue: () => [] |
43 | | - } |
44 | | -) |
| 42 | + modelValue: () => [], |
| 43 | + }, |
| 44 | +); |
45 | 45 |
|
46 | | -const emits = defineEmits(['change']) |
| 46 | +const emits = defineEmits(['change', 'update:modelValue']); |
47 | 47 |
|
48 | 48 | /** 当前展开的面板 */ |
49 | 49 |
|
50 | | -const values = defineModel<(string | number)[]>({ default: [] }) |
51 | | -const actives = ref<(string | number)[]>(values.value) |
| 50 | +const actives = ref<(string | number)[]>(props.modelValue); |
52 | 51 |
|
53 | 52 | const styles = computed(() => { |
54 | | - const styleRes: { [index: string]: string } = {} |
55 | | - if (props.itemSpace != null) { |
56 | | - styleRes['--nt-collapse-item-space'] = props.itemSpace |
| 53 | + const styleRes: { [index: string]: string } = {}; |
| 54 | + if (props.gap != null) { |
| 55 | + styleRes['--nt-collapse-gap'] = props.gap; |
57 | 56 | } |
58 | 57 | if (props.borderRadius != null) { |
59 | | - styleRes['--nt-collapse-border-radius'] = props.borderRadius |
| 58 | + styleRes['--nt-collapse-border-radius'] = props.borderRadius; |
60 | 59 | } |
61 | | - return styleRes |
62 | | -}) |
| 60 | + return styleRes; |
| 61 | +}); |
63 | 62 |
|
64 | 63 | /** |
65 | 64 | * 更新活动面板 |
66 | 65 | * @param name 面板名称 |
67 | 66 | */ |
68 | 67 | function updateActive(name: string | number) { |
69 | | - const index = actives.value.indexOf(name) |
| 68 | + const index = actives.value.indexOf(name); |
70 | 69 | if (index === -1) { |
71 | 70 | if (props.accordion) { |
72 | | - actives.value = [name] |
| 71 | + actives.value = [name]; |
73 | 72 | } else { |
74 | | - actives.value.push(name) |
| 73 | + actives.value.push(name); |
75 | 74 | } |
76 | 75 | } else { |
77 | | - actives.value.splice(index, 1) |
| 76 | + actives.value.splice(index, 1); |
78 | 77 | } |
79 | | - values.value = actives.value |
80 | | - emits('change', actives.value) |
| 78 | + emits('update:modelValue', actives.value); |
| 79 | + emits('change', actives.value); |
81 | 80 | } |
82 | 81 |
|
83 | | -watch(values, (v) => { |
84 | | - if (v != null) { |
85 | | - actives.value = v |
86 | | - } |
87 | | -}) |
| 82 | +watch( |
| 83 | + () => props.modelValue, |
| 84 | + (v) => { |
| 85 | + if (v != null) { |
| 86 | + actives.value = v; |
| 87 | + } |
| 88 | + }, |
| 89 | +); |
88 | 90 |
|
89 | 91 | provide(collapseContext, { |
90 | 92 | arrowPlacement: props.arrowPlacement, |
91 | 93 | headerJustify: props.headerJustify, |
92 | 94 | actives, |
93 | | - toggle: updateActive |
94 | | -}) |
| 95 | + toggle: updateActive, |
| 96 | +}); |
95 | 97 | </script> |
0 commit comments