uview-pro 0.0.1

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.
Files changed (149) hide show
  1. package/changelog.md +6 -0
  2. package/components/u-action-sheet/u-action-sheet.vue +205 -0
  3. package/components/u-alert-tips/u-alert-tips.vue +241 -0
  4. package/components/u-avatar/u-avatar.vue +220 -0
  5. package/components/u-avatar-cropper/u-avatar-cropper.vue +329 -0
  6. package/components/u-avatar-cropper/weCropper.d.ts +54 -0
  7. package/components/u-avatar-cropper/weCropper.js +1267 -0
  8. package/components/u-avatar-cropper/weCropper.ts +1254 -0
  9. package/components/u-back-top/u-back-top.vue +156 -0
  10. package/components/u-badge/u-badge.vue +189 -0
  11. package/components/u-button/u-button.vue +562 -0
  12. package/components/u-calendar/u-calendar.vue +725 -0
  13. package/components/u-car-keyboard/u-car-keyboard.vue +236 -0
  14. package/components/u-card/u-card.vue +240 -0
  15. package/components/u-cell-group/u-cell-group.vue +56 -0
  16. package/components/u-cell-item/u-cell-item.vue +245 -0
  17. package/components/u-checkbox/u-checkbox.vue +310 -0
  18. package/components/u-checkbox-group/u-checkbox-group.vue +134 -0
  19. package/components/u-circle-progress/u-circle-progress.vue +210 -0
  20. package/components/u-col/u-col.vue +135 -0
  21. package/components/u-collapse/u-collapse.vue +82 -0
  22. package/components/u-collapse-item/u-collapse-item.vue +190 -0
  23. package/components/u-column-notice/u-column-notice.vue +264 -0
  24. package/components/u-count-down/u-count-down.vue +333 -0
  25. package/components/u-count-to/u-count-to.vue +297 -0
  26. package/components/u-divider/u-divider.vue +141 -0
  27. package/components/u-dropdown/u-dropdown.vue +311 -0
  28. package/components/u-dropdown-item/u-dropdown-item.vue +135 -0
  29. package/components/u-empty/u-empty.vue +111 -0
  30. package/components/u-field/u-field.vue +469 -0
  31. package/components/u-form/u-form.vue +162 -0
  32. package/components/u-form-item/u-form-item.vue +476 -0
  33. package/components/u-full-screen/u-full-screen.vue +80 -0
  34. package/components/u-gap/u-gap.vue +48 -0
  35. package/components/u-grid/u-grid.vue +101 -0
  36. package/components/u-grid-item/u-grid-item.vue +136 -0
  37. package/components/u-icon/u-icon.vue +389 -0
  38. package/components/u-image/types.ts +48 -0
  39. package/components/u-image/u-image.vue +218 -0
  40. package/components/u-index-anchor/u-index-anchor.vue +101 -0
  41. package/components/u-index-list/u-index-list.vue +376 -0
  42. package/components/u-input/u-input.vue +462 -0
  43. package/components/u-keyboard/u-keyboard.vue +188 -0
  44. package/components/u-lazy-load/u-lazy-load.vue +288 -0
  45. package/components/u-line/u-line.vue +71 -0
  46. package/components/u-line-progress/u-line-progress.vue +128 -0
  47. package/components/u-link/u-link.vue +87 -0
  48. package/components/u-loading/u-loading.vue +111 -0
  49. package/components/u-loadmore/u-loadmore.vue +205 -0
  50. package/components/u-mask/u-mask.vue +137 -0
  51. package/components/u-message-input/u-message-input.vue +315 -0
  52. package/components/u-modal/u-modal.vue +284 -0
  53. package/components/u-navbar/u-navbar.vue +314 -0
  54. package/components/u-no-network/image.ts +2 -0
  55. package/components/u-no-network/u-no-network.vue +311 -0
  56. package/components/u-notice-bar/u-notice-bar.vue +274 -0
  57. package/components/u-number-box/u-number-box.vue +344 -0
  58. package/components/u-number-keyboard/u-number-keyboard.vue +170 -0
  59. package/components/u-parse/libs/CssHandler.js +100 -0
  60. package/components/u-parse/libs/MpHtmlParser.js +580 -0
  61. package/components/u-parse/libs/config.js +80 -0
  62. package/components/u-parse/libs/handler.wxs +22 -0
  63. package/components/u-parse/libs/trees.vue +505 -0
  64. package/components/u-parse/u-parse.vue +645 -0
  65. package/components/u-picker/u-picker.vue +808 -0
  66. package/components/u-popup/u-popup.vue +404 -0
  67. package/components/u-radio/u-radio.vue +272 -0
  68. package/components/u-radio-group/u-radio-group.vue +116 -0
  69. package/components/u-rate/u-rate.vue +349 -0
  70. package/components/u-read-more/u-read-more.vue +199 -0
  71. package/components/u-row/u-row.vue +95 -0
  72. package/components/u-row-notice/u-row-notice.vue +273 -0
  73. package/components/u-search/u-search.vue +298 -0
  74. package/components/u-section/u-section.vue +175 -0
  75. package/components/u-select/u-select.vue +387 -0
  76. package/components/u-skeleton/u-skeleton.vue +230 -0
  77. package/components/u-slider/u-slider.vue +293 -0
  78. package/components/u-steps/u-steps.vue +200 -0
  79. package/components/u-sticky/u-sticky.vue +189 -0
  80. package/components/u-subsection/u-subsection.vue +388 -0
  81. package/components/u-swipe-action/u-swipe-action.vue +289 -0
  82. package/components/u-swiper/u-swiper.vue +305 -0
  83. package/components/u-switch/u-switch.vue +146 -0
  84. package/components/u-tabbar/u-tabbar.vue +347 -0
  85. package/components/u-table/u-table.vue +104 -0
  86. package/components/u-tabs/u-tabs.vue +322 -0
  87. package/components/u-tabs-swiper/u-tabs-swiper.vue +426 -0
  88. package/components/u-tag/u-tag.vue +270 -0
  89. package/components/u-td/u-td.vue +76 -0
  90. package/components/u-th/u-th.vue +70 -0
  91. package/components/u-time-line/u-time-line.vue +39 -0
  92. package/components/u-time-line-item/u-time-line-item.vue +88 -0
  93. package/components/u-toast/types.ts +4 -0
  94. package/components/u-toast/u-toast.vue +238 -0
  95. package/components/u-top-tips/u-top-tips.vue +118 -0
  96. package/components/u-tr/u-tr.vue +24 -0
  97. package/components/u-upload/u-upload.vue +600 -0
  98. package/components/u-verification-code/u-verification-code.vue +194 -0
  99. package/components/u-waterfall/u-waterfall.vue +186 -0
  100. package/iconfont.css +910 -0
  101. package/index.scss +23 -0
  102. package/index.ts +166 -0
  103. package/libs/config/config.ts +26 -0
  104. package/libs/config/zIndex.ts +37 -0
  105. package/libs/css/color.scss +155 -0
  106. package/libs/css/common.scss +176 -0
  107. package/libs/css/style.components.scss +7 -0
  108. package/libs/css/style.h5.scss +8 -0
  109. package/libs/css/style.mp.scss +72 -0
  110. package/libs/css/style.nvue.scss +3 -0
  111. package/libs/css/style.vue.scss +175 -0
  112. package/libs/function/$parent.ts +22 -0
  113. package/libs/function/addUnit.ts +13 -0
  114. package/libs/function/color.ts +37 -0
  115. package/libs/function/colorGradient.ts +123 -0
  116. package/libs/function/debounce.ts +28 -0
  117. package/libs/function/deepClone.ts +39 -0
  118. package/libs/function/deepMerge.ts +34 -0
  119. package/libs/function/getParent.ts +59 -0
  120. package/libs/function/getRect.ts +26 -0
  121. package/libs/function/guid.ts +42 -0
  122. package/libs/function/md5.ts +397 -0
  123. package/libs/function/parent.ts +21 -0
  124. package/libs/function/queryParams.ts +60 -0
  125. package/libs/function/random.ts +16 -0
  126. package/libs/function/randomArray.ts +11 -0
  127. package/libs/function/route.ts +118 -0
  128. package/libs/function/sys.ts +15 -0
  129. package/libs/function/test.ts +229 -0
  130. package/libs/function/throttle.ts +31 -0
  131. package/libs/function/timeFormat.ts +54 -0
  132. package/libs/function/timeFrom.ts +48 -0
  133. package/libs/function/toast.ts +14 -0
  134. package/libs/function/trim.ts +21 -0
  135. package/libs/function/type2icon.ts +36 -0
  136. package/libs/hooks/useEmitter.ts +77 -0
  137. package/libs/hooks/useParent.ts +29 -0
  138. package/libs/request/index.ts +237 -0
  139. package/libs/store/index.ts +88 -0
  140. package/libs/util/area.ts +1 -0
  141. package/libs/util/async-validator.js +1356 -0
  142. package/libs/util/city.ts +1 -0
  143. package/libs/util/emitter.ts +112 -0
  144. package/libs/util/mitt.ts +118 -0
  145. package/libs/util/parent.ts +20 -0
  146. package/libs/util/province.ts +1 -0
  147. package/package.json +98 -0
  148. package/readme.md +165 -0
  149. package/theme.scss +38 -0
