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,116 @@
1
+ <template>
2
+ <view class="u-radio-group u-clearfix">
3
+ <slot></slot>
4
+ </view>
5
+ </template>
6
+
7
+ <script setup lang="ts">
8
+ import { ref, provide, watch, getCurrentInstance } from 'vue';
9
+ import { $u } from '../..';
10
+
11
+ defineOptions({
12
+ name: 'u-radio-group'
13
+ });
14
+
15
+ /**
16
+ * radioGroup 单选框父组件
17
+ * @description 单选框用于有一个选择,用户只能选择其中一个的场景。搭配u-radio使用
18
+ * @tutorial https://www.uviewui.com/components/radio.html
19
+ * @property {Boolean} disabled 是否禁用所有radio(默认false)
20
+ * @property {String|Number} size 组件整体的大小,单位rpx(默认40)
21
+ * @property {String} active-color 选中时的颜色,应用到所有子Radio组件(默认#2979ff)
22
+ * @property {String|Number} icon-size 图标大小,单位rpx(默认20)
23
+ * @property {String} shape 外观形状,shape-方形,circle-圆形(默认circle)
24
+ * @property {Boolean} label-disabled 是否禁止点击文本操作checkbox(默认false)
25
+ * @property {String|Number} width 宽度,需带单位
26
+ * @property {Boolean} wrap 是否每个radio都换行(默认false)
27
+ * @event {Function} change 任一个radio状态发生变化时触发
28
+ * @example <u-radio-group v-model="value"></u-radio-group>
29
+ */
30
+
31
+ const props = defineProps({
32
+ /** 是否禁用所有单选框 */
33
+ disabled: { type: Boolean, default: false },
34
+ /** 匹配某一个radio组件,如果某个radio的name值等于此值,那么这个radio就被会选中 */
35
+ modelValue: { type: [String, Number], default: '' },
36
+ /** 选中状态下的颜色 */
37
+ activeColor: { type: String, default: '#2979ff' },
38
+ /** 组件的整体大小 */
39
+ size: { type: [String, Number], default: 34 },
40
+ /** 是否禁止点击提示语选中复选框 */
41
+ labelDisabled: { type: Boolean, default: false },
42
+ /** 形状,square为方形,circle为原型 */
43
+ shape: { type: String, default: 'circle' },
44
+ /** 图标的大小,单位rpx */
45
+ iconSize: { type: [String, Number], default: 20 },
46
+ /** 每个checkbox占u-checkbox-group的宽度 */
47
+ width: { type: [String, Number], default: 'auto' },
48
+ /** 是否每个checkbox都换行 */
49
+ wrap: { type: Boolean, default: false }
50
+ });
51
+
52
+ const emit = defineEmits(['update:modelValue', 'change']);
53
+
54
+ // 当前所有子 radio 实例
55
+ const children = ref<any[]>([]);
56
+
57
+ /**
58
+ * 父组件数据,供子组件 inject 使用
59
+ */
60
+ function getData() {
61
+ return {
62
+ iconSize: props.iconSize,
63
+ labelDisabled: props.labelDisabled,
64
+ disabled: props.disabled,
65
+ shape: props.shape,
66
+ activeColor: props.activeColor,
67
+ size: props.size,
68
+ width: props.width,
69
+ wrap: props.wrap,
70
+ value: props.modelValue
71
+ };
72
+ }
73
+
74
+ /**
75
+ * 设置选中值
76
+ * @param val 选中的 radio 的 name
77
+ */
78
+ function setValue(val: string | number) {
79
+ // 通过emit事件,设置父组件通过v-model双向绑定的值
80
+ emit('update:modelValue', val);
81
+ emit('change', val);
82
+ // 等待下一个周期再执行,因为emit作用于父组件,再反馈到子组件内部,需要时间
83
+ // 由于头条小程序执行迟钝,故需要用几十毫秒的延时
84
+ setTimeout(() => {
85
+ // 将当前的值发送到 u-form-item 进行校验
86
+ $u.dispatch(getCurrentInstance(), 'u-form-item', 'on-form-change', val);
87
+ }, 60);
88
+ }
89
+
90
+ // provide 父组件数据和方法,供子组件使用
91
+ provide('u-radio-group', {
92
+ getData,
93
+ setValue
94
+ });
95
+
96
+ // 监听 props 变化,通知子组件刷新
97
+ watch(
98
+ () => [props.modelValue, props.disabled, props.activeColor, props.size, props.labelDisabled, props.shape, props.iconSize, props.width, props.wrap],
99
+ () => {
100
+ // 这里可通过事件或响应式通知子组件刷新
101
+ // 兼容性处理,子组件可通过 inject 的 getData 获取最新值
102
+ console.log('u-radio-group 监听到属性变化');
103
+ }
104
+ );
105
+ </script>
106
+
107
+ <style lang="scss" scoped>
108
+ @import '../../libs/css/style.components.scss';
109
+
110
+ .u-radio-group {
111
+ /* #ifndef MP || APP-NVUE */
112
+ display: inline-flex;
113
+ flex-wrap: wrap;
114
+ /* #endif */
115
+ }
116
+ </style>
@@ -0,0 +1,349 @@
1
+ <template>
2
+ <view class="u-rate" :id="elId" @touchmove.stop.prevent="touchMove">
3
+ <view class="u-star-wrap" v-for="(item, index) in count" :key="index" :class="[elClass]">
4
+ <u-icon
5
+ :name="activeIndex > index ? elActiveIcon : inactiveIcon"
6
+ @click="click(index + 1, $event)"
7
+ :color="activeIndex > index ? elActiveColor : inactiveColor"
8
+ :custom-style="{
9
+ fontSize: size + 'rpx',
10
+ padding: `0 ${Number(gutter) / 2 + 'rpx'}`
11
+ }"
12
+ :custom-prefix="customPrefix"
13
+ :show-decimal-icon="showDecimalIcon(index)"
14
+ :percent="decimal"
15
+ :inactive-color="inactiveColor"
16
+ ></u-icon>
17
+ </view>
18
+ </view>
19
+ </template>
20
+
21
+ <script setup lang="ts">
22
+ import { ref, computed, watch, onMounted, getCurrentInstance } from 'vue';
23
+ import { $u } from '../..';
24
+
25
+ defineOptions({
26
+ name: 'u-rate'
27
+ });
28
+
29
+ /**
30
+ * rate 评分
31
+ * @description 该组件一般用于满意度调查,星型评分的场景
32
+ * @tutorial https://www.uviewui.com/components/rate.html
33
+ * @property {String|Number} count 最多可选的星星数量(默认5)
34
+ * @property {String|Number} current 默认选中的星星数量(默认0)
35
+ * @property {Boolean} disabled 是否禁止用户操作(默认false)
36
+ * @property {String|Number} size 星星的大小,单位rpx(默认32)
37
+ * @property {String} inactive-color 未选中星星的颜色(默认#b2b2b2)
38
+ * @property {String} active-color 选中的星星颜色(默认#FA3534)
39
+ * @property {String} active-icon 选中时的图标名,只能为uView的内置图标(默认star-fill)
40
+ * @property {String} inactive-icon 未选中时的图标名,只能为uView的内置图标(默认star)
41
+ * @property {String|Number} gutter 星星之间的距离(默认10)
42
+ * @property {String|Number} min-count 最少选中星星的个数(默认0)
43
+ * @property {Boolean} allow-half 是否允许半星选择(默认false)
44
+ * @event {Function} change 选中的星星发生变化时触发
45
+ * @example <u-rate :count="count" :current="2"></u-rate>
46
+ */
47
+ const props = defineProps({
48
+ /**
49
+ * 用于v-model双向绑定选中的星星数量
50
+ * 1.4.5版新增
51
+ * @default -1
52
+ */
53
+ modelValue: {
54
+ type: [Number, String],
55
+ default: -1
56
+ },
57
+ /**
58
+ * 要显示的星星数量
59
+ * @default 5
60
+ */
61
+ count: {
62
+ type: [Number, String],
63
+ default: 5
64
+ },
65
+ /**
66
+ * 当前需要默认选中的星星(选中的个数)
67
+ * 1.4.5后通过value双向绑定,不再建议使用此参数
68
+ * @default 0
69
+ */
70
+ current: {
71
+ type: [Number, String],
72
+ default: 0
73
+ },
74
+ /**
75
+ * 是否不可选中
76
+ * @default false
77
+ */
78
+ disabled: {
79
+ type: Boolean,
80
+ default: false
81
+ },
82
+ /**
83
+ * 星星的大小,单位rpx
84
+ * @default 32
85
+ */
86
+ size: {
87
+ type: [Number, String],
88
+ default: 32
89
+ },
90
+ /**
91
+ * 未选中时的颜色
92
+ * @default '#b2b2b2'
93
+ */
94
+ inactiveColor: {
95
+ type: String,
96
+ default: '#b2b2b2'
97
+ },
98
+ /**
99
+ * 选中的颜色
100
+ * @default '#FA3534'
101
+ */
102
+ activeColor: {
103
+ type: String,
104
+ default: '#FA3534'
105
+ },
106
+ /**
107
+ * 星星之间的间距,单位rpx
108
+ * @default 10
109
+ */
110
+ gutter: {
111
+ type: [Number, String],
112
+ default: 10
113
+ },
114
+ /**
115
+ * 最少能选择的星星个数
116
+ * @default 0
117
+ */
118
+ minCount: {
119
+ type: [Number, String],
120
+ default: 0
121
+ },
122
+ /**
123
+ * 是否允许半星(功能尚未实现)
124
+ * @default false
125
+ */
126
+ allowHalf: {
127
+ type: Boolean,
128
+ default: false
129
+ },
130
+ /**
131
+ * 选中时的图标(星星)
132
+ * @default 'star-fill'
133
+ */
134
+ activeIcon: {
135
+ type: String,
136
+ default: 'star-fill'
137
+ },
138
+ /**
139
+ * 未选中时的图标(星星)
140
+ * @default 'star'
141
+ */
142
+ inactiveIcon: {
143
+ type: String,
144
+ default: 'star'
145
+ },
146
+ /**
147
+ * 自定义扩展前缀,方便用户扩展自己的图标库
148
+ * @default 'uicon'
149
+ */
150
+ customPrefix: {
151
+ type: String,
152
+ default: 'uicon'
153
+ },
154
+ /**
155
+ * 分段颜色
156
+ * @default []
157
+ */
158
+ colors: {
159
+ type: Array,
160
+ default: () => []
161
+ },
162
+ /**
163
+ * 分段图标
164
+ * @default []
165
+ */
166
+ icons: {
167
+ type: Array,
168
+ default: () => []
169
+ }
170
+ });
171
+
172
+ const emit = defineEmits(['update:modelValue', 'change']);
173
+ const instance = getCurrentInstance();
174
+ // 生成唯一id,防止页面多个评分组件冲突
175
+ const elId = ref($u.guid());
176
+ const elClass = ref($u.guid());
177
+ const starBoxLeft = ref(0); // 评分盒子左边到屏幕左边的距离
178
+ const starWidth = ref(0); // 每个星星的宽度
179
+ const starWidthArr = ref<number[]>([]); //每个星星最右边到组件盒子最左边的距离
180
+ // 当前激活的星星的index,如果存在value,优先使用value,因为它可以双向绑定(1.4.5新增)
181
+ const activeIndex = ref(Number(props.modelValue) !== -1 ? Number(props.modelValue) : Number(props.current));
182
+
183
+ // 监听props变化,保持activeIndex同步
184
+ watch(
185
+ () => props.current,
186
+ val => {
187
+ activeIndex.value = Number(val);
188
+ }
189
+ );
190
+ watch(
191
+ () => props.modelValue,
192
+ val => {
193
+ activeIndex.value = Number(val);
194
+ }
195
+ );
196
+
197
+ /**
198
+ * 计算当前星星的显示小数部分(半星)
199
+ */
200
+ const decimal = computed(() => {
201
+ if (props.disabled) {
202
+ return (Number(activeIndex.value) * 100) % 100;
203
+ } else if (props.allowHalf) {
204
+ return 50;
205
+ }
206
+ return 0;
207
+ });
208
+ /**
209
+ * 计算当前激活的图标
210
+ */
211
+ const elActiveIcon = computed(() => {
212
+ const len = props.icons.length;
213
+ // 此处规则类似于下方的elActiveColor参数,都是根据一定的规则,显示不同的图标
214
+ // 结果可能如此:icons参数传递了3个图标,当选中两个时,用第一个图标,4个时,用第二个图标
215
+ // 第三个时,用第三个图标作为激活的图标
216
+ if (len && len <= Number(props.count)) {
217
+ const step = Math.round(Number(activeIndex.value) / Math.round(Number(props.count) / len));
218
+ if (step < 1) return props.icons[0];
219
+ if (step > len) return props.icons[len - 1];
220
+ return props.icons[step - 1];
221
+ }
222
+ return props.activeIcon;
223
+ });
224
+ /**
225
+ * 计算当前激活的颜色
226
+ */
227
+ const elActiveColor = computed(() => {
228
+ const len = props.colors.length;
229
+ // 如果有设置colors参数(此参数用于将图标分段,比如一共5颗星,colors传3个颜色值,那么根据一定的规则,2颗星可能为第一个颜色
230
+ // 4颗星为第二个颜色值,5颗星为第三个颜色值)
231
+ if (len && len <= Number(props.count)) {
232
+ const step = Math.round(Number(activeIndex.value) / Math.round(Number(props.count) / len));
233
+ if (step < 1) return props.colors[0];
234
+ if (step > len) return props.colors[len - 1];
235
+ return props.colors[step - 1];
236
+ }
237
+ return props.activeColor;
238
+ });
239
+
240
+ /**
241
+ * 获取评分组件盒子的布局信息
242
+ */
243
+ function getElRectById() {
244
+ // uView封装的获取节点的方法,详见文档
245
+ $u.getRect('#' + elId.value, instance).then((res: any) => {
246
+ starBoxLeft.value = res.left;
247
+ });
248
+ }
249
+ /**
250
+ * 获取单个星星的尺寸
251
+ */
252
+ function getElRectByClass() {
253
+ // uView封装的获取节点的方法,详见文档
254
+ $u.getRect('.' + elClass.value, instance).then((res: any) => {
255
+ starWidth.value = res.width;
256
+ // 把每个星星右边到组件盒子左边的距离放入数组中
257
+ for (let i = 0; i < Number(props.count); i++) {
258
+ starWidthArr.value[i] = (i + 1) * starWidth.value;
259
+ }
260
+ });
261
+ }
262
+ /**
263
+ * 手指滑动评分
264
+ */
265
+ function touchMove(e: any) {
266
+ if (props.disabled) {
267
+ return;
268
+ }
269
+ if (!e.changedTouches[0]) {
270
+ return;
271
+ }
272
+ const movePageX = e.changedTouches[0].pageX;
273
+ // 滑动点相对于评分盒子左边的距离
274
+ const distance = movePageX - starBoxLeft.value;
275
+ // 如果滑动到了评分盒子的左边界,就设置为0星
276
+ if (distance <= 0) {
277
+ activeIndex.value = 0;
278
+ }
279
+ // 滑动的距离,相当于多少颗星星
280
+ let index = Math.ceil(distance / starWidth.value);
281
+ activeIndex.value = index > Number(props.count) ? Number(props.count) : index;
282
+ // 对最少颗星星的限制
283
+ if (activeIndex.value < Number(props.minCount)) activeIndex.value = Number(props.minCount);
284
+ emitEvent();
285
+ }
286
+ /**
287
+ * 通过点击,直接选中
288
+ */
289
+ function click(index: number, e: any) {
290
+ if (props.disabled) {
291
+ return;
292
+ }
293
+ // 半星选择,尚未实现
294
+ if (props.allowHalf) {
295
+ // 预留半星实现
296
+ }
297
+ // 对第一个星星特殊处理,只有一个的时候,点击可以取消,否则无法作0星评价
298
+ if (index == 1) {
299
+ if (activeIndex.value == 1) {
300
+ activeIndex.value = 0;
301
+ } else {
302
+ activeIndex.value = 1;
303
+ }
304
+ } else {
305
+ activeIndex.value = index;
306
+ }
307
+ // 对最少颗星星的限制
308
+ if (activeIndex.value < Number(props.minCount)) activeIndex.value = Number(props.minCount);
309
+ emitEvent();
310
+ }
311
+ /**
312
+ * 发出事件
313
+ */
314
+ function emitEvent() {
315
+ // 发出change事件
316
+ emit('change', activeIndex.value);
317
+ // 同时修改双向绑定的value的值
318
+ if (Number(props.modelValue) !== -1) {
319
+ emit('update:modelValue', activeIndex.value);
320
+ }
321
+ }
322
+ /**
323
+ * 是否显示半星图标
324
+ */
325
+ function showDecimalIcon(index: number) {
326
+ return props.disabled && parseInt(String(activeIndex.value)) === index;
327
+ }
328
+
329
+ onMounted(() => {
330
+ getElRectById();
331
+ getElRectByClass();
332
+ });
333
+ </script>
334
+
335
+ <style scoped lang="scss">
336
+ @import '../../libs/css/style.components.scss';
337
+
338
+ .u-rate {
339
+ display: -webkit-inline-flex;
340
+ display: inline-flex;
341
+ align-items: center;
342
+ margin: 0;
343
+ padding: 0;
344
+ }
345
+
346
+ .u-icon {
347
+ box-sizing: border-box;
348
+ }
349
+ </style>
@@ -0,0 +1,199 @@
1
+ <template>
2
+ <view class="">
3
+ <view
4
+ class="u-content"
5
+ :class="[elId]"
6
+ :style="{
7
+ height: isLongContent && !showMore ? showHeight + 'rpx' : 'auto',
8
+ textIndent: textIndent
9
+ }"
10
+ >
11
+ <slot></slot>
12
+ </view>
13
+ <view @tap="toggleReadMore" v-if="isLongContent" class="u-content__showmore-wrap" :class="{ 'u-content__show-more': showMore }" :style="[innerShadowStyle]">
14
+ <text
15
+ class="u-content__showmore-wrap__readmore-btn"
16
+ :style="{
17
+ fontSize: fontSize + 'rpx',
18
+ color: color
19
+ }"
20
+ >
21
+ {{ showMore ? openText : closeText }}
22
+ </text>
23
+ <view class="u-content__showmore-wrap__readmore-btn__icon u-flex">
24
+ <u-icon :color="color" :size="fontSize" :name="showMore ? 'arrow-up' : 'arrow-down'"></u-icon>
25
+ </view>
26
+ </view>
27
+ </view>
28
+ </template>
29
+
30
+ <script setup lang="ts">
31
+ import { ref, computed, watch, onMounted, nextTick, getCurrentInstance } from 'vue';
32
+ import { $u } from '../..';
33
+
34
+ defineOptions({ name: 'u-read-more' });
35
+
36
+ /**
37
+ * readMore 阅读更多
38
+ * @description 该组件一般用于内容较长,预先收起一部分,点击展开全部内容的场景。
39
+ * @tutorial https://www.uviewui.com/components/readMore.html
40
+ * @property {String|Number} showHeight 内容超出此高度才会显示展开全文按钮,单位rpx(默认400)
41
+ * @property {Boolean} toggle 展开后是否显示收起按钮(默认false)
42
+ * @property {String} closeText 关闭时的提示文字(默认“展开阅读全文”)
43
+ * @property {String|Number} fontSize 提示文字的大小,单位rpx(默认28)
44
+ * @property {String} textIndent 段落首行缩进的字符个数(默认2em)
45
+ * @property {String} openText 展开时的提示文字(默认“收起”)
46
+ * @property {String} color 提示文字的颜色(默认#2979ff)
47
+ * @property {Object} shadowStyle 是否显示阴影,样式对象
48
+ * @property {String|Number} index open和close事件时,将此参数返回在回调参数中
49
+ * @event open 展开时触发
50
+ * @event close 收起时触发
51
+ * @example <u-read-more><rich-text :nodes="content"></rich-text></u-read-more>
52
+ */
53
+
54
+ const props = defineProps({
55
+ /** 默认的显示占位高度,单位为rpx */
56
+ showHeight: {
57
+ type: [Number, String],
58
+ default: 400
59
+ },
60
+ /** 展开后是否显示"收起"按钮 */
61
+ toggle: {
62
+ type: Boolean,
63
+ default: false
64
+ },
65
+ /** 关闭时的提示文字 */
66
+ closeText: {
67
+ type: String,
68
+ default: '展开阅读全文'
69
+ },
70
+ /** 展开时的提示文字 */
71
+ openText: {
72
+ type: String,
73
+ default: '收起'
74
+ },
75
+ /** 提示的文字颜色 */
76
+ color: {
77
+ type: String,
78
+ default: '#2979ff'
79
+ },
80
+ /** 提示文字的大小 */
81
+ fontSize: {
82
+ type: [String, Number],
83
+ default: 28
84
+ },
85
+ /** 是否显示阴影 */
86
+ shadowStyle: {
87
+ type: Object as () => Record<string, any>,
88
+ default: () => ({
89
+ backgroundImage: 'linear-gradient(-180deg, rgba(255, 255, 255, 0) 0%, #fff 80%)',
90
+ paddingTop: '300rpx',
91
+ marginTop: '-300rpx'
92
+ })
93
+ },
94
+ /** 段落首行缩进的字符个数 */
95
+ textIndent: {
96
+ type: String,
97
+ default: '2em'
98
+ },
99
+ /** open和close事件时,将此参数返回在回调参数中 */
100
+ index: {
101
+ type: [Number, String],
102
+ default: ''
103
+ }
104
+ });
105
+
106
+ const emit = defineEmits(['open', 'close']);
107
+
108
+ // 是否需要隐藏一部分内容
109
+ const isLongContent = ref(false);
110
+ // 当前隐藏与显示的状态,true-显示,false-收起
111
+ const showMore = ref(false);
112
+ // 生成唯一class
113
+ const elId = ref('');
114
+ const instance = getCurrentInstance();
115
+
116
+ // 展开后无需阴影,收起时才需要阴影样式
117
+ const innerShadowStyle = computed(() => {
118
+ return showMore.value ? {} : props.shadowStyle;
119
+ });
120
+
121
+ // 监听 toggle 和 showHeight 变化,重新初始化
122
+ watch(
123
+ () => `${props.toggle}-${props.showHeight}`,
124
+ () => {
125
+ init();
126
+ }
127
+ );
128
+
129
+ onMounted(() => {
130
+ elId.value = $u.guid();
131
+ nextTick(() => {
132
+ init();
133
+ });
134
+ });
135
+
136
+ /**
137
+ * 初始化内容高度判断
138
+ * @description 判断内容是否超出指定高度,决定是否显示展开/收起按钮
139
+ */
140
+ function init() {
141
+ $u.getRect('.' + elId.value, instance).then((res: { height: number }) => {
142
+ // 判断高度,如果真实内容高度大于占位高度,则显示收起与展开的控制按钮
143
+ if (res.height > uni.upx2px(Number(props.showHeight))) {
144
+ isLongContent.value = true;
145
+ showMore.value = false;
146
+ }
147
+ });
148
+ }
149
+
150
+ /**
151
+ * 展开或者收起内容
152
+ * @description 切换显示状态,发出 open/close 事件
153
+ */
154
+ function toggleReadMore() {
155
+ showMore.value = !showMore.value;
156
+ // 如果toggle为false,隐藏"收起"部分的内容
157
+ if (props.toggle == false) isLongContent.value = false;
158
+ // 发出打开或者收齐的事件
159
+ emit(showMore.value ? 'open' : 'close', props.index);
160
+ }
161
+ </script>
162
+
163
+ <style lang="scss" scoped>
164
+ @import '../../libs/css/style.components.scss';
165
+
166
+ .u-content {
167
+ font-size: 30rpx;
168
+ color: $u-content-color;
169
+ line-height: 1.8;
170
+ text-align: left;
171
+ overflow: hidden;
172
+
173
+ &__show-more {
174
+ padding-top: 0;
175
+ background: none;
176
+ margin-top: 20rpx;
177
+ }
178
+
179
+ &__showmore-wrap {
180
+ position: relative;
181
+ width: 100%;
182
+ padding-bottom: 26rpx;
183
+ @include vue-flex;
184
+ align-items: center;
185
+ justify-content: center;
186
+
187
+ &__readmore-btn {
188
+ @include vue-flex;
189
+ align-items: center;
190
+ justify-content: center;
191
+ line-height: 1;
192
+
193
+ &__icon {
194
+ margin-left: 14rpx;
195
+ }
196
+ }
197
+ }
198
+ }
199
+ </style>