@tuya-miniapp/smart-ui 2.6.4-beta-8 → 2.6.4-beta-9

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,13 +1,4 @@
1
1
  "use strict";
2
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
- if (ar || !(i in from)) {
5
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
- ar[i] = from[i];
7
- }
8
- }
9
- return to.concat(ar || Array.prototype.slice.call(from));
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
@@ -16,6 +7,7 @@ var component_1 = require("../common/component");
16
7
  var utils_1 = require("../common/utils");
17
8
  var validator_1 = require("../common/validator");
18
9
  var ty_1 = __importDefault(require("../common/ty"));
10
+ var DEFAULT_DURATION = 400;
19
11
  var compIdList = [];
20
12
  var getId = function () {
21
13
  var id = 'smart-picker-column-' + compIdList.length;
@@ -40,8 +32,8 @@ var getId = function () {
40
32
  observer: function (value) {
41
33
  if (!this.data.isInit)
42
34
  return;
43
- this.updateViewOptions();
44
35
  this.updateUint(value);
36
+ this.checkIndexUpdateList();
45
37
  },
46
38
  },
47
39
  defaultIndex: {
@@ -50,19 +42,18 @@ var getId = function () {
50
42
  },
51
43
  changeAnimation: {
52
44
  type: Boolean,
53
- value: true,
45
+ value: false,
54
46
  },
55
47
  fontStyle: {
56
48
  type: String,
57
49
  value: '',
58
50
  },
59
51
  activeIndex: {
60
- type: Number,
61
- value: -1,
52
+ type: null,
62
53
  observer: function () {
63
54
  if (!this.data.isInit)
64
55
  return;
65
- this.updateViewOptions();
56
+ this.checkIndexUpdateList();
66
57
  },
67
58
  },
68
59
  unit: {
@@ -79,83 +70,75 @@ var getId = function () {
79
70
  },
80
71
  },
81
72
  data: {
82
- startY: 0,
83
- offset: 0,
84
- duration: 0,
85
- startOffset: 0,
86
- optionsV: [],
87
- currentIndex: -1,
88
- renderNum: 0,
89
- renderStart: 0,
90
- animate: false,
91
- playing: false,
92
73
  isInit: false,
93
74
  maxText: '',
94
- instanceId: '',
95
- timer: null,
96
- preOffsetList: [],
97
- viewOptions: [],
75
+ optionsVIndexList: [],
76
+ offsetActiveIndex: 0,
77
+ endTimer: null,
78
+ offsetList: [],
79
+ moving: false,
80
+ movingDirection: 'down',
81
+ startY: 0,
82
+ offsetting: 0,
83
+ animationIndex: 0,
84
+ isDestroy: false,
98
85
  },
99
86
  created: function () {
100
87
  this.setData({
101
88
  instanceId: getId(),
102
89
  });
103
- this.checkIndex();
104
- this.updateViewOptions();
105
- var options = this.data.options;
106
- this.updateUint(options);
90
+ this.updateUint(this.data.options);
91
+ this.checkIndexUpdateList();
107
92
  this.setData({
108
93
  isInit: true,
109
94
  });
110
95
  },
96
+ destroyed: function () {
97
+ this.setData({
98
+ isDestroy: true,
99
+ });
100
+ },
111
101
  methods: {
112
- checkIndex: function () {
113
- var activeIndex = this.data.activeIndex;
114
- var index = this.adjustIndex(activeIndex);
115
- if (activeIndex === index)
116
- return;
102
+ checkIndexUpdateList: function () {
103
+ var _a = this.data, activeIndex = _a.activeIndex, defaultIndex = _a.defaultIndex;
104
+ var count = this.data.options.length;
105
+ var currIndex = activeIndex !== null ? activeIndex : defaultIndex;
106
+ var animationIndex = this.getAnimationIndex(currIndex);
107
+ animationIndex = this.adjustIndex(animationIndex);
108
+ var currActiveIndex = this.data.loop ? ((animationIndex + 1) % count) - 1 : animationIndex;
109
+ if (currActiveIndex < 0) {
110
+ currActiveIndex += count;
111
+ }
112
+ var optionsVIndexList = this.getVisibleOptions(animationIndex);
117
113
  this.setData({
118
- activeIndex: index,
114
+ activeIndex: currActiveIndex,
115
+ animationIndex: animationIndex,
116
+ optionsVIndexList: optionsVIndexList,
119
117
  });
118
+ // if (currActiveIndex !== activeIndex) {
119
+ // this.$emit('change', currActiveIndex);
120
+ // }
120
121
  },
121
- adjustIndex: function (index) {
122
- var data = this.data;
123
- var count = this.data.options.length;
124
- index = (0, utils_1.range)(index, 0, count);
125
- for (var i = index; i < count; i++) {
126
- if (!this.isDisabled(data.options[i]))
127
- return i;
128
- }
129
- for (var i = index - 1; i >= 0; i--) {
130
- if (!this.isDisabled(data.options[i]))
131
- return i;
122
+ getAnimationIndex: function (activeIndex) {
123
+ var animationIndex = this.data.animationIndex;
124
+ var length = this.data.options.length || 1;
125
+ if (this.data.loop) {
126
+ var newAnimationIndex = this.getNewAnimationIndex(animationIndex, activeIndex, length, this.data.loop);
127
+ return newAnimationIndex;
132
128
  }
129
+ return activeIndex;
133
130
  },
134
- isDisabled: function (option) {
135
- return (0, validator_1.isObj)(option) && option.disabled;
136
- },
137
- updateViewOptions: function () {
138
- var currActiveIndex = this.data.activeIndex < 0 ? 0 : this.data.activeIndex;
139
- var partNum = Math.floor(currActiveIndex / 10);
140
- var lastNum = this.data.activeIndex - partNum * 10;
141
- if (lastNum < 5 && partNum > 0) {
142
- partNum -= 1;
143
- }
144
- var part2Times = Math.floor(partNum / 2);
145
- var part2Percent = partNum % 2;
146
- var onePartOffset = part2Percent + part2Times;
147
- var twoPartOffset = part2Times;
148
- var isReverse = onePartOffset > twoPartOffset;
149
- var startPart = twoPartOffset + onePartOffset;
150
- var viewIndexList = !isReverse
151
- ? (0, utils_1.generateRangeArray)(startPart * 10, startPart * 10 + 20)
152
- : __spreadArray(__spreadArray([], (0, utils_1.generateRangeArray)(startPart * 10 + 10, startPart * 10 + 20), true), (0, utils_1.generateRangeArray)(startPart * 10, startPart * 10 + 10), true);
153
- this.setData({
154
- viewOptions: viewIndexList,
155
- });
131
+ getCount: function () {
132
+ return this.data.options.length;
156
133
  },
157
134
  vibrateShort: function (count, time) {
158
- var _this = this;
135
+ if (time === void 0) { time = DEFAULT_DURATION; }
136
+ if (this.data.vibrateTimer) {
137
+ clearInterval(this.data.vibrateTimer);
138
+ this.setData({
139
+ vibrateTimer: null,
140
+ });
141
+ }
159
142
  if (!count) {
160
143
  ty_1.default.vibrateShort({ type: 'light' });
161
144
  return;
@@ -167,8 +150,11 @@ var getId = function () {
167
150
  return;
168
151
  }
169
152
  has++;
170
- _this.vibrateShort();
171
- }, (time || this.data.animationTime) / count - 20);
153
+ ty_1.default.vibrateShort({ type: 'light' });
154
+ }, time / count - 20);
155
+ this.setData({
156
+ vibrateTimer: timer,
157
+ });
172
158
  },
173
159
  updateUint: function (options) {
174
160
  var _a = this.data, unit = _a.unit, valueKey = _a.valueKey;
@@ -184,6 +170,115 @@ var getId = function () {
184
170
  this.setData({ maxText: maxText_1 });
185
171
  }
186
172
  },
173
+ getVisibleOptions: function (currentIndex) {
174
+ var animationIndex = Math.round(currentIndex !== undefined ? currentIndex : this.data.animationIndex);
175
+ var vOptionLength = this.data.visibleItemCount * 4 - 2;
176
+ var rotateAngle = 360 / vOptionLength;
177
+ var partCount = Math.floor(this.data.visibleItemCount / 2) + 3;
178
+ var newArr = new Array(vOptionLength).fill('');
179
+ var newValueArr = new Array(partCount * 2 + 1).fill('');
180
+ if (this.data.loop) {
181
+ // 循环模式:根据 options 首尾填充 newValueArr 数组
182
+ var optionsLength_1 = this.data.options.length;
183
+ if (optionsLength_1 === 0) {
184
+ // 如果没有选项,填充空值
185
+ newValueArr.fill('');
186
+ }
187
+ else {
188
+ newValueArr.forEach(function (item, index) {
189
+ // 计算相对于中心的偏移量
190
+ var offset = index - partCount;
191
+ // 计算目标索引,支持循环
192
+ var targetAnimationIndex = animationIndex + offset;
193
+ var targetIndex = ((targetAnimationIndex + 1) % optionsLength_1) - 1;
194
+ if (targetIndex < 0) {
195
+ targetIndex += optionsLength_1;
196
+ }
197
+ newValueArr[index] = targetIndex;
198
+ });
199
+ }
200
+ }
201
+ else {
202
+ if (animationIndex < 0) {
203
+ animationIndex = 0;
204
+ }
205
+ if (animationIndex > this.data.options.length - 1) {
206
+ animationIndex = this.data.options.length - 1;
207
+ }
208
+ newValueArr.forEach(function (item, index) {
209
+ var valueIndex = animationIndex - partCount + index >= 0
210
+ ? animationIndex - partCount + index
211
+ : undefined;
212
+ if (valueIndex === undefined) {
213
+ return;
214
+ }
215
+ newValueArr[index] = valueIndex;
216
+ });
217
+ }
218
+ var rotate = (animationIndex * rotateAngle) % 360;
219
+ if (rotate < 0) {
220
+ rotate += 360;
221
+ }
222
+ var rotateIndex = Math.round(rotate / rotateAngle);
223
+ // 环形结构填充:以rotateIndex为中心,向两边扩展填充newValueArr
224
+ var centerIndex = rotateIndex; // 中心位置
225
+ var halfLength = Math.floor(newValueArr.length / 2); // newValueArr的一半长度
226
+ // 从中心位置开始,向两边填充
227
+ for (var i = 0; i < newValueArr.length; i++) {
228
+ var targetIndex = (centerIndex - halfLength + i + vOptionLength) % vOptionLength; // 确保索引在0-17范围内
229
+ newArr[targetIndex] = newValueArr[i];
230
+ }
231
+ return newArr;
232
+ },
233
+ getNewAnimationIndex: function (animationIndex, activeIndex, length, loop) {
234
+ var curOptionsNewIndex = Math.floor((animationIndex + 1) / length) * length + activeIndex;
235
+ var preOptionsNewIndex = curOptionsNewIndex - length;
236
+ var afterOptionsNewIndex = curOptionsNewIndex + length;
237
+ var newAnimationIndex = !loop
238
+ ? activeIndex
239
+ : Math.abs(preOptionsNewIndex - animationIndex) >
240
+ Math.abs(curOptionsNewIndex - animationIndex)
241
+ ? Math.abs(curOptionsNewIndex - animationIndex) >
242
+ Math.abs(afterOptionsNewIndex - animationIndex)
243
+ ? afterOptionsNewIndex
244
+ : curOptionsNewIndex
245
+ : Math.abs(preOptionsNewIndex - animationIndex) >
246
+ Math.abs(afterOptionsNewIndex - animationIndex)
247
+ ? afterOptionsNewIndex
248
+ : preOptionsNewIndex;
249
+ return newAnimationIndex;
250
+ },
251
+ adjustIndex: function (index) {
252
+ var data = this.data;
253
+ var count = this.getCount();
254
+ if (this.data.loop) {
255
+ for (var i = 0; i < count; i++) {
256
+ var targetIndex = index + i;
257
+ var optionIndex = ((targetIndex + 1) % count) - 1;
258
+ if (optionIndex < 0) {
259
+ optionIndex += count;
260
+ }
261
+ if (!this.isDisabled(data.options[optionIndex]) &&
262
+ data.options[optionIndex] !== undefined) {
263
+ return targetIndex;
264
+ }
265
+ }
266
+ return 0;
267
+ }
268
+ var activeIndex = (0, utils_1.range)(index, 0, count);
269
+ for (var i = activeIndex; i < count; i++) {
270
+ if (!this.isDisabled(data.options[i]) && data.options[i] !== undefined)
271
+ return i;
272
+ }
273
+ for (var i = activeIndex - 1; i >= 0; i--) {
274
+ if (!this.isDisabled(data.options[i]) && data.options[i] !== undefined)
275
+ return i;
276
+ }
277
+ return 0;
278
+ },
279
+ isDisabled: function (option) {
280
+ return (0, validator_1.isObj)(option) && option.disabled;
281
+ },
187
282
  getOptionText: function (option) {
188
283
  var data = this.data;
189
284
  return (0, validator_1.isObj)(option) && data.valueKey in option ? option[data.valueKey] : option;
@@ -198,29 +293,43 @@ var getId = function () {
198
293
  return Promise.resolve();
199
294
  },
200
295
  setIndex: function (index) {
296
+ var activeIndex = ((index + 1) % this.data.options.length) - 1;
297
+ if (activeIndex < 0) {
298
+ activeIndex += this.data.options.length;
299
+ }
201
300
  this.setData({
202
- activeIndex: index,
301
+ activeIndex: activeIndex,
302
+ animationIndex: index,
203
303
  });
204
304
  },
205
305
  getValue: function () {
206
306
  var data = this.data;
207
- return data.options[data.activeIndex < 0 ? 0 : data.activeIndex];
208
- },
209
- viewOptionsChange: function (list) {
210
- this.setData({
211
- viewOptions: list,
212
- });
307
+ return (0, validator_1.isObj)(data.options[data.activeIndex])
308
+ ? data.options[data.activeIndex][data.valueKey]
309
+ : data.options[data.activeIndex];
213
310
  },
214
311
  activeIndexChange: function (index) {
312
+ var activeIndex = ((index + 1) % this.data.options.length) - 1;
313
+ if (activeIndex < 0) {
314
+ activeIndex += this.data.options.length;
315
+ }
316
+ var isSame = activeIndex === this.data.activeIndex;
215
317
  this.setData({
216
- activeIndex: index,
318
+ activeIndex: activeIndex,
319
+ animationIndex: index,
217
320
  });
218
- this.$emit('change', index);
321
+ !isSame && this.$emit('change', index);
219
322
  },
220
323
  animationIndexChange: function (index) {
221
324
  this.setData({
222
325
  animationIndex: index,
223
326
  });
224
327
  },
328
+ animationStart: function () {
329
+ this.$emit('animation-start');
330
+ },
331
+ animationEnd: function () {
332
+ this.$emit('animation-end');
333
+ },
225
334
  },
226
335
  });
@@ -6,6 +6,8 @@
6
6
  style="{{ computed.rootStyle({ itemHeight, visibleItemCount }) }}"
7
7
  instanceId="{{instanceId}}"
8
8
  id="{{instanceId}}"
9
+ data-isdestroy="{{isDestroy}}"
10
+ data-changeanimation="{{changeAnimation}}"
9
11
  data-options="{{options}}"
10
12
  data-valuekey="{{valueKey}}"
11
13
  data-itemheight="{{itemHeight}}"
@@ -13,13 +15,16 @@
13
15
  data-activeindex="{{activeIndex}}"
14
16
  data-loop="{{loop}}"
15
17
  data-animationtime="{{animationTime}}"
18
+ isdestroy="{{isDestroy}}"
16
19
  options="{{options}}"
17
20
  activeindex="{{activeIndex}}"
21
+ changeanimation="{{changeAnimation}}"
18
22
  loop="{{loop}}"
19
23
  valuekey="{{valueKey}}"
20
24
  itemheight="{{itemHeight}}"
21
25
  visibleitemcount="{{visibleItemCount}}"
22
26
  animationtime="{{animationTime}}"
27
+ change:isdestroy="{{computed.updateValue(instanceId, 'isDestroy')}}"
23
28
  change:options="{{computed.updateValue(instanceId, 'options')}}"
24
29
  change:valuekey="{{computed.updateValue(instanceId,'valueKey')}}"
25
30
  change:itemheight="{{computed.updateValue(instanceId,'itemHeight')}}"
@@ -33,42 +38,16 @@
33
38
  bind:touchend="{{computed.touchEnd(instanceId)}}"
34
39
  bind:touchcancel="{{computed.touchEnd(instanceId)}}"
35
40
  >
36
- <view
37
- id="options"
38
- class="smart-picker-column__offset"
39
- style="height: {{ itemHeight * options.length }}px;{{ computed.wrapperStyle({ offset, itemHeight, visibleItemCount, animate, duration, renderStart, unit, loop }) }}"
40
- >
41
- <view class="smart-picker-column__visual">
42
- <view class="smart-picker-column__visual__item_1">
43
- <view
44
- wx:for="{{ computed.sliceArray(viewOptions, 0, 10) }}"
45
- wx:key="index"
46
- data-index="{{item}}"
47
- style="{{computed.wrapperItemStyle({ itemHeight, animationIndex, index: item, visibleItemCount })}}"
48
- class="smart-picker-column__item_{{index}} {{ computed.wrapperItemClass({ index: item, animationIndex, options })}}"
49
- bind:tap="{{computed.tapItem(instanceId)}}"
50
- >
51
- <view
52
- class="smart-picker-column__item__text"
53
- style="{{computed.wrapperItemTextStyle({ fontStyle, activeStyle, index: item, animationIndex })}}"
54
- ></view>
55
- </view>
56
- </view>
57
- <view class="smart-picker-column__visual__item_2">
58
- <view
59
- wx:for="{{ computed.sliceArray(viewOptions, 10) }}"
60
- wx:key="index"
61
- data-index="{{item}}"
62
- style="{{computed.wrapperItemStyle({ itemHeight, fontStyle, activeStyle: item === currentIndex ? activeStyle : '', animationIndex, index: item, visibleItemCount })}}"
63
- class="smart-picker-column__item_{{index + 10}} {{ computed.wrapperItemClass({ index: item, animationIndex, options })}}"
64
- bind:tap="{{computed.tapItem(instanceId)}}"
65
- >
66
- <view
67
- class="smart-picker-column__item__text"
68
- style="{{computed.wrapperItemTextStyle({ fontStyle, activeStyle, index: item, animationIndex })}}"
69
- ></view>
70
- </view>
71
- </view>
41
+ <view class="smart-picker-column__offset" style="{{ computed.wrapperStyle({ animationIndex, itemHeight, animationTime, unit, moving, optionsVIndexList, options, valueKey, visibleItemCount, loop, changeAnimation }) }}" id="options">
42
+ <view
43
+ wx:for="{{ optionsVIndexList }}"
44
+ wx:for-item="optionIndex"
45
+ wx:key="index"
46
+ data-index="{{index}}"
47
+ style="{{computed.wrapperItemStyle({ itemHeight, index, visibleItemCount, fontStyle, activeStyle, instanceId, index, optionsVIndexList, animationIndex, options })}}"
48
+ class="smart-picker-column__item_{{index}} smart-ellipsis {{ computed.wrapperItemClass({ index, animationIndex, optionsVIndexList, instanceId, options })}}"
49
+ bind:tap="{{computed.tapItem(instanceId)}}"
50
+ >
72
51
  </view>
73
52
  </view>
74
53
  <view wx:if="{{unit}}" class="smart-picker-column__unit" style="height: {{ itemHeight }}px">