uview-pro 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +21 -0
- package/components/u-checkbox/u-checkbox.vue +81 -62
- package/components/u-checkbox-group/types.ts +1 -1
- package/components/u-checkbox-group/u-checkbox-group.vue +77 -26
- package/components/u-collapse/u-collapse.vue +140 -14
- package/components/u-collapse-item/u-collapse-item.vue +159 -61
- package/index.ts +11 -2
- package/libs/hooks/index.ts +1 -1
- package/libs/hooks/useComponent.ts +343 -0
- package/libs/hooks/useParent.ts +22 -20
- package/libs/index.ts +34 -5
- package/libs/util/eventBus.ts +86 -0
- package/libs/util/logger.ts +364 -0
- package/package.json +1 -1
- package/readme.md +3 -1
- package/types/global.d.ts +39 -3
package/changelog.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
## 0.2.3(2025-10-06)
|
|
2
|
+
|
|
3
|
+
### 🐛 Bug Fixes | Bug 修复
|
|
4
|
+
|
|
5
|
+
- **u-collapse:** 修复手风琴组件在头条小程序的兼容性 ([3189dc4](https://github.com/anyup/uView-Pro/commit/3189dc468edd977bc3e20256bd8a6c1b124bc4e6))
|
|
6
|
+
|
|
7
|
+
### ✨ Features | 新功能
|
|
8
|
+
|
|
9
|
+
- **util:** 新增日志功能并调整主题配置 ([5e07894](https://github.com/anyup/uView-Pro/commit/5e078945fb64867e4e49be1c9228aac7be8c1104))
|
|
10
|
+
- **components:** 重构复选框和手风琴组件,新增父子组件通信库 ([55b9b60](https://github.com/anyup/uView-Pro/commit/55b9b6069f1a40a6c4190b27952bcf9a489f8923))
|
|
11
|
+
- **util:** 添加自定义事件总线 ([6589f4f](https://github.com/anyup/uView-Pro/commit/6589f4f20b1391a9267670f6622538d4a6db0d82))
|
|
12
|
+
|
|
13
|
+
### ♻️ Code Refactoring | 代码重构
|
|
14
|
+
|
|
15
|
+
- **components:** 重构复选框和折叠组件,支持多平台小程序,微信,支付宝,头条 ([0ebc0f5](https://github.com/anyup/uView-Pro/commit/0ebc0f5edcc1df6ccd9baaaf659a0bde7b37d21d))
|
|
16
|
+
- **hooks:** 移除组件关系热更新时的错误日志输出 ([c8a2a3d](https://github.com/anyup/uView-Pro/commit/c8a2a3d041a55f94c8bd86d47eaaee9657cbd597))
|
|
17
|
+
|
|
18
|
+
### 📝 Documentation | 文档
|
|
19
|
+
|
|
20
|
+
- **readme:** 更新微信交流群二维码 ([cff3a3d](https://github.com/anyup/uView-Pro/commit/cff3a3dbb035ca365a1ab9839b14f300ef775c16))
|
|
21
|
+
|
|
1
22
|
## 0.2.2(2025-09-30)
|
|
2
23
|
|
|
3
24
|
### 🚀 Chore | 构建/工程依赖/工具
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<view class="u-checkbox" :style="[checkboxStyle]">
|
|
3
|
-
<view class="u-checkbox__icon-wrap" @tap="toggle" :class="
|
|
4
|
-
<u-icon
|
|
3
|
+
<view class="u-checkbox__icon-wrap" @tap="toggle" :class="iconClass" :style="$u.toStyle(iconStyle)">
|
|
4
|
+
<u-icon
|
|
5
|
+
class="u-checkbox__icon-wrap__icon"
|
|
6
|
+
name="checkbox-mark"
|
|
7
|
+
:size="checkboxIconSize"
|
|
8
|
+
:color="iconColor"
|
|
9
|
+
/>
|
|
5
10
|
</view>
|
|
6
11
|
<view
|
|
7
12
|
class="u-checkbox__label"
|
|
@@ -15,15 +20,24 @@
|
|
|
15
20
|
</view>
|
|
16
21
|
</template>
|
|
17
22
|
|
|
23
|
+
<script lang="ts">
|
|
24
|
+
export default {
|
|
25
|
+
name: 'u-checkbox',
|
|
26
|
+
options: {
|
|
27
|
+
addGlobalClass: true,
|
|
28
|
+
// #ifndef MP-TOUTIAO
|
|
29
|
+
virtualHost: true,
|
|
30
|
+
// #endif
|
|
31
|
+
styleIsolation: 'shared'
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
</script>
|
|
35
|
+
|
|
18
36
|
<script setup lang="ts">
|
|
19
|
-
import { computed,
|
|
20
|
-
import { $u } from '../..';
|
|
37
|
+
import { computed, onUnmounted } from 'vue';
|
|
38
|
+
import { $u, useChildren, onParentEvent } from '../..';
|
|
21
39
|
import { CheckboxProps } from './types';
|
|
22
40
|
|
|
23
|
-
defineOptions({
|
|
24
|
-
name: 'u-checkbox'
|
|
25
|
-
});
|
|
26
|
-
|
|
27
41
|
/**
|
|
28
42
|
* checkbox 复选框
|
|
29
43
|
* @description 该组件需要搭配checkboxGroup组件使用,以便用户进行操作时,获得当前复选框组的选中情况。
|
|
@@ -40,54 +54,44 @@ defineOptions({
|
|
|
40
54
|
*/
|
|
41
55
|
|
|
42
56
|
const props = defineProps(CheckboxProps);
|
|
43
|
-
|
|
44
57
|
const emit = defineEmits(['change', 'update:modelValue']);
|
|
45
58
|
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
// 父组件 group 注入
|
|
49
|
-
let parent = inject<any>('u-checkbox-group', null);
|
|
50
|
-
|
|
51
|
-
// 组件注册到 group
|
|
52
|
-
onMounted(() => {
|
|
53
|
-
// 兼容头条小程序不支持provide/inject
|
|
54
|
-
// #ifdef MP-TOUTIAO
|
|
55
|
-
parent = $u.parentData('u-checkbox-group', instance);
|
|
56
|
-
// #endif
|
|
57
|
-
// 如果存在u-checkbox-group,将本组件的实例塞进父组件的children中
|
|
58
|
-
if (parent && parent.children && !parent.children.value.includes(instanceProxy)) {
|
|
59
|
-
parent.children.value.push(instanceProxy);
|
|
60
|
-
}
|
|
61
|
-
});
|
|
59
|
+
// 使用子组件Hook
|
|
60
|
+
const { childId, parentExposed } = useChildren('u-checkbox', 'u-checkbox-group');
|
|
62
61
|
|
|
63
62
|
// 是否禁用,如果父组件u-checkbox-group禁用的话,将会忽略子组件的配置
|
|
64
63
|
const isDisabled = computed(() => {
|
|
65
|
-
return props.disabled !== '' ? props.disabled :
|
|
64
|
+
return props.disabled !== '' ? props.disabled : (parentExposed.value?.props?.disabled ?? false);
|
|
66
65
|
});
|
|
66
|
+
|
|
67
67
|
// 是否禁用label点击
|
|
68
68
|
const isLabelDisabled = computed(() => {
|
|
69
|
-
return props.labelDisabled !== '' ? props.labelDisabled :
|
|
69
|
+
return props.labelDisabled !== '' ? props.labelDisabled : (parentExposed.value?.props?.labelDisabled ?? false);
|
|
70
70
|
});
|
|
71
|
+
|
|
71
72
|
// 组件尺寸,对应size的值,默认值为34rpx
|
|
72
73
|
const checkboxSize = computed(() => {
|
|
73
|
-
return props.size ? props.size :
|
|
74
|
+
return props.size ? props.size : (parentExposed.value?.props?.size ?? 34);
|
|
74
75
|
});
|
|
76
|
+
|
|
75
77
|
// 组件的勾选图标的尺寸,默认20
|
|
76
78
|
const checkboxIconSize = computed(() => {
|
|
77
|
-
return props.iconSize ? props.iconSize :
|
|
79
|
+
return props.iconSize ? props.iconSize : (parentExposed.value?.props?.iconSize ?? 20);
|
|
78
80
|
});
|
|
81
|
+
|
|
79
82
|
// 组件选中激活时的颜色
|
|
80
83
|
const elActiveColor = computed(() => {
|
|
81
|
-
return props.activeColor ? props.activeColor :
|
|
84
|
+
return props.activeColor ? props.activeColor : (parentExposed.value?.props?.activeColor ?? 'primary');
|
|
82
85
|
});
|
|
86
|
+
|
|
83
87
|
// 组件的形状
|
|
84
88
|
const elShape = computed(() => {
|
|
85
|
-
return props.shape ? props.shape :
|
|
89
|
+
return props.shape ? props.shape : (parentExposed.value?.props?.shape ?? 'square');
|
|
86
90
|
});
|
|
91
|
+
|
|
87
92
|
// 图标样式
|
|
88
93
|
const iconStyle = computed(() => {
|
|
89
94
|
let style: Record<string, string> = {};
|
|
90
|
-
// 既要判断是否手动禁用,还要判断用户v-model绑定的值,如果绑定为false,那么也无法选中
|
|
91
95
|
if (elActiveColor.value && props.modelValue && !isDisabled.value) {
|
|
92
96
|
style.borderColor = elActiveColor.value;
|
|
93
97
|
style.backgroundColor = elActiveColor.value;
|
|
@@ -96,36 +100,35 @@ const iconStyle = computed(() => {
|
|
|
96
100
|
style.height = $u.addUnit(checkboxSize.value);
|
|
97
101
|
return style;
|
|
98
102
|
});
|
|
103
|
+
|
|
99
104
|
// checkbox内部的勾选图标,如果选中状态,为白色,否则为透明色即可
|
|
100
105
|
const iconColor = computed(() => {
|
|
101
106
|
return props.modelValue ? '#ffffff' : 'transparent';
|
|
102
107
|
});
|
|
108
|
+
|
|
103
109
|
const iconClass = computed(() => {
|
|
104
110
|
let classes: string[] = [];
|
|
105
111
|
classes.push('u-checkbox__icon-wrap--' + elShape.value);
|
|
106
112
|
if (props.modelValue == true) classes.push('u-checkbox__icon-wrap--checked');
|
|
107
113
|
if (isDisabled.value) classes.push('u-checkbox__icon-wrap--disabled');
|
|
108
114
|
if (props.modelValue && isDisabled.value) classes.push('u-checkbox__icon-wrap--disabled--checked');
|
|
109
|
-
// 支付宝小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
|
|
110
115
|
return classes.join(' ');
|
|
111
116
|
});
|
|
117
|
+
|
|
112
118
|
const checkboxStyle = computed(() => {
|
|
113
119
|
let style: Record<string, string> = {};
|
|
114
|
-
if (
|
|
115
|
-
style.width =
|
|
120
|
+
if (parentExposed.value?.props?.width) {
|
|
121
|
+
style.width = parentExposed.value.props.width;
|
|
116
122
|
// #ifdef MP
|
|
117
|
-
// 各家小程序因为它们特殊的编译结构,使用float布局
|
|
118
123
|
style.float = 'left';
|
|
119
124
|
// #endif
|
|
120
125
|
// #ifndef MP
|
|
121
|
-
|
|
122
|
-
style.flex = `0 0 ${parent.props.width}`;
|
|
126
|
+
style.flex = `0 0 ${parentExposed.value.props.width}`;
|
|
123
127
|
// #endif
|
|
124
128
|
}
|
|
125
|
-
if (
|
|
129
|
+
if (parentExposed.value?.props?.wrap) {
|
|
126
130
|
style.width = '100%';
|
|
127
131
|
// #ifndef MP
|
|
128
|
-
// H5和APP使用flex布局,将宽度设置100%,即可自动换行
|
|
129
132
|
style.flex = '0 0 100%';
|
|
130
133
|
// #endif
|
|
131
134
|
}
|
|
@@ -140,6 +143,7 @@ function onClickLabel() {
|
|
|
140
143
|
setValue();
|
|
141
144
|
}
|
|
142
145
|
}
|
|
146
|
+
|
|
143
147
|
/**
|
|
144
148
|
* 点击icon
|
|
145
149
|
*/
|
|
@@ -148,48 +152,64 @@ function toggle() {
|
|
|
148
152
|
setValue();
|
|
149
153
|
}
|
|
150
154
|
}
|
|
155
|
+
|
|
151
156
|
/**
|
|
152
157
|
* 触发change事件
|
|
153
158
|
*/
|
|
154
159
|
function emitEvent() {
|
|
155
|
-
|
|
160
|
+
const changeValue = {
|
|
156
161
|
value: !props.modelValue,
|
|
157
162
|
name: props.name
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
163
|
+
};
|
|
164
|
+
emit('change', changeValue);
|
|
165
|
+
|
|
166
|
+
// 通知父组件状态变化
|
|
161
167
|
setTimeout(() => {
|
|
162
|
-
if (
|
|
168
|
+
if (parentExposed.value?.emitEvent) {
|
|
169
|
+
parentExposed.value.emitEvent();
|
|
170
|
+
}
|
|
163
171
|
}, 80);
|
|
164
172
|
}
|
|
173
|
+
|
|
165
174
|
/**
|
|
166
|
-
*
|
|
175
|
+
* 设置通过v-model绑定的组件的值
|
|
167
176
|
*/
|
|
168
177
|
function setValue() {
|
|
169
178
|
// 判断是否超过了可选的最大数量
|
|
170
|
-
let checkedNum = 0;
|
|
171
|
-
if (parent && parent.children && parent.children.value) {
|
|
172
|
-
// 只要父组件的某一个子元素的value为true,就加1(已有的选中数量)
|
|
173
|
-
parent.children.value.forEach((val: any) => {
|
|
174
|
-
if (val.value) checkedNum++;
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
// 如果原来为选中状态,那么可以取消
|
|
178
179
|
if (props.modelValue == true) {
|
|
179
180
|
emitEvent();
|
|
180
|
-
emit('update:modelValue',
|
|
181
|
+
emit('update:modelValue', false);
|
|
181
182
|
} else {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
return $u.toast(`最多可选${parent.props.max}项`);
|
|
183
|
+
if (!parentExposed.value?.validateSelection()) {
|
|
184
|
+
return;
|
|
185
185
|
}
|
|
186
|
-
// 如果原来为未选中状态,需要选中的数量少于父组件中设置的max值,才可以选中
|
|
187
186
|
emitEvent();
|
|
188
|
-
emit('update:modelValue',
|
|
187
|
+
emit('update:modelValue', true);
|
|
189
188
|
}
|
|
190
189
|
}
|
|
191
190
|
|
|
192
|
-
|
|
191
|
+
// 监听父组件事件
|
|
192
|
+
const unsubscribeSetChecked = onParentEvent(childId, 'setChecked', data => {
|
|
193
|
+
if (!isDisabled.value) {
|
|
194
|
+
emit('update:modelValue', data.checked);
|
|
195
|
+
if (data.checked !== props.modelValue) {
|
|
196
|
+
emitEvent();
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
// 在适当的时候取消监听
|
|
202
|
+
onUnmounted(() => {
|
|
203
|
+
unsubscribeSetChecked();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
defineExpose({
|
|
207
|
+
isChecked: computed(() => props.modelValue),
|
|
208
|
+
name: props.name,
|
|
209
|
+
setValue,
|
|
210
|
+
emitEvent,
|
|
211
|
+
props
|
|
212
|
+
});
|
|
193
213
|
</script>
|
|
194
214
|
|
|
195
215
|
<style lang="scss" scoped>
|
|
@@ -222,7 +242,6 @@ defineExpose({ setValue, emitEvent });
|
|
|
222
242
|
transition-duration: 0.2s;
|
|
223
243
|
|
|
224
244
|
/* #ifdef MP-TOUTIAO */
|
|
225
|
-
// 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
|
|
226
245
|
&__icon {
|
|
227
246
|
line-height: 0;
|
|
228
247
|
}
|
|
@@ -8,7 +8,7 @@ import type { Shape } from '../../types/global';
|
|
|
8
8
|
|
|
9
9
|
export const CheckboxGroupProps = {
|
|
10
10
|
/** 最多能选中多少个checkbox */
|
|
11
|
-
max: { type:
|
|
11
|
+
max: { type: Number, default: 999 },
|
|
12
12
|
/** 是否禁用所有复选框 */
|
|
13
13
|
disabled: { type: Boolean, default: false },
|
|
14
14
|
/** 在表单内提交时的标识符 */
|
|
@@ -4,15 +4,24 @@
|
|
|
4
4
|
</view>
|
|
5
5
|
</template>
|
|
6
6
|
|
|
7
|
+
<script lang="ts">
|
|
8
|
+
export default {
|
|
9
|
+
name: 'u-checkbox-group',
|
|
10
|
+
options: {
|
|
11
|
+
addGlobalClass: true,
|
|
12
|
+
// #ifndef MP-TOUTIAO
|
|
13
|
+
virtualHost: true,
|
|
14
|
+
// #endif
|
|
15
|
+
styleIsolation: 'shared'
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
</script>
|
|
19
|
+
|
|
7
20
|
<script setup lang="ts">
|
|
8
|
-
import {
|
|
9
|
-
import { $u } from '../..';
|
|
21
|
+
import { getCurrentInstance, computed } from 'vue';
|
|
22
|
+
import { $u, useParent } from '../..';
|
|
10
23
|
import { CheckboxGroupProps } from './types';
|
|
11
24
|
|
|
12
|
-
defineOptions({
|
|
13
|
-
name: 'u-checkbox-group'
|
|
14
|
-
});
|
|
15
|
-
|
|
16
25
|
/**
|
|
17
26
|
* checkboxGroup 开关选择器父组件Group
|
|
18
27
|
* @description 复选框组件一般用于需要多个选择的场景,该组件功能完整,使用方便
|
|
@@ -29,22 +38,12 @@ defineOptions({
|
|
|
29
38
|
* @event {Function} change 任一个checkbox状态发生变化时触发,回调为一个对象
|
|
30
39
|
* @example <u-checkbox-group></u-checkbox-group>
|
|
31
40
|
*/
|
|
32
|
-
|
|
41
|
+
const instance = getCurrentInstance();
|
|
33
42
|
const props = defineProps(CheckboxGroupProps);
|
|
43
|
+
const emit = defineEmits(['update:modelValue', 'change']);
|
|
34
44
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// 复选框子项集合
|
|
38
|
-
const children = ref<any[]>([]); // 用于存储所有子checkbox实例
|
|
39
|
-
|
|
40
|
-
// 向子组件 provide 本 group 实例
|
|
41
|
-
provide('u-checkbox-group', {
|
|
42
|
-
props,
|
|
43
|
-
emitEvent,
|
|
44
|
-
children
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
const instance = getCurrentInstance();
|
|
45
|
+
// 使用父组件Hook
|
|
46
|
+
const { children, broadcast } = useParent('u-checkbox-group');
|
|
48
47
|
|
|
49
48
|
/**
|
|
50
49
|
* 派发 change 事件和表单校验
|
|
@@ -52,19 +51,71 @@ const instance = getCurrentInstance();
|
|
|
52
51
|
function emitEvent() {
|
|
53
52
|
// 收集所有选中的 name
|
|
54
53
|
let values: any[] = [];
|
|
55
|
-
children.
|
|
56
|
-
if (
|
|
54
|
+
children.forEach((child: any) => {
|
|
55
|
+
if (child.getExposed?.()?.isChecked.value) {
|
|
56
|
+
values.push(child.getExposed?.()?.name);
|
|
57
|
+
}
|
|
57
58
|
});
|
|
58
59
|
emit('change', values);
|
|
59
|
-
// 发出事件,用于在表单组件中嵌入checkbox的情况,进行验证
|
|
60
|
-
// 由于头条小程序执行迟钝,故需要用几十毫秒的延时
|
|
61
60
|
setTimeout(() => {
|
|
62
|
-
// 将当前的值发送到 u-form-item 进行校验
|
|
63
61
|
$u.dispatch(instance, 'u-form-item', 'on-form-change', values);
|
|
64
62
|
}, 60);
|
|
65
63
|
}
|
|
66
64
|
|
|
67
|
-
|
|
65
|
+
/**
|
|
66
|
+
* 全选/全不选方法
|
|
67
|
+
*/
|
|
68
|
+
function setAllChecked(checked: boolean) {
|
|
69
|
+
if (props.disabled) {
|
|
70
|
+
console.warn('u-checkbox-group已禁用,无法操作');
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
broadcast('setChecked', { checked });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 获取选中的值
|
|
78
|
+
*/
|
|
79
|
+
function getSelectedValues() {
|
|
80
|
+
return children
|
|
81
|
+
.filter(child => child.getExposed?.()?.isChecked.value)
|
|
82
|
+
.map(child => child.getExposed?.()?.name)
|
|
83
|
+
.filter(Boolean);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 验证选择是否超过最大数量
|
|
88
|
+
*/
|
|
89
|
+
function validateSelection() {
|
|
90
|
+
const selectedCount = children.filter(child => child.getExposed?.()?.isChecked.value).length;
|
|
91
|
+
if (props.max && selectedCount >= props.max) {
|
|
92
|
+
$u.toast(`超过最大选择数量: ${props.max}`);
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 使用defineExpose暴露给外部
|
|
99
|
+
defineExpose({
|
|
100
|
+
// props
|
|
101
|
+
props,
|
|
102
|
+
|
|
103
|
+
// 方法
|
|
104
|
+
emitEvent,
|
|
105
|
+
setAllChecked,
|
|
106
|
+
getSelectedValues,
|
|
107
|
+
validateSelection,
|
|
108
|
+
|
|
109
|
+
// 计算属性
|
|
110
|
+
selectedCount: computed(() => children.filter(child => child.getExposed?.()?.isChecked.value).length),
|
|
111
|
+
isFull: computed(() => {
|
|
112
|
+
const selectedCount = children.filter(child => child.getExposed?.()?.isChecked.value).length;
|
|
113
|
+
return props.max && selectedCount >= props.max;
|
|
114
|
+
}),
|
|
115
|
+
isEmpty: computed(() => children.filter(child => child.getExposed?.()?.isChecked.value).length === 0),
|
|
116
|
+
// 工具方法
|
|
117
|
+
getChildrenCount: () => children.length
|
|
118
|
+
});
|
|
68
119
|
</script>
|
|
69
120
|
|
|
70
121
|
<style lang="scss" scoped>
|
|
@@ -9,16 +9,18 @@ export default {
|
|
|
9
9
|
name: 'u-collapse',
|
|
10
10
|
options: {
|
|
11
11
|
addGlobalClass: true,
|
|
12
|
+
// #ifndef MP-TOUTIAO
|
|
12
13
|
virtualHost: true,
|
|
14
|
+
// #endif
|
|
13
15
|
styleIsolation: 'shared'
|
|
14
16
|
}
|
|
15
17
|
};
|
|
16
18
|
</script>
|
|
17
19
|
|
|
18
20
|
<script setup lang="ts">
|
|
19
|
-
import { ref } from 'vue';
|
|
21
|
+
import { nextTick, onMounted, ref } from 'vue';
|
|
20
22
|
import { CollapseProps } from './types';
|
|
21
|
-
import { $u } from '../../';
|
|
23
|
+
import { $u, useParent } from '../../';
|
|
22
24
|
|
|
23
25
|
/**
|
|
24
26
|
* collapse 手风琴
|
|
@@ -34,33 +36,157 @@ import { $u } from '../../';
|
|
|
34
36
|
* @example <u-collapse></u-collapse>
|
|
35
37
|
*/
|
|
36
38
|
const props = defineProps(CollapseProps);
|
|
37
|
-
|
|
38
39
|
const emit = defineEmits(['change']);
|
|
39
40
|
|
|
40
|
-
//
|
|
41
|
-
const children =
|
|
41
|
+
// 使用通信库的父组件Hook
|
|
42
|
+
const { children, broadcast, getChildrenExposed } = useParent('u-collapse');
|
|
43
|
+
|
|
44
|
+
// 当前激活的面板 - 只在手风琴模式下使用
|
|
45
|
+
const activeName = ref<string | number>('');
|
|
46
|
+
|
|
47
|
+
onMounted(() => {
|
|
48
|
+
nextTick(() => {
|
|
49
|
+
// 初始化:收集所有open为true的项
|
|
50
|
+
setTimeout(() => {
|
|
51
|
+
initializeActiveName();
|
|
52
|
+
}, 100);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
42
55
|
|
|
43
56
|
/**
|
|
44
|
-
*
|
|
57
|
+
* 初始化activeName - 找到第一个open为true的项
|
|
45
58
|
*/
|
|
46
|
-
function
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
59
|
+
function initializeActiveName() {
|
|
60
|
+
if (props.accordion) {
|
|
61
|
+
// 手风琴模式下,取第一个open为true的项作为初始激活项
|
|
62
|
+
const childrenExposed = getChildrenExposed();
|
|
63
|
+
const openChild = childrenExposed.find(child => child.exposed.isShow === true);
|
|
64
|
+
if (openChild) {
|
|
65
|
+
activeName.value = openChild.exposed.itemName || '';
|
|
66
|
+
}
|
|
67
|
+
}
|
|
50
68
|
}
|
|
51
69
|
|
|
52
70
|
/**
|
|
53
71
|
* collapse item被点击,由collapse item调用父组件方法
|
|
54
72
|
*/
|
|
55
|
-
function onChange(
|
|
56
|
-
|
|
73
|
+
function onChange(name: string | number) {
|
|
74
|
+
if (props.accordion) {
|
|
75
|
+
// 手风琴模式
|
|
76
|
+
const childrenExposed = getChildrenExposed();
|
|
77
|
+
const targetChild = childrenExposed.find(child => child.exposed.itemName === name);
|
|
78
|
+
if (targetChild?.exposed.isShow.value === true) {
|
|
79
|
+
// 目标项当前是展开状态,点击后要关闭它
|
|
80
|
+
activeName.value = '';
|
|
81
|
+
broadcast('closeAll', {});
|
|
82
|
+
} else {
|
|
83
|
+
// 目标项当前是关闭状态,点击后要展开它并关闭其他
|
|
84
|
+
activeName.value = name;
|
|
85
|
+
broadcast('openSingle', { targetName: name });
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
// 非手风琴模式 - 只通知目标项切换状态
|
|
89
|
+
broadcast('toggleSingle', { targetName: name });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 收集当前所有展开的项
|
|
93
|
+
let currentActiveNames: (string | number)[] = [];
|
|
94
|
+
if (props.accordion) {
|
|
95
|
+
currentActiveNames = activeName.value ? [activeName.value] : [];
|
|
96
|
+
} else {
|
|
97
|
+
// 对于非手风琴模式,我们不知道所有项的状态,所以这里不处理
|
|
98
|
+
currentActiveNames = [];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
emit('change', props.accordion ? activeName.value || '' : currentActiveNames);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 设置激活的面板
|
|
106
|
+
*/
|
|
107
|
+
function setActiveNames(names: string | number | (string | number)[]) {
|
|
108
|
+
if (props.accordion) {
|
|
109
|
+
// 手风琴模式
|
|
110
|
+
const name = Array.isArray(names) ? names[0] : names;
|
|
111
|
+
activeName.value = name || '';
|
|
112
|
+
if (name) {
|
|
113
|
+
broadcast('openSingle', { targetName: name });
|
|
114
|
+
} else {
|
|
115
|
+
broadcast('closeAll', {});
|
|
116
|
+
}
|
|
117
|
+
} else {
|
|
118
|
+
// 非手风琴模式
|
|
119
|
+
const namesArray = Array.isArray(names) ? names : [names];
|
|
120
|
+
broadcast('setMultiple', { targetNames: namesArray });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 打开所有面板
|
|
126
|
+
*/
|
|
127
|
+
function openAll() {
|
|
128
|
+
if (props.accordion) {
|
|
129
|
+
console.warn('手风琴模式下不能打开所有面板');
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const childrenExposed = getChildrenExposed();
|
|
133
|
+
const allNames = childrenExposed.map(child => child.exposed.itemName).filter(Boolean);
|
|
134
|
+
broadcast('setMultiple', { targetNames: allNames });
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 关闭所有面板
|
|
139
|
+
*/
|
|
140
|
+
function closeAll() {
|
|
141
|
+
broadcast('closeAll', {});
|
|
142
|
+
if (props.accordion) {
|
|
143
|
+
activeName.value = '';
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 重新初始化,用于动态内容变化
|
|
149
|
+
*/
|
|
150
|
+
function init() {
|
|
151
|
+
const childrenExposed = getChildrenExposed();
|
|
152
|
+
childrenExposed.forEach(child => {
|
|
153
|
+
if (child.exposed.init) {
|
|
154
|
+
child.exposed.init();
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// 重新初始化activeName
|
|
159
|
+
setTimeout(() => {
|
|
160
|
+
initializeActiveName();
|
|
161
|
+
}, 150);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// 热更新处理 - 重新连接所有子组件
|
|
165
|
+
if (import.meta.hot) {
|
|
166
|
+
import.meta.hot.accept(() => {
|
|
167
|
+
setTimeout(() => {
|
|
168
|
+
broadcast('reconnect', {});
|
|
169
|
+
}, 100);
|
|
170
|
+
});
|
|
57
171
|
}
|
|
58
172
|
|
|
173
|
+
// 使用defineExpose暴露给外部
|
|
59
174
|
defineExpose({
|
|
175
|
+
// props
|
|
60
176
|
props,
|
|
61
|
-
|
|
177
|
+
|
|
178
|
+
// 状态
|
|
179
|
+
activeName,
|
|
180
|
+
|
|
181
|
+
// 方法
|
|
182
|
+
onChange,
|
|
183
|
+
setActiveNames,
|
|
184
|
+
openAll,
|
|
185
|
+
closeAll,
|
|
62
186
|
init,
|
|
63
|
-
|
|
187
|
+
|
|
188
|
+
// 计算属性
|
|
189
|
+
childrenCount: () => children.length
|
|
64
190
|
});
|
|
65
191
|
</script>
|
|
66
192
|
|