sadais-piui 1.2.7-beta35 → 1.2.7-beta36

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.
@@ -1,7 +1,7 @@
1
1
  <!--
2
2
  * @Author: zhangzhenfei
3
3
  * @Date: 2022-06-28 16:53:54
4
- * @LastEditTime: 2022-08-19 17:25:53
4
+ * @LastEditTime: 2022-08-23 11:45:24
5
5
  * @LastEditors: zhangzhenfei
6
6
  * @Description: 日历日期数据
7
7
  * @FilePath: /dt-weitibao-console/Users/feilin/workspace/piui/piui-awesome/src/piui/components/pi-calendar-panel/components/calendar-days/index.vue
@@ -361,7 +361,7 @@ export default {
361
361
  return
362
362
  }
363
363
  // 处理范围选择方式
364
- if (this.calendarValue.length === 2) {
364
+ if (this.calendarValue.length === 0 || this.calendarValue.length === 2) {
365
365
  // 如果选了两项,则重新开始选择范围
366
366
  this.handleChange([date])
367
367
  } else {
@@ -232,8 +232,8 @@ export default {
232
232
  const now = this.$pi.date.parseDate()
233
233
  return {
234
234
  now: now, // 当前时间
235
- year: '', // 年份
236
- month: '', // 月份
235
+ year: now.year, // 年份
236
+ month: now.month, // 月份
237
237
  week: 1, // 当前周
238
238
  calendarValue: '',
239
239
  currentTabIndex: 1, // 当前选中的tab索引
@@ -278,24 +278,25 @@ export default {
278
278
  this.init()
279
279
  },
280
280
  methods: {
281
- valid() {
282
- if (this.type === 'date') return true
283
- if (this.type === 'range' && this.$pi.lang.isArray(this.value) && this.value.length === 2)
284
- return true
285
- return false
286
- },
287
281
  init() {
288
282
  console.log(TAG, '组件初始化', this.value)
289
- if (!this.valid()) return
290
283
  let initValue = this.value
291
284
  const nowDate = this.$pi.date.parseDate(this.type === 'date' ? this.value : this.value[0])
292
285
  if (this.type === 'range') {
293
- const endDate = this.$pi.date.parseDate(this.value[1])
294
- if (this.year !== endDate.year && this.month !== endDate.month) {
286
+ initValue = initValue || []
287
+ if (this.value[1]) {
288
+ const endDate = this.$pi.date.parseDate(this.value[1])
289
+
290
+ this.year = endDate.year
291
+ this.month = endDate.month
292
+
293
+ initValue = [nowDate, endDate]
294
+ } else if (initValue.length === 1) {
295
+ initValue = [nowDate]
296
+
295
297
  this.year = nowDate.year
296
298
  this.month = nowDate.month
297
299
  }
298
- initValue = [nowDate, endDate]
299
300
  } else {
300
301
  this.year = nowDate.year
301
302
  this.month = nowDate.month
@@ -0,0 +1,324 @@
1
+ <template>
2
+ <scroll-view class="cascader-wrap" scroll-x :scroll-left="scrollLeft">
3
+ <view v-for="(col, idx) of columns" :key="idx" class="column">
4
+ <view
5
+ v-for="(item, subidx) in col"
6
+ :key="getKey(item, subidx)"
7
+ class="column-item"
8
+ :class="{
9
+ active: activeIds.includes(item.uid),
10
+ arrow: item.children && item.children.length > 0,
11
+ }"
12
+ @click="handleClickItem(item)"
13
+ >
14
+ {{ item[props.label] || "" }}
15
+ </view>
16
+ </view>
17
+ </scroll-view>
18
+ </template>
19
+
20
+ <script>
21
+ export default {
22
+ name: "PiCascader",
23
+ model: {
24
+ prop: "value",
25
+ event: "input",
26
+ },
27
+ props: {
28
+ // 自定义样式,对象形式
29
+ customStyle: {
30
+ type: Object,
31
+ default() {
32
+ // {}
33
+ return {};
34
+ },
35
+ },
36
+ // 自定义样式类,字符串形式
37
+ customClass: {
38
+ type: String,
39
+ // ''
40
+ default() {
41
+ return "";
42
+ },
43
+ },
44
+ // 数据源
45
+ options: {
46
+ type: Array,
47
+ default() {
48
+ return [];
49
+ },
50
+ },
51
+ value: {
52
+ type: Array,
53
+ default: () => [],
54
+ },
55
+ // 配置数据源的key
56
+ props: {
57
+ type: Object,
58
+ default() {
59
+ return {
60
+ label: "label",
61
+ value: "value",
62
+ children: "children",
63
+ id: "id",
64
+ expandTrigger: true,
65
+ };
66
+ },
67
+ },
68
+ },
69
+ data() {
70
+ return {
71
+ curItem: null,
72
+ innerOptions: [],
73
+ optMap: {},
74
+ selectedIds: [],
75
+ scrollLeft: 0,
76
+ };
77
+ },
78
+ computed: {
79
+ columns() {
80
+ if (this.curItem) {
81
+ const arr = [];
82
+ let p = this.curItem;
83
+ while (p) {
84
+ const tmp = p[this.props.children] || [];
85
+ if (tmp.length > 0) {
86
+ arr.unshift(tmp);
87
+ }
88
+ p = this.optMap[p.pid];
89
+ }
90
+ arr.unshift(this.innerOptions);
91
+ return arr;
92
+ }
93
+ return [this.innerOptions];
94
+ },
95
+ activeIds() {
96
+ if (this.curItem) {
97
+ const arr = [];
98
+ let p = this.curItem;
99
+ while (p) {
100
+ arr.unshift(p.uid);
101
+ p = this.optMap[p.pid];
102
+ }
103
+ return arr;
104
+ }
105
+ return [];
106
+ },
107
+ syncIds() {
108
+ return this.innerOptions.length + this.value.length;
109
+ },
110
+ },
111
+ watch: {
112
+ options: {
113
+ immediate: true,
114
+ handler() {
115
+ this.rebuildOptions();
116
+ },
117
+ },
118
+ syncIds: {
119
+ handler() {
120
+ this.initSelectedIds();
121
+ },
122
+ },
123
+ },
124
+ methods: {
125
+ getKey(item, idx) {
126
+ if (this.props.id) {
127
+ return item[this.props.id];
128
+ }
129
+ return `${item.level}-${idx}`;
130
+ },
131
+ randomId() {
132
+ const x = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
133
+ return x.replace(/[xy]/g, (g) => {
134
+ const t = (Math.random() * 16) | 0;
135
+ const c = g === "x" ? t : (t & 0x03) | 0x08;
136
+ return c.toString(16);
137
+ });
138
+ },
139
+ initSelectedIds() {
140
+ let curs = [...this.innerOptions];
141
+ const items = Object.values(this.optMap);
142
+ const results = [];
143
+ for (const val of this.value) {
144
+ const item = items.filter((i) => i[this.props.value] === val)[0];
145
+ if (item) {
146
+ results.push(item.uid);
147
+ }
148
+ }
149
+ this.selectedIds = results;
150
+ },
151
+ rebuildOptions() {
152
+ // 以非递归的方式 深度拷贝options 且建立父子节点关系
153
+ // 目的为了不影响外部传入的prop: options
154
+ const newOptions = [];
155
+ // 初始化堆栈 为options的根节点
156
+ const stack = [...this.options];
157
+ const optMap = {};
158
+ while (stack.length > 0) {
159
+ let item = stack.shift();
160
+ if (!item.pid) {
161
+ // 拷贝
162
+ item = { ...item, level: 0, uid: this.randomId() };
163
+ newOptions.push(item);
164
+ }
165
+ const children = item[this.props.children] || [];
166
+ const newChilds = [];
167
+ for (let i = children.length - 1; i >= 0; i--) {
168
+ // 拷贝子孩子
169
+ const subItem = {
170
+ ...children[i],
171
+ level: item.level + 1,
172
+ uid: this.randomId(),
173
+ };
174
+ // 建立父子关系
175
+ subItem.pid = item.uid;
176
+ // 放置到新数组中
177
+ newChilds.unshift(subItem);
178
+ // 入栈 模拟递归效果
179
+ stack.unshift(subItem);
180
+ }
181
+ // 更新children 为新的数组
182
+ item[this.props.children] = newChilds;
183
+ optMap[item.uid] = item;
184
+ }
185
+ this.innerOptions = newOptions;
186
+ this.optMap = optMap;
187
+ },
188
+ handleClickItem(item) {
189
+ this.curItem = item;
190
+ if (item?.children?.length === 0) {
191
+ // 点击的是叶子节点
192
+ if (this.selectedIds.includes(item.uid)) {
193
+ // 取消选中 其中uid是在rebuildOptions方法中生成的
194
+ this.selectedIds = this.selectedIds.filter((i) => i !== item.uid);
195
+ } else {
196
+ // 选中
197
+ this.selectedIds.push(item.uid);
198
+ }
199
+ // 根据选中的uid转换为对应的value
200
+ const values = this.selectedIds.reduce((prev, i) => {
201
+ const item = this.optMap[i];
202
+ if (item) {
203
+ prev.push(item[this.props.value]);
204
+ }
205
+ return prev;
206
+ }, []);
207
+ this.$emit("input", values);
208
+ this.$emit("change", values);
209
+ }
210
+ this.$nextTick(async () => {
211
+ const maxx = await this.getMaxX();
212
+ const containerWidth = await this.getViewport();
213
+ const left = await this.getScrollLeft();
214
+ const diff = maxx - (left + containerWidth);
215
+ this.scrollLeft = left + diff;
216
+ });
217
+ },
218
+ getViewport() {
219
+ return new Promise((resolve) => {
220
+ const ctx = uni.createSelectorQuery();
221
+ //#ifndef MP-ALIPAY
222
+ ctx.in(this);
223
+ //#endif
224
+ const columns = ctx
225
+ .select(".cascader-wrap")
226
+ .boundingClientRect((res) => {
227
+ console.log(res);
228
+ resolve(res?.width || 0);
229
+ })
230
+ .exec();
231
+ });
232
+ },
233
+ getScrollLeft() {
234
+ return new Promise((resolve) => {
235
+ const ctx = uni.createSelectorQuery();
236
+ //#ifndef MP-ALIPAY
237
+ ctx.in(this);
238
+ //#endif
239
+ const columns = ctx
240
+ .select(".cascader-wrap")
241
+ .scrollOffset((res) => {
242
+ console.log(res);
243
+ resolve(res?.scrollLeft || 0);
244
+ })
245
+ .exec();
246
+ });
247
+ },
248
+ getMaxX(ctx) {
249
+ return new Promise((resolve) => {
250
+ const ctx = uni.createSelectorQuery();
251
+ //#ifndef MP-ALIPAY
252
+ ctx.in(this);
253
+ //#endif
254
+ const columns = ctx
255
+ .selectAll(".column")
256
+ .boundingClientRect((res) => {
257
+ console.log(res);
258
+ if (res?.length > 0) {
259
+ const x = res.reduce((prev, i) => {
260
+ prev += i.width;
261
+ return prev;
262
+ }, 0);
263
+ resolve(x);
264
+ } else {
265
+ resolve(0);
266
+ }
267
+ })
268
+ .exec();
269
+ });
270
+ },
271
+ },
272
+ };
273
+ </script>
274
+
275
+ <style lang="scss" scoped>
276
+ ::-webkit-scrollbar {
277
+ width: 8rpx;
278
+ height: 8rpx;
279
+ }
280
+ ::-webkit-scrollbar-thumb {
281
+ background: #00ff00;
282
+ }
283
+ .cascader-wrap {
284
+ width: 100%;
285
+ height: 100%;
286
+ white-space: nowrap;
287
+ overflow: auto hidden;
288
+ background: #f4f2f6;
289
+ box-sizing: border-box;
290
+ font-size: 0;
291
+ .column {
292
+ display: inline-block;
293
+ height: 100%;
294
+ width: 50%;
295
+ overflow: hidden auto;
296
+ .column-item {
297
+ position: relative;
298
+ display: block;
299
+ height: 60rpx;
300
+ line-height: 60rpx;
301
+ padding: 0 calc(#{16rpx / 0.7 + 16rpx}) 0 32rpx;
302
+ font-size: 28rpx;
303
+ color: #333333;
304
+ &.active {
305
+ color: #2069f0;
306
+ background: #ffffff;
307
+ }
308
+ &.arrow::after {
309
+ display: block;
310
+ content: "";
311
+ width: 16rpx;
312
+ height: 16rpx;
313
+ border: 2rpx solid #333333;
314
+ border-top: none;
315
+ border-left: none;
316
+ position: absolute;
317
+ right: 8rpx;
318
+ top: 50%;
319
+ transform: translateY(-50%) rotate(-45deg);
320
+ }
321
+ }
322
+ }
323
+ }
324
+ </style>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sadais-piui",
3
- "version": "1.2.7beta35",
3
+ "version": "1.2.7beta36",
4
4
  "description": "piui组件库",
5
5
  "main": "index.js",
6
6
  "scripts": {