@@ -0,0 +1,245 @@
1
+ <template>
2
+ <view
3
+ @tap="onClick"
4
+ class="u-cell"
5
+ :class="{
6
+ 'u-border-bottom': borderBottom,
7
+ 'u-border-top': borderTop,
8
+ 'u-col-center': center,
9
+ 'u-cell--required': required
10
+ }"
11
+ hover-stay-time="150"
12
+ :hover-class="hoverClass"
13
+ :style="{ backgroundColor: bgColor }"
14
+ >
15
+ <u-icon :size="iconSize" :name="icon" v-if="icon" :custom-style="iconStyle" class="u-cell__left-icon-wrap"></u-icon>
16
+ <view class="u-flex" v-else>
17
+ <slot name="icon"></slot>
18
+ </view>
19
+ <view class="u-cell_title" :style="[{ width: titleWidth ? titleWidth + 'rpx' : 'auto' }, titleStyle]">
20
+ <template v-if="title !== ''">{{ title }}</template>
21
+ <slot name="title" v-else></slot>
22
+ <view class="u-cell__label" v-if="label || $slots.label" :style="[labelStyle]">
23
+ <template v-if="label !== ''">{{ label }}</template>
24
+ <slot name="label" v-else></slot>
25
+ </view>
26
+ </view>
27
+ <view class="u-cell__value" :style="[valueStyle]">
28
+ <template v-if="value !== ''">{{ value }}</template>
29
+ <slot v-else></slot>
30
+ </view>
31
+ <view class="u-flex u-cell_right" v-if="$slots['right-icon']">
32
+ <slot name="right-icon"></slot>
33
+ </view>
34
+ <u-icon v-if="arrow" name="arrow-right" :style="[arrowStyle]" class="u-icon-wrap u-cell__right-icon-wrap"></u-icon>
35
+ </view>
36
+ </template>
37
+
38
+ <script setup lang="ts">
39
+ import { computed, useSlots } from 'vue';
40
+
41
+ defineOptions({
42
+ name: 'u-cell-item'
43
+ });
44
+
45
+ /**
46
+ * cellItem 单元格Item
47
+ * @description cell单元格一般用于一组列表的情况,比如个人中心页,设置页等。搭配u-cell-group使用
48
+ * @tutorial https://www.uviewui.com/components/cell.html
49
+ * @property {String} title 左侧标题
50
+ * @property {String} icon 左侧图标名,只支持uView内置图标,见Icon 图标
51
+ * @property {Object} icon-style 左边图标的样式,对象形式
52
+ * @property {String} value 右侧内容
53
+ * @property {String} label 标题下方的描述信息
54
+ * @property {Boolean} border-bottom 是否显示cell的下边框(默认true)
55
+ * @property {Boolean} border-top 是否显示cell的上边框(默认false)
56
+ * @property {Boolean} center 是否使内容垂直居中(默认false)
57
+ * @property {String} hover-class 是否开启点击反馈,none为无效果(默认true)
58
+ * // @property {Boolean} border-gap border-bottom为true时,Cell列表中间的条目的下边框是否与左边有一个间隔(默认true)
59
+ * @property {Boolean} arrow 是否显示右侧箭头(默认true)
60
+ * @property {Boolean} required 箭头方向,可选值(默认right)
61
+ * @property {Boolean} arrow-direction 是否显示左边表示必填的星号(默认false)
62
+ * @property {Object} title-style 标题样式,对象形式
63
+ * @property {Object} value-style 右侧内容样式,对象形式
64
+ * @property {Object} label-style 标题下方描述信息的样式,对象形式
65
+ * @property {String} bg-color 背景颜色(默认transparent)
66
+ * @property {String|Number} index 用于在click事件回调中返回,标识当前是第几个Item
67
+ * @property {String|Number} title-width 标题的宽度,单位rpx
68
+ * @example <u-cell-item icon="integral-fill" title="会员等级" value="新版本"></u-cell-item>
69
+ */
70
+
71
+ const emit = defineEmits<{ (e: 'click', index: string | number): void }>();
72
+
73
+ const props = defineProps({
74
+ /** 左侧图标名称(只能uView内置图标),或者图标src */
75
+ icon: { type: String, default: '' },
76
+ /** 左侧标题 */
77
+ title: { type: [String, Number], default: '' },
78
+ /** 右侧内容 */
79
+ value: { type: [String, Number], default: '' },
80
+ /** 标题下方的描述信息 */
81
+ label: { type: [String, Number], default: '' },
82
+ /** 是否显示下边框 */
83
+ borderBottom: { type: Boolean, default: true },
84
+ /** 是否显示上边框 */
85
+ borderTop: { type: Boolean, default: false },
86
+ /**多个cell中,中间的cell显示下划线时,下划线是否给一个到左边的距离 */
87
+ /* 1.4.0版本废除此参数,默认边框由border-top和border-bottom提供,此参数会造成干扰 */
88
+ // borderGap: { type: Boolean, default: true },
89
+ /** 是否开启点击反馈,即点击时cell背景为灰色,none为无效果 */
90
+ hoverClass: { type: String, default: 'u-cell-hover' },
91
+ /** 是否显示右侧箭头 */
92
+ arrow: { type: Boolean, default: true },
93
+ /** 内容是否垂直居中 */
94
+ center: { type: Boolean, default: false },
95
+ /** 是否显示左边表示必填的星号 */
96
+ required: { type: Boolean, default: false },
97
+ /** 标题的宽度,单位rpx */
98
+ titleWidth: { type: [Number, String], default: '' },
99
+ /** 右侧箭头方向,可选值:right|up|down,默认为right */
100
+ arrowDirection: { type: String, default: 'right' },
101
+ /** 控制标题的样式 */
102
+ titleStyle: { type: Object as () => Record<string, any>, default: () => ({}) },
103
+ /** 右侧显示内容的样式 */
104
+ valueStyle: { type: Object as () => Record<string, any>, default: () => ({}) },
105
+ /** 描述信息的样式 */
106
+ labelStyle: { type: Object as () => Record<string, any>, default: () => ({}) },
107
+ /** 背景颜色 */
108
+ bgColor: { type: String, default: 'transparent' },
109
+ /** 用于识别被点击的是第几个cell */
110
+ index: { type: [String, Number], default: '' },
111
+ /** 是否使用label插槽 */
112
+ useLabelSlot: { type: Boolean, default: false },
113
+ /** 左边图标的大小,单位rpx,只对传入icon字段时有效 */
114
+ iconSize: { type: [Number, String], default: 34 },
115
+ /** 左边图标的样式,对象形式 */
116
+ iconStyle: { type: Object as () => Record<string, any>, default: () => ({}) }
117
+ });
118
+
119
+ const $slots = useSlots();
120
+
121
+ /**
122
+ * 箭头样式
123
+ */
124
+ const arrowStyle = computed(() => {
125
+ let style: Record<string, any> = {};
126
+ if (props.arrowDirection === 'up') style.transform = 'rotate(-90deg)';
127
+ else if (props.arrowDirection === 'down') style.transform = 'rotate(90deg)';
128
+ else style.transform = 'rotate(0deg)';
129
+ return style;
130
+ });
131
+
132
+ /**
133
+ * 点击事件
134
+ */
135
+ function onClick() {
136
+ emit('click', props.index);
137
+ }
138
+ </script>
139
+
140
+ <style lang="scss" scoped>
141
+ @import '../../libs/css/style.components.scss';
142
+ .u-cell {
143
+ @include vue-flex;
144
+ align-items: center;
145
+ position: relative;
146
+ /* #ifndef APP-NVUE */
147
+ box-sizing: border-box;
148
+ /* #endif */
149
+ width: 100%;
150
+ padding: 26rpx 32rpx;
151
+ font-size: 28rpx;
152
+ line-height: 54rpx;
153
+ color: $u-content-color;
154
+ background-color: #fff;
155
+ text-align: left;
156
+ }
157
+
158
+ .u-cell_title {
159
+ font-size: 28rpx;
160
+ }
161
+
162
+ .u-cell__left-icon-wrap {
163
+ margin-right: 10rpx;
164
+ font-size: 32rpx;
165
+ }
166
+
167
+ .u-cell__right-icon-wrap {
168
+ margin-left: 10rpx;
169
+ color: #969799;
170
+ font-size: 28rpx;
171
+ }
172
+
173
+ .u-cell__left-icon-wrap,
174
+ .u-cell__right-icon-wrap {
175
+ @include vue-flex;
176
+ align-items: center;
177
+ height: 48rpx;
178
+ }
179
+
180
+ .u-cell-border:after {
181
+ position: absolute;
182
+ /* #ifndef APP-NVUE */
183
+ box-sizing: border-box;
184
+ content: ' ';
185
+ pointer-events: none;
186
+ border-bottom: 1px solid $u-border-color;
187
+ /* #endif */
188
+ right: 0;
189
+ left: 0;
190
+ top: 0;
191
+ transform: scaleY(0.5);
192
+ }
193
+
194
+ .u-cell-border {
195
+ position: relative;
196
+ }
197
+
198
+ .u-cell__label {
199
+ margin-top: 6rpx;
200
+ font-size: 26rpx;
201
+ line-height: 36rpx;
202
+ color: $u-tips-color;
203
+ /* #ifndef APP-NVUE */
204
+ word-wrap: break-word;
205
+ /* #endif */
206
+ }
207
+
208
+ .u-cell__value {
209
+ overflow: hidden;
210
+ text-align: right;
211
+ /* #ifndef APP-NVUE */
212
+ vertical-align: middle;
213
+ /* #endif */
214
+ color: $u-tips-color;
215
+ font-size: 26rpx;
216
+ }
217
+
218
+ .u-cell__title,
219
+ .u-cell__value {
220
+ flex: 1;
221
+ }
222
+
223
+ .u-cell--required {
224
+ /* #ifndef APP-NVUE */
225
+ overflow: visible;
226
+ /* #endif */
227
+ @include vue-flex;
228
+ align-items: center;
229
+ }
230
+
231
+ .u-cell--required:before {
232
+ position: absolute;
233
+ /* #ifndef APP-NVUE */
234
+ content: '*';
235
+ /* #endif */
236
+ left: 8px;
237
+ margin-top: 4rpx;
238
+ font-size: 14px;
239
+ color: $u-type-error;
240
+ }
241
+
242
+ .u-cell_right {
243
+ line-height: 1;
244
+ }
245
+ </style>
@@ -0,0 +1,310 @@
1
+ <template>
2
+ <view class="u-checkbox" :style="[checkboxStyle]">
3
+ <view class="u-checkbox__icon-wrap" @tap="toggle" :class="[iconClass]" :style="[iconStyle]">
4
+ <u-icon class="u-checkbox__icon-wrap__icon" name="checkbox-mark" :size="checkboxIconSize" :color="iconColor" />
5
+ </view>
6
+ <view
7
+ class="u-checkbox__label"
8
+ @tap="onClickLabel"
9
+ :style="{
10
+ fontSize: $u.addUnit(labelSize)
11
+ }"
12
+ >
13
+ <slot />
14
+ </view>
15
+ </view>
16
+ </template>
17
+
18
+ <script setup lang="ts">
19
+ import { computed, inject, getCurrentInstance, onMounted } from 'vue';
20
+ import { $u } from '../..';
21
+
22
+ defineOptions({
23
+ name: 'u-checkbox'
24
+ });
25
+
26
+ /**
27
+ * checkbox 复选框
28
+ * @description 该组件需要搭配checkboxGroup组件使用,以便用户进行操作时,获得当前复选框组的选中情况。
29
+ * @tutorial https://www.uviewui.com/components/checkbox.html
30
+ * @property {String Number} icon-size 图标大小,单位rpx(默认20)
31
+ * @property {String Number} label-size label字体大小,单位rpx(默认28)
32
+ * @property {String Number} name checkbox组件的标示符
33
+ * @property {String} shape 形状,见官网说明(默认circle)
34
+ * @property {Boolean} disabled 是否禁用
35
+ * @property {Boolean} label-disabled 是否禁止点击文本操作checkbox
36
+ * @property {String} active-color 选中时的颜色,如设置CheckboxGroup的active-color将失效
37
+ * @event {Function} change 某个checkbox状态发生变化时触发,回调为一个对象
38
+ * @example <u-checkbox v-model="checked" :disabled="false">天涯</u-checkbox>
39
+ */
40
+
41
+ const props = defineProps({
42
+ /** checkbox的名称 */
43
+ name: {
44
+ type: [String, Number],
45
+ default: ''
46
+ },
47
+ /** 形状,square为方形,circle为原型 */
48
+ shape: {
49
+ type: String,
50
+ default: ''
51
+ },
52
+ /** 是否为选中状态 */
53
+ modelValue: {
54
+ type: Boolean,
55
+ default: false
56
+ },
57
+ /** 是否禁用 */
58
+ disabled: {
59
+ type: [String, Boolean],
60
+ default: ''
61
+ },
62
+ /** 是否禁止点击提示语选中复选框 */
63
+ labelDisabled: {
64
+ type: [String, Boolean],
65
+ default: ''
66
+ },
67
+ /** 选中状态下的颜色,如设置此值,将会覆盖checkboxGroup的activeColor值 */
68
+ activeColor: {
69
+ type: String,
70
+ default: ''
71
+ },
72
+ /** 图标的大小,单位rpx */
73
+ iconSize: {
74
+ type: [String, Number],
75
+ default: ''
76
+ },
77
+ /** label的字体大小,rpx单位 */
78
+ labelSize: {
79
+ type: [String, Number],
80
+ default: ''
81
+ },
82
+ /** 组件的整体大小 */
83
+ size: {
84
+ type: [String, Number],
85
+ default: ''
86
+ }
87
+ });
88
+
89
+ const emit = defineEmits(['change', 'update:modelValue']);
90
+
91
+ // 父组件 group 注入
92
+ // 支付宝小程序不支持provide/inject?
93
+ const parent = inject<any>('u-checkbox-group', null);
94
+
95
+ // 组件注册到 group
96
+ onMounted(() => {
97
+ // 如果存在u-checkbox-group,将本组件的实例塞进父组件的children中
98
+ if (parent && parent.children && !parent.children.value.includes(instanceProxy)) {
99
+ parent.children.value.push(instanceProxy);
100
+ }
101
+ });
102
+
103
+ const instance = getCurrentInstance();
104
+ const instanceProxy = instance?.proxy;
105
+
106
+ // 是否禁用,如果父组件u-checkbox-group禁用的话,将会忽略子组件的配置
107
+ const isDisabled = computed(() => {
108
+ return props.disabled !== '' ? props.disabled : parent ? parent.props.disabled : false;
109
+ });
110
+ // 是否禁用label点击
111
+ const isLabelDisabled = computed(() => {
112
+ return props.labelDisabled !== '' ? props.labelDisabled : parent ? parent.props.labelDisabled : false;
113
+ });
114
+ // 组件尺寸,对应size的值,默认值为34rpx
115
+ const checkboxSize = computed(() => {
116
+ return props.size ? props.size : parent ? parent.props.size : 34;
117
+ });
118
+ // 组件的勾选图标的尺寸,默认20
119
+ const checkboxIconSize = computed(() => {
120
+ return props.iconSize ? props.iconSize : parent ? parent.props.iconSize : 20;
121
+ });
122
+ // 组件选中激活时的颜色
123
+ const elActiveColor = computed(() => {
124
+ return props.activeColor ? props.activeColor : parent ? parent.props.activeColor : 'primary';
125
+ });
126
+ // 组件的形状
127
+ const elShape = computed(() => {
128
+ return props.shape ? props.shape : parent ? parent.props.shape : 'square';
129
+ });
130
+ // 图标样式
131
+ const iconStyle = computed(() => {
132
+ let style: Record<string, string> = {};
133
+ // 既要判断是否手动禁用,还要判断用户v-model绑定的值,如果绑定为false,那么也无法选中
134
+ if (elActiveColor.value && props.modelValue && !isDisabled.value) {
135
+ style.borderColor = elActiveColor.value;
136
+ style.backgroundColor = elActiveColor.value;
137
+ }
138
+ style.width = $u.addUnit(checkboxSize.value);
139
+ style.height = $u.addUnit(checkboxSize.value);
140
+ return style;
141
+ });
142
+ // checkbox内部的勾选图标,如果选中状态,为白色,否则为透明色即可
143
+ const iconColor = computed(() => {
144
+ return props.modelValue ? '#ffffff' : 'transparent';
145
+ });
146
+ const iconClass = computed(() => {
147
+ let classes: string[] = [];
148
+ classes.push('u-checkbox__icon-wrap--' + elShape.value);
149
+ if (props.modelValue == true) classes.push('u-checkbox__icon-wrap--checked');
150
+ if (isDisabled.value) classes.push('u-checkbox__icon-wrap--disabled');
151
+ if (props.modelValue && isDisabled.value) classes.push('u-checkbox__icon-wrap--disabled--checked');
152
+ // 支付宝小程序无法动态绑定一个数组类名,否则解析出来的结果会带有",",而导致失效
153
+ return classes.join(' ');
154
+ });
155
+ const checkboxStyle = computed(() => {
156
+ let style: Record<string, string> = {};
157
+ if (parent && parent.props.width) {
158
+ style.width = parent.props.width;
159
+ // #ifdef MP
160
+ // 各家小程序因为它们特殊的编译结构,使用float布局
161
+ style.float = 'left';
162
+ // #endif
163
+ // #ifndef MP
164
+ // H5和APP使用flex布局
165
+ style.flex = `0 0 ${parent.props.width}`;
166
+ // #endif
167
+ }
168
+ if (parent && parent.props.wrap) {
169
+ style.width = '100%';
170
+ // #ifndef MP
171
+ // H5和APP使用flex布局,将宽度设置100%,即可自动换行
172
+ style.flex = '0 0 100%';
173
+ // #endif
174
+ }
175
+ return style;
176
+ });
177
+
178
+ /**
179
+ * 点击label
180
+ */
181
+ function onClickLabel() {
182
+ if (!isLabelDisabled.value && !isDisabled.value) {
183
+ setValue();
184
+ }
185
+ }
186
+ /**
187
+ * 点击icon
188
+ */
189
+ function toggle() {
190
+ if (!isDisabled.value) {
191
+ setValue();
192
+ }
193
+ }
194
+ /**
195
+ * 触发change事件
196
+ */
197
+ function emitEvent() {
198
+ emit('change', {
199
+ value: !props.modelValue,
200
+ name: props.name
201
+ });
202
+ // 执行父组件u-checkbox-group的事件方法
203
+ // 等待下一个周期再执行,因为emit('input')作用于父组件,再反馈到子组件内部,需要时间
204
+ setTimeout(() => {
205
+ if (parent && parent.children && parent.children.value && parent.children.value.includes(instanceProxy) && parent.emitEvent) parent.emitEvent();
206
+ }, 80);
207
+ }
208
+ /**
209
+ * 设置input的值,这里通过input事件,设置通过v-model绑定的组件的值
210
+ */
211
+ function setValue() {
212
+ // 判断是否超过了可选的最大数量
213
+ let checkedNum = 0;
214
+ if (parent && parent.children && parent.children.value) {
215
+ // 只要父组件的某一个子元素的value为true,就加1(已有的选中数量)
216
+ parent.children.value.forEach((val: any) => {
217
+ if (val.value) checkedNum++;
218
+ });
219
+ }
220
+ // 如果原来为选中状态,那么可以取消
221
+ if (props.modelValue == true) {
222
+ emitEvent();
223
+ emit('update:modelValue', !props.modelValue);
224
+ } else {
225
+ // 如果超出最多可选项,提示
226
+ if (parent && checkedNum >= parent.props.max) {
227
+ return $u.toast(`最多可选${parent.props.max}项`);
228
+ }
229
+ // 如果原来为未选中状态,需要选中的数量少于父组件中设置的max值,才可以选中
230
+ emitEvent();
231
+ emit('update:modelValue', !props.modelValue);
232
+ }
233
+ }
234
+
235
+ defineExpose({ setValue, emitEvent });
236
+ </script>
237
+
238
+ <style lang="scss" scoped>
239
+ @import '../../libs/css/style.components.scss';
240
+
241
+ .u-checkbox {
242
+ /* #ifndef APP-NVUE */
243
+ display: inline-flex;
244
+ /* #endif */
245
+ align-items: center;
246
+ overflow: hidden;
247
+ user-select: none;
248
+ line-height: 1.8;
249
+
250
+ &__icon-wrap {
251
+ color: $u-content-color;
252
+ flex: none;
253
+ display: -webkit-flex;
254
+ @include vue-flex;
255
+ align-items: center;
256
+ justify-content: center;
257
+ box-sizing: border-box;
258
+ width: 42rpx;
259
+ height: 42rpx;
260
+ color: transparent;
261
+ text-align: center;
262
+ transition-property: color, border-color, background-color;
263
+ font-size: 20px;
264
+ border: 1px solid #c8c9cc;
265
+ transition-duration: 0.2s;
266
+
267
+ /* #ifdef MP-TOUTIAO */
268
+ // 头条小程序兼容性问题,需要设置行高为0,否则图标偏下
269
+ &__icon {
270
+ line-height: 0;
271
+ }
272
+ /* #endif */
273
+
274
+ &--circle {
275
+ border-radius: 100%;
276
+ }
277
+
278
+ &--square {
279
+ border-radius: 6rpx;
280
+ }
281
+
282
+ &--checked {
283
+ color: #fff;
284
+ background-color: $u-type-primary;
285
+ border-color: $u-type-primary;
286
+ }
287
+
288
+ &--disabled {
289
+ background-color: #ebedf0;
290
+ border-color: #c8c9cc;
291
+ }
292
+
293
+ &--disabled--checked {
294
+ color: #c8c9cc !important;
295
+ }
296
+ }
297
+
298
+ &__label {
299
+ word-wrap: break-word;
300
+ margin-left: 10rpx;
301
+ margin-right: 24rpx;
302
+ color: $u-content-color;
303
+ font-size: 30rpx;
304
+
305
+ &--disabled {
306
+ color: #c8c9cc;
307
+ }
308
+ }
309
+ }
310
+ </style>
@@ -0,0 +1,134 @@
1
+ <template>
2
+ <view class="u-checkbox-group u-clearfix">
3
+ <slot></slot>
4
+ </view>
5
+ </template>
6
+
7
+ <script setup lang="ts">
8
+ import { ref, getCurrentInstance, provide } from 'vue';
9
+ import { $u } from '../..';
10
+
11
+ defineOptions({
12
+ name: 'u-checkbox-group'
13
+ });
14
+
15
+ /**
16
+ * checkboxGroup 开关选择器父组件Group
17
+ * @description 复选框组件一般用于需要多个选择的场景,该组件功能完整,使用方便
18
+ * @tutorial https://www.uviewui.com/components/checkbox.html
19
+ * @property {String Number} max 最多能选中多少个checkbox(默认999)
20
+ * @property {String Number} size 组件整体的大小,单位rpx(默认40)
21
+ * @property {Boolean} disabled 是否禁用所有checkbox(默认false)
22
+ * @property {String Number} icon-size 图标大小,单位rpx(默认20)
23
+ * @property {Boolean} label-disabled 是否禁止点击文本操作checkbox(默认false)
24
+ * @property {String} width 宽度,需带单位
25
+ * @property {String} shape 外观形状,shape-方形,circle-圆形(默认circle)
26
+ * @property {Boolean} wrap 是否每个checkbox都换行(默认false)
27
+ * @property {String} active-color 选中时的颜色,应用到所有子Checkbox组件(默认#2979ff)
28
+ * @event {Function} change 任一个checkbox状态发生变化时触发,回调为一个对象
29
+ * @example <u-checkbox-group></u-checkbox-group>
30
+ */
31
+
32
+ const props = defineProps({
33
+ /** 最多能选中多少个checkbox */
34
+ max: {
35
+ type: [Number, String],
36
+ default: 999
37
+ },
38
+ // /** 所有选中项的 name */
39
+ // value: {
40
+ // default: Array,
41
+ // default: () => []
42
+ // },
43
+ /** 是否禁用所有复选框 */
44
+ disabled: {
45
+ type: Boolean,
46
+ default: false
47
+ },
48
+ /** 在表单内提交时的标识符 */
49
+ name: {
50
+ type: [Boolean, String],
51
+ default: ''
52
+ },
53
+ /** 是否禁止点击提示语选中复选框 */
54
+ labelDisabled: {
55
+ type: Boolean,
56
+ default: false
57
+ },
58
+ /** 形状,square为方形,circle为原型 */
59
+ shape: {
60
+ type: String,
61
+ default: 'square'
62
+ },
63
+ /** 选中状态下的颜色 */
64
+ activeColor: {
65
+ type: String,
66
+ default: '#2979ff'
67
+ },
68
+ /** 组件的整体大小 */
69
+ size: {
70
+ type: [String, Number],
71
+ default: 34
72
+ },
73
+ /** 每个checkbox占u-checkbox-group的宽度 */
74
+ width: {
75
+ type: String,
76
+ default: 'auto'
77
+ },
78
+ /** 是否每个checkbox都换行 */
79
+ wrap: {
80
+ type: Boolean,
81
+ default: false
82
+ },
83
+ /** 图标的大小,单位rpx */
84
+ iconSize: {
85
+ type: [String, Number],
86
+ default: 20
87
+ }
88
+ });
89
+
90
+ const emit = defineEmits(['change']);
91
+
92
+ // 复选框子项集合
93
+ const children = ref<any[]>([]); // 用于存储所有子checkbox实例
94
+
95
+ // 向子组件 provide 本 group 实例
96
+ provide('u-checkbox-group', {
97
+ props,
98
+ emitEvent,
99
+ children
100
+ });
101
+
102
+ const instance = getCurrentInstance();
103
+
104
+ /**
105
+ * 派发 change 事件和表单校验
106
+ */
107
+ function emitEvent() {
108
+ // 收集所有选中的 name
109
+ let values: any[] = [];
110
+ children.value.forEach(val => {
111
+ if (val.modelValue) values.push(val.name);
112
+ });
113
+ emit('change', values);
114
+ // 发出事件,用于在表单组件中嵌入checkbox的情况,进行验证
115
+ // 由于头条小程序执行迟钝,故需要用几十毫秒的延时
116
+ setTimeout(() => {
117
+ // 将当前的值发送到 u-form-item 进行校验
118
+ $u.dispatch(instance, 'u-form-item', 'on-form-change', values);
119
+ }, 60);
120
+ }
121
+
122
+ defineExpose({ emitEvent, children, props });
123
+ </script>
124
+
125
+ <style lang="scss" scoped>
126
+ @import '../../libs/css/style.components.scss';
127
+
128
+ .u-checkbox-group {
129
+ /* #ifndef MP || APP-NVUE */
130
+ display: inline-flex;
131
+ flex-wrap: wrap;
132
+ /* #endif */
133
+ }
134
+ </style>