uview-plus 3.4.76 → 3.4.77

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 CHANGED
@@ -1,9 +1,17 @@
1
+ ## 3.4.77(2025-08-08)
2
+ feat: 优化waterfall瀑布流组件逻辑
3
+
4
+ feat: waterfall瀑布流组件支持多列
5
+
6
+ feat: waterfall瀑布流组件支持响应式列数
7
+
1
8
  ## 3.4.76(2025-08-08)
2
9
  feat: 取消props中tabs组件默认颜色影响主题色生效
3
10
 
4
11
  fix: 修复parse富文本组件报错$options
5
12
 
6
13
  feat: timeFormat时间格式化方法支持UTC格式时间 #596
14
+
7
15
  ## 3.4.75(2025-08-07)
8
16
  feat: 新增loadFontOnce参数控制是否全局只加载一次字体图标
9
17
 
@@ -153,7 +153,6 @@
153
153
  </script>
154
154
 
155
155
  <style lang="scss" scoped>
156
-
157
156
  // 变量定义
158
157
  $u-icon-primary: $u-primary !default;
159
158
  $u-icon-success: $u-success !default;
@@ -1,25 +1,41 @@
1
1
  <template>
2
2
  <view class="u-waterfall">
3
- <view ref="u-left-column" id="u-left-column" class="u-column">
4
- <slot name="left" :leftList="leftList"></slot>
5
- </view>
6
- <view ref="u-right-column" id="u-right-column" class="u-column">
7
- <slot name="right" :rightList="rightList"></slot>
3
+ <!-- 新增支持多列布局 -->
4
+ <view
5
+ v-for="(column, index) in columnList"
6
+ :key="index"
7
+ :ref="`u-column-${index}`"
8
+ :id="`u-column-${index}`"
9
+ class="u-column"
10
+ >
11
+ <slot name="column"
12
+ :colIndex="index"
13
+ :colList="column">
14
+ </slot>
15
+ <slot name="left"
16
+ :colIndex="index"
17
+ :leftList="column">
18
+ </slot>
19
+ <template v-if="!$slots['left'] && !$slots['column']" v-for="(item, itemIndex) in column" :key="itemIndex">
20
+ <slot :item="item" :itemIndex="itemIndex"></slot>
21
+ </template>
8
22
  </view>
9
23
  </view>
10
24
  </template>
11
25
 
12
26
  <script>
13
27
  /**
14
- * waterfall 瀑布流
15
- * @description 这是一个瀑布流形式的组件,内容分为左右两列,结合uview的懒加载组件效果更佳。相较于某些只是奇偶数左右分别,或者没有利用vue作用域插槽的做法,uview的瀑布流实现了真正的 组件化,搭配LazyLoad 懒加载和loadMore 加载更多组件,让您开箱即用,眼前一亮。
28
+ * waterfall 瀴布流
29
+ * @description 这是一个瀑布流形式的组件,对原组件进行升级已经支持自定义列数模式,便于适配不同屏幕。搭配loadMore 加载更多组件,让您开箱即用,眼前一亮。
16
30
  * @tutorial https://uview-plus.jiangruyi.com/components/waterfall.html
17
31
  * @property {Array} flow-list 用于渲染的数据
18
32
  * @property {String Number} add-time 单条数据添加到队列的时间间隔,单位ms,见上方注意事项说明(默认200)
33
+ * @property {String Number} columns 瀑布流列数,默认为2,设置为auto时自动根据屏幕宽度调整列数
19
34
  * @example <u-waterfall :flowList="flowList"></u-waterfall>
20
35
  */
21
36
  import { mpMixin } from '../../libs/mixin/mpMixin';
22
37
  import { mixin } from '../../libs/mixin/mixin';
38
+ import { sleep } from '../../libs/function/index';
23
39
  export default {
24
40
  name: "u-waterfall",
25
41
  props: {
@@ -43,7 +59,7 @@
43
59
  }
44
60
  },
45
61
  // #endif
46
- // 每次向结构插入数据的时间间隔,间隔越长,越能保证两列高度相近,但是对用户体验越不好
62
+ // 每次向结构插入数据的时间间隔,单位ms
47
63
  // 单位ms
48
64
  addTime: {
49
65
  type: [Number, String],
@@ -54,41 +70,98 @@
54
70
  idKey: {
55
71
  type: String,
56
72
  default: 'id'
73
+ },
74
+ // 瀑布流列数
75
+ columns: {
76
+ type: [Number, String],
77
+ default: 2
78
+ },
79
+ // 瀑布流最小列数
80
+ columnsMin: {
81
+ type: [Number, String],
82
+ default: 2
83
+ },
84
+ // 最小列宽
85
+ minColumnWidth: {
86
+ type: Number,
87
+ default: 230
57
88
  }
58
89
  },
59
90
  mixins: [mpMixin, mixin],
60
91
  data() {
61
92
  return {
62
- leftList: [],
63
- rightList: [],
64
- tempList: [],
65
- children: []
93
+ columnList: [[]], // 存储每列的数据
94
+ children: [],
95
+ // 用于标记是否已经初始化
96
+ initialized: false,
97
+ windowWidth: 375,
98
+ windowHeight: 0
66
99
  }
67
100
  },
68
101
  watch: {
69
- copyFlowList(nVal, oVal) {
70
- if (!nVal || nVal.length == 0) {
71
- this.clear(false);
72
- // console.log('clear');
73
- } else {
74
- // 取差值,即这一次数组变化新增的部分
75
- let startIndex = Array.isArray(oVal) && oVal.length > 0 ? oVal.length : 0;
76
- // 拼接上原有数据
77
- this.tempList = this.tempList.concat(this.cloneData(nVal.slice(startIndex)));
78
- this.splitData();
79
- }
102
+ copyFlowList: {
103
+ handler(nVal, oVal) {
104
+ if (!nVal || nVal.length == 0) {
105
+ this.clear(false);
106
+ } else {
107
+ if (this.columnList.length == 1) {
108
+ this.initColumnList()
109
+ }
110
+ // 取差值,即这一次数组变化新增的部分
111
+ let startIndex = Array.isArray(oVal) && oVal.length > 0 ? oVal.length : 0;
112
+ // 直接处理数据,不再使用tempList和splitData
113
+ this.handleData(nVal.slice(startIndex));
114
+ }
115
+ },
116
+ immediate: true
117
+ },
118
+ columns: {
119
+ handler() {
120
+ this.initColumnList();
121
+ // 重新分配数据
122
+ if (this.copyFlowList.length > 0) {
123
+ this.redistributeData();
124
+ }
125
+ },
126
+ immediate: false
80
127
  }
81
128
  },
129
+ created() {
130
+ this.initColumnList();
131
+ },
82
132
  mounted() {
83
- this.tempList = this.cloneData(this.copyFlowList);
84
- this.splitData();
133
+ this.initialized = true;
134
+ // 添加窗口大小变化监听
135
+ // #ifdef H5
136
+ if (this.columns === 'auto') {
137
+ uni.onWindowResize(this.handleWindowResize);
138
+ }
139
+ // #endif
85
140
  },
141
+ // 添加beforeUnmount生命周期清理事件监听
142
+ // #ifdef VUE3
143
+ beforeUnmount() {
144
+ // #ifdef H5
145
+ if (this.columns === 'auto') {
146
+ uni.offWindowResize(this.handleWindowResize);
147
+ }
148
+ // #endif
149
+ },
150
+ // #endif
151
+ // #ifdef VUE2
152
+ beforeDestroy() {
153
+ // #ifdef H5
154
+ if (this.columns === 'auto') {
155
+ window.removeEventListener('resize', this.handleWindowResize);
156
+ }
157
+ // #endif
158
+ }
159
+ // #endif
86
160
  computed: {
87
161
  // 破坏flowList变量的引用,否则watch的结果新旧值是一样的
88
162
  copyFlowList() {
89
163
  // #ifdef VUE3
90
164
  if (!this.modelValue || this.modelValue.length == 0) {
91
- this.clear(false);
92
165
  // console.log('clear');
93
166
  return [];
94
167
  } else {
@@ -102,45 +175,126 @@
102
175
  },
103
176
  emits: ['update:modelValue'],
104
177
  methods: {
105
- async splitData() {
106
- if (!this.tempList.length) return;
107
- let leftRect = await this.$uGetRect('#u-left-column');
108
- let rightRect = await this.$uGetRect('#u-right-column');
109
- // 如果左边小于或等于右边,就添加到左边,否则添加到右边
110
- let item = this.tempList[0];
111
- // 解决多次快速上拉后,可能数据会乱的问题,因为经过上面的两个await节点查询阻塞一定时间,加上后面的定时器干扰
112
- // 数组可能变成[],导致此item值可能为undefined
113
- if (!item) return;
114
- if (leftRect.height < rightRect.height) {
115
- this.leftList.push(item);
116
- } else if (leftRect.height > rightRect.height) {
117
- this.rightList.push(item);
118
- } else {
119
- // 这里是为了保证第一和第二张添加时,左右都能有内容
120
- // 因为添加第一张,实际队列的高度可能还是0,这时需要根据队列元素长度判断下一个该放哪边
121
- if (this.leftList.length <= this.rightList.length) {
122
- this.leftList.push(item);
123
- } else {
124
- this.rightList.push(item);
178
+ // 初始化列数据数组
179
+ initColumnList() {
180
+ this.windowWidth = uni.getSystemInfoSync().windowWidth;
181
+ const cols = this.getColumnsCount();
182
+ // console.log(cols)
183
+ this.columnList = Array.from({ length: cols }, () => []);
184
+ },
185
+
186
+ // 获取列数,支持auto模式
187
+ getColumnsCount() {
188
+ if (this.columns === 'auto') {
189
+ // 列间距为10rpx(约等于7px)
190
+ const columnGap = 7;
191
+ // 计算可容纳的列数
192
+ let columnCount = Math.max(1, Math.floor(this.windowWidth / (this.minColumnWidth + columnGap)));
193
+ if (columnCount < this.columnsMin) {
194
+ columnCount = this.columnsMin
195
+ }
196
+ return columnCount;
197
+ }
198
+ return parseInt(this.columns) || 2;
199
+ },
200
+
201
+ // 窗口大小变化处理函数
202
+ handleWindowResize(res) {
203
+ this.windowWidth = res.size.windowWidth
204
+ this.windowHeight = res.size.windowHeight
205
+ // 防抖处理,避免频繁触发
206
+ if (this.resizeTimer) {
207
+ clearTimeout(this.resizeTimer);
208
+ }
209
+ this.resizeTimer = setTimeout(() => {
210
+ const newColumnsCount = this.getColumnsCount();
211
+ const oldColumnsCount = this.columnList.length;
212
+
213
+ // 只有列数发生变化时才重新分配数据
214
+ if (newColumnsCount !== oldColumnsCount) {
215
+ this.redistributeData();
216
+ }
217
+ }, 300);
218
+ },
219
+
220
+ // 重新分配所有数据
221
+ async redistributeData() {
222
+ // 清空所有列
223
+ this.initColumnList();
224
+ // 保存所有数据
225
+ const allData = this.cloneData(this.copyFlowList);
226
+ // 重新分配数据
227
+ this.handleData(allData);
228
+ },
229
+
230
+ // 处理新增数据
231
+ async handleData(newData) {
232
+ if (!newData || newData.length === 0) return;
233
+
234
+ // 初始化列高度数组
235
+ const columnHeights = new Array(this.columnList.length).fill(0);
236
+
237
+ // 获取各列当前高度
238
+ for (let i = 0; i < this.columnList.length; i++) {
239
+ try {
240
+ const rect = await this.$uGetRect(`#u-column-${i}`);
241
+ // console.log(`#u-column-${i}`, rect.height)
242
+ columnHeights[i] = rect.height || 0;
243
+ } catch (e) {
244
+ columnHeights[i] = 0;
125
245
  }
126
246
  }
127
- // 移除临时列表的第一项
128
- this.tempList.splice(0, 1);
129
- // 如果临时数组还有数据,继续循环
130
- if (this.tempList.length) {
131
- setTimeout(() => {
132
- this.splitData();
133
- }, this.addTime)
247
+
248
+ // 分配新数据到最短的列
249
+ for (let item of newData) {
250
+ const minHeightIndex = columnHeights.indexOf(Math.min(...columnHeights));
251
+ // console.log('this.columnList', this.columnList)
252
+ this.columnList[minHeightIndex].push(item);
253
+
254
+ // 获取实际渲染后的元素高度而不是估算
255
+ await sleep(30)
256
+ this.$nextTick(async () => {
257
+ try {
258
+ const rect = await this.$uGetRect(`#u-column-${minHeightIndex}`);
259
+ // console.log(`#u-column-${minHeightIndex}`, rect.height)
260
+ if (rect.height) {
261
+ columnHeights[minHeightIndex] = rect.height
262
+ }
263
+ } catch (e) {
264
+ // console.log(e)
265
+ // columnHeights[i] = 0;
266
+ }
267
+ });
268
+ // this.$nextTick(async () => {
269
+ // try {
270
+ // // 等待DOM更新后获取实际高度
271
+ // const lastIndex = this.columnList[minHeightIndex].length - 1;
272
+ // const el = this.$refs[`u-column-${minHeightIndex}`][0].children[lastIndex];
273
+ // if (el) {
274
+ // const rect = await this.$uGetRect(el);
275
+ // const actualHeight = rect.height || 100;
276
+ // columnHeights[minHeightIndex] += actualHeight;
277
+ // } else {
278
+ // // 备用方案:如果无法获取实际高度,则使用默认值
279
+ // columnHeights[minHeightIndex] += 100;
280
+ // }
281
+ // } catch (e) {
282
+ // // 出错时使用默认高度
283
+ // console.log(e)
284
+ // columnHeights[minHeightIndex] += 100;
285
+ // }
286
+ // });
134
287
  }
135
288
  },
289
+
136
290
  // 复制而不是引用对象和数组
137
291
  cloneData(data) {
138
292
  return JSON.parse(JSON.stringify(data));
139
293
  },
294
+
140
295
  // 清空数据列表
141
296
  clear(bak = true) {
142
- this.leftList = [];
143
- this.rightList = [];
297
+ this.initColumnList();
144
298
  // 同时清除父组件列表中的数据
145
299
  if (bak) {
146
300
  // #ifdef VUE2
@@ -150,69 +304,73 @@
150
304
  this.$emit('update:modelValue', []);
151
305
  // #endif
152
306
  }
153
- this.tempList = [];
154
307
  },
308
+
155
309
  // 清除某一条指定的数据,根据id实现
156
310
  remove(id) {
157
- // 如果findIndex找不到合适的条件,就会返回-1
158
- let index = -1;
159
- index = this.leftList.findIndex(val => val[this.idKey] == id);
160
- if (index != -1) {
161
- // 如果index不等于-1,说明已经找到了要找的id,根据index索引删除这一条数据
162
- this.leftList.splice(index, 1);
163
- } else {
164
- // 同理于上方面的方法
165
- index = this.rightList.findIndex(val => val[this.idKey] == id);
166
- if (index != -1) this.rightList.splice(index, 1);
311
+ // 遍历所有列查找并删除数据
312
+ for (let i = 0; i < this.columnList.length; i++) {
313
+ const index = this.columnList[i].findIndex(val => val[this.idKey] == id);
314
+ if (index !== -1) {
315
+ this.columnList[i].splice(index, 1);
316
+ break;
317
+ }
167
318
  }
319
+
168
320
  // 同时清除父组件的数据中的对应id的条目
169
321
  // #ifdef VUE2
170
- index = this.value.findIndex(val => val[this.idKey] == id);
171
- if (index != -1) this.$emit('input', this.value.splice(index, 1));
322
+ const valueIndex = this.value.findIndex(val => val[this.idKey] == id);
323
+ if (valueIndex !== -1) {
324
+ const newValue = this.cloneData(this.value);
325
+ newValue.splice(valueIndex, 1);
326
+ this.$emit('input', newValue);
327
+ }
172
328
  // #endif
173
329
  // #ifdef VUE3
174
- index = this.modelValue.findIndex(val => val[this.idKey] == id);
175
- if (index != -1) this.$emit('update:modelValue', this.modelValue.splice(index, 1));
330
+ const modelValueIndex = this.modelValue.findIndex(val => val[this.idKey] == id);
331
+ if (modelValueIndex !== -1) {
332
+ const newModelValue = this.cloneData(this.modelValue);
333
+ newModelValue.splice(modelValueIndex, 1);
334
+ this.$emit('update:modelValue', newModelValue);
335
+ }
176
336
  // #endif
177
337
  },
338
+
178
339
  // 修改某条数据的某个属性
179
340
  modify(id, key, value) {
180
- // 如果findIndex找不到合适的条件,就会返回-1
181
- let index = -1;
182
- index = this.leftList.findIndex(val => val[this.idKey] == id);
183
- if (index != -1) {
184
- // 如果index不等于-1,说明已经找到了要找的id,修改对应key的值
185
- this.leftList[index][key] = value;
186
- } else {
187
- // 同理于上方面的方法
188
- index = this.rightList.findIndex(val => val[this.idKey] == id);
189
- if (index != -1) this.rightList[index][key] = value;
341
+ let found = false;
342
+ let targetItem = null;
343
+
344
+ // 在所有列中查找数据
345
+ for (let i = 0; i < this.columnList.length; i++) {
346
+ const index = this.columnList[i].findIndex(val => val[this.idKey] == id);
347
+ if (index !== -1) {
348
+ // 修改对应key的值
349
+ this.columnList[i][index][key] = value;
350
+ targetItem = this.columnList[i][index];
351
+ found = true;
352
+ break;
353
+ }
190
354
  }
191
- // 修改父组件的数据中的对应id的条目
192
- // #ifdef VUE2
193
- index = this.value.findIndex(val => val[this.idKey] == id);
194
- // #endif
195
- // #ifdef VUE3
196
- index = this.modelValue.findIndex(val => val[this.idKey] == id);
197
- // #endif
198
- if (index != -1) {
199
- // 首先复制一份value的数据
355
+
356
+ if (found && targetItem) {
357
+ // 修改父组件的数据中的对应id的条目
200
358
  // #ifdef VUE2
201
- let data = this.cloneData(this.value);
202
- // #endif
203
- // #ifdef VUE3
204
- let data = this.cloneData(this.modelValue);
205
- // #endif
206
- // 修改对应索引的key属性的值为value
207
- data[index][key] = value;
208
- // 修改父组件通过v-model绑定的变量的值
209
- // #ifdef VUE2
210
- this.$emit('input', data);
359
+ const valueIndex = this.value.findIndex(val => val[this.idKey] == id);
360
+ if (valueIndex !== -1) {
361
+ let data = this.cloneData(this.value);
362
+ data[valueIndex][key] = value;
363
+ this.$emit('input', data);
364
+ }
211
365
  // #endif
212
366
  // #ifdef VUE3
213
- this.$emit('update:modelValue', data);
367
+ const modelValueIndex = this.modelValue.findIndex(val => val[this.idKey] == id);
368
+ if (modelValueIndex !== -1) {
369
+ let data = this.cloneData(this.modelValue);
370
+ data[modelValueIndex][key] = value;
371
+ this.$emit('update:modelValue', data);
372
+ }
214
373
  // #endif
215
-
216
374
  }
217
375
  }
218
376
  }
@@ -234,6 +392,10 @@
234
392
  /* #ifndef APP-NVUE */
235
393
  height: 100%;
236
394
  /* #endif */
395
+ // 添加列之间的间距
396
+ &:not(:first-child) {
397
+ margin-left: 10rpx;
398
+ }
237
399
  }
238
400
 
239
401
  .u-image {
@@ -241,4 +403,4 @@
241
403
  max-width: 100%;
242
404
  /* #endif */
243
405
  }
244
- </style>
406
+ </style>
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "id": "uview-plus",
3
3
  "name": "uview-plus",
4
4
  "displayName": "零云®uview-plus3.0重磅发布,全面的Vue3鸿蒙跨端移动组件库,组件丰富维护更新稳定。",
5
- "version": "3.4.76",
5
+ "version": "3.4.77",
6
6
  "description": "零云®uview-plus已兼容vue3,100+全面的组件和便捷的工具会让您信手拈来,如鱼得水。",
7
7
  "keywords": [
8
8
  "uview",