sadais-piui 1.2.7-beta113 → 1.2.7-beta115

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 (160) hide show
  1. package/.prettierrc +7 -7
  2. package/CHANGELOG.md +545 -545
  3. package/LICENSE +20 -20
  4. package/README.md +166 -166
  5. package/components/pi-action-sheet/index.vue +247 -247
  6. package/components/pi-badge/index.vue +146 -146
  7. package/components/pi-bottom-bar/index.vue +139 -139
  8. package/components/pi-bubble/index.vue +174 -174
  9. package/components/pi-button/index.vue +492 -492
  10. package/components/pi-calendar-input/index.vue +230 -230
  11. package/components/pi-calendar-panel/components/calendar-days/index.vue +410 -410
  12. package/components/pi-calendar-panel/index.vue +473 -473
  13. package/components/pi-canvas/index.vue +407 -407
  14. package/components/pi-card/index.vue +127 -127
  15. package/components/pi-cascader/index.vue +601 -601
  16. package/components/pi-checkbox/index.vue +363 -363
  17. package/components/pi-checkbox-group/index.vue +222 -222
  18. package/components/pi-circle-progress/index.vue +311 -311
  19. package/components/pi-code-input/index.vue +270 -270
  20. package/components/pi-collapse/index.vue +124 -124
  21. package/components/pi-collapse-item/index.vue +352 -352
  22. package/components/pi-count-down/index.vue +245 -245
  23. package/components/pi-count-up/index.vue +209 -209
  24. package/components/pi-date-picker-input/index.vue +219 -219
  25. package/components/pi-divider/index.vue +112 -112
  26. package/components/pi-dropdown/components/dropdown-tab.vue +71 -71
  27. package/components/pi-dropdown/index.vue +356 -356
  28. package/components/pi-dropdown-item/index.vue +252 -252
  29. package/components/pi-form/index.vue +273 -273
  30. package/components/pi-form-item/index.vue +603 -590
  31. package/components/pi-gap/index.vue +72 -72
  32. package/components/pi-grid/index.vue +92 -92
  33. package/components/pi-grid-item/index.vue +220 -220
  34. package/components/pi-icon/index.vue +147 -147
  35. package/components/pi-img/index.vue +266 -266
  36. package/components/pi-img-cropper/canvas.js +106 -106
  37. package/components/pi-img-cropper/index.vue +357 -357
  38. package/components/pi-img-cropper/touch.js +163 -163
  39. package/components/pi-img-cropper/vector.js +34 -34
  40. package/components/pi-input/index.vue +409 -401
  41. package/components/pi-line/index.vue +78 -78
  42. package/components/pi-line-progress/index.vue +198 -198
  43. package/components/pi-list/index.vue +115 -115
  44. package/components/pi-list-item/index.vue +335 -335
  45. package/components/pi-loading/index.vue +188 -188
  46. package/components/pi-marquee/index.vue +310 -310
  47. package/components/pi-marquee-item/index.vue +47 -47
  48. package/components/pi-mask/index.vue +198 -198
  49. package/components/pi-modal/index.vue +303 -303
  50. package/components/pi-navbar/index.vue +421 -421
  51. package/components/pi-notify/index.vue +239 -239
  52. package/components/pi-number-keyboard/index.vue +189 -189
  53. package/components/pi-opentype-button/index.vue +501 -501
  54. package/components/pi-pagination/index.vue +290 -290
  55. package/components/pi-picker/index.vue +317 -317
  56. package/components/pi-popup/index.vue +356 -356
  57. package/components/pi-popup-select/index.vue +300 -300
  58. package/components/pi-radio/index.vue +403 -403
  59. package/components/pi-radio-group/index.vue +300 -300
  60. package/components/pi-rate/index.vue +212 -212
  61. package/components/pi-regions-select/index.vue +349 -349
  62. package/components/pi-regions-select-input/index.vue +230 -230
  63. package/components/pi-search/index.vue +386 -386
  64. package/components/pi-section/index.vue +218 -218
  65. package/components/pi-select/index.vue +678 -678
  66. package/components/pi-select-input/index.vue +273 -273
  67. package/components/pi-skeleton/index.vue +280 -280
  68. package/components/pi-status-bar/index.vue +56 -56
  69. package/components/pi-stepper/index.vue +408 -408
  70. package/components/pi-steps/index.vue +378 -378
  71. package/components/pi-subsection/index.vue +403 -403
  72. package/components/pi-swiper/index.vue +344 -344
  73. package/components/pi-switch/index.vue +231 -231
  74. package/components/pi-table/index.vue +351 -351
  75. package/components/pi-tabs/index.vue +435 -435
  76. package/components/pi-tag/index.vue +488 -488
  77. package/components/pi-upload-img/index.vue +980 -980
  78. package/components/pi-waterfall/index.vue +130 -130
  79. package/config/actionSheet.js +24 -24
  80. package/config/badge.js +9 -9
  81. package/config/bottomBar.js +11 -11
  82. package/config/bubble.js +6 -6
  83. package/config/button.js +29 -29
  84. package/config/calendar.js +19 -19
  85. package/config/calendarInput.js +19 -19
  86. package/config/calendarPanel.js +27 -27
  87. package/config/card.js +8 -8
  88. package/config/cascader.js +22 -22
  89. package/config/checkbox.js +15 -15
  90. package/config/checkboxGroup.js +15 -15
  91. package/config/circleProgress.js +20 -20
  92. package/config/codeInput.js +13 -13
  93. package/config/collapse.js +7 -7
  94. package/config/collapseItem.js +11 -11
  95. package/config/countDown.js +12 -12
  96. package/config/countUp.js +13 -13
  97. package/config/datePickerInput.js +18 -18
  98. package/config/divider.js +10 -10
  99. package/config/dropdown.js +25 -25
  100. package/config/dropdownItem.js +26 -26
  101. package/config/form.js +29 -29
  102. package/config/formItem.js +27 -27
  103. package/config/gap.js +8 -8
  104. package/config/grid.js +9 -9
  105. package/config/gridItem.js +12 -12
  106. package/config/icon.js +11 -11
  107. package/config/img.js +21 -21
  108. package/config/imgCropper.js +26 -26
  109. package/config/index.js +28 -28
  110. package/config/input.js +37 -36
  111. package/config/line.js +8 -8
  112. package/config/lineProgress.js +14 -14
  113. package/config/list.js +12 -12
  114. package/config/listItem.js +25 -25
  115. package/config/loading.js +10 -10
  116. package/config/marquee.js +10 -10
  117. package/config/mask.js +12 -12
  118. package/config/modal.js +36 -36
  119. package/config/navbar.js +32 -32
  120. package/config/notify.js +13 -13
  121. package/config/numberKeyboard.js +21 -21
  122. package/config/pagination.js +10 -10
  123. package/config/picker.js +19 -19
  124. package/config/popup.js +23 -23
  125. package/config/popupSelect.js +35 -35
  126. package/config/radio.js +16 -16
  127. package/config/radioGroup.js +17 -17
  128. package/config/rate.js +15 -15
  129. package/config/regionsData.js +58 -58
  130. package/config/regionsSelect.js +19 -19
  131. package/config/regionsSelectInput.js +29 -29
  132. package/config/search.js +27 -27
  133. package/config/section.js +23 -23
  134. package/config/select.js +41 -41
  135. package/config/selectInput.js +29 -29
  136. package/config/skeleton.js +13 -13
  137. package/config/statusBar.js +5 -5
  138. package/config/stepper.js +19 -19
  139. package/config/steps.js +19 -19
  140. package/config/subsection.js +14 -14
  141. package/config/swiper.js +22 -22
  142. package/config/switch.js +13 -13
  143. package/config/table.js +8 -8
  144. package/config/tabs.js +23 -23
  145. package/config/tag.js +21 -21
  146. package/config/uploadImg.js +81 -81
  147. package/config/waterfall.js +6 -6
  148. package/index.js +41 -41
  149. package/mixin/component-custom.js +24 -24
  150. package/mixin/emitter.js +45 -45
  151. package/mixin/props-sync.js +65 -65
  152. package/mixin/value-sync.js +32 -32
  153. package/mixin/wx-code.js +30 -30
  154. package/package.json +28 -28
  155. package/plugins/async-validator.js +1511 -1511
  156. package/scss/animation.scss +260 -260
  157. package/scss/icon/icon.scss +595 -595
  158. package/scss/index.scss +5 -5
  159. package/scss/mixin.scss +47 -47
  160. package/tools/index.js +19 -19
@@ -1,403 +1,403 @@
1
- <template>
2
- <view
3
- ref="pi-subsection"
4
- class="pi-subsection"
5
- :class="[`pi-subsection--${mode}`, { round: round }, customClass]"
6
- :style="[customStyle, wrapperStyle]"
7
- >
8
- <view
9
- ref="pi-subsection__bar"
10
- class="pi-subsection__bar"
11
- :style="[barStyle]"
12
- :class="[barClass]"
13
- />
14
- <view
15
- v-for="(item, index) in items"
16
- :ref="`pi-subsection__item--${index}`"
17
- :key="index"
18
- class="pi-subsection__item"
19
- :class="[itemClass(index)]"
20
- :style="[itemStyle(index)]"
21
- @tap="handleSelectItem(index)"
22
- >
23
- <text class="pi-subsection__item__text" :style="[textStyle(index)]">{{ getText(item) }}</text>
24
- </view>
25
- </view>
26
- </template>
27
-
28
- <script>
29
- // #ifdef APP-NVUE
30
- const dom = uni.requireNativePlugin('dom')
31
- const animation = uni.requireNativePlugin('animation')
32
- // #endif
33
- import ValueSync from '../../mixin/value-sync'
34
- import { getConfig } from '../../config'
35
- const { subsection } = getConfig()
36
- /**
37
- * Subsection 分段器
38
- * @description 该分段器一般用于用户从几个选项中选择某一个的场景
39
- * @tutorial http://piui.sadais.com/docs/components/Subsection.html
40
- * @event {Function} change 分段器选项发生改变时触发 回调 index:选项的index索引值,从0开始
41
- * @example <pi-subsection v-model="current" :items="items" @change="sectionChange" />
42
- */
43
- export default {
44
- name: 'PiSubsection',
45
- // 混入v-model
46
- mixins: [ValueSync],
47
- props: {
48
- // 当前活动的tab的index
49
- value: {
50
- type: Number,
51
- // 0
52
- default() {
53
- return subsection.value
54
- }
55
- },
56
- // 自定义样式,对象形式
57
- customStyle: {
58
- type: Object,
59
- // `{}`
60
- default() {
61
- return subsection.customStyle
62
- }
63
- },
64
- // 自定义样式类,字符串形式
65
- customClass: {
66
- type: String,
67
- // ''
68
- default() {
69
- return subsection.customClass
70
- }
71
- },
72
- // tab的数据
73
- items: {
74
- type: Array,
75
- // []
76
- default() {
77
- return subsection.items
78
- }
79
- },
80
- // 激活时的颜色
81
- activeColor: {
82
- type: String,
83
- // #5a7dff
84
- default() {
85
- return subsection.activeColor
86
- }
87
- },
88
- // 未激活时的颜色
89
- inactiveColor: {
90
- type: String,
91
- // #303133
92
- default() {
93
- return subsection.inactiveColor
94
- }
95
- },
96
- // 模式选择,mode=button为按钮形式,mode=subsection时为分段模式
97
- mode: {
98
- type: String,
99
- // button
100
- default() {
101
- return subsection.mode
102
- }
103
- },
104
- // 圆角模式
105
- round: {
106
- type: Boolean,
107
- // false
108
- default() {
109
- return subsection.round
110
- }
111
- },
112
- // 字体大小,单位px
113
- fontSize: {
114
- type: [String, Number],
115
- // 24rpx
116
- default() {
117
- return subsection.fontSize
118
- }
119
- },
120
- // 激活选项的字体是否加粗
121
- bold: {
122
- type: Boolean,
123
- // true
124
- default() {
125
- return subsection.bold
126
- }
127
- },
128
- // 组件背景颜色,mode为button时有效
129
- bgColor: {
130
- type: String,
131
- // #eeeeef
132
- default() {
133
- return subsection.bgColor
134
- }
135
- },
136
- // 从`items`元素对象中读取的键名
137
- keyField: {
138
- type: String,
139
- // name
140
- default() {
141
- return subsection.keyField
142
- }
143
- }
144
- },
145
- data() {
146
- return {
147
- // 组件尺寸
148
- itemRect: {
149
- width: 0,
150
- height: 0
151
- }
152
- }
153
- },
154
- computed: {
155
- // 滑块指定class
156
- barClass() {
157
- // 默认按钮模式
158
- let className = 'pi-subsection--button__bar'
159
- // 分段器模式
160
- if (this.mode === 'subsection') {
161
- if (this.val === 0) {
162
- className = 'pi-subsection__bar--first'
163
- } else if (this.val > 0 && this.val < this.items.length - 1) {
164
- className = 'pi-subsection__bar--center'
165
- } else if (this.val === this.items.length - 1) {
166
- className = 'pi-subsection__bar--last'
167
- }
168
- }
169
- return className
170
- },
171
- wrapperStyle() {
172
- const style = {}
173
- // button模式时,设置背景色
174
- if (this.mode === 'button') {
175
- style.background = this.bgColor
176
- }
177
- return style
178
- },
179
- // 滑块的样式
180
- barStyle() {
181
- const style = {}
182
- style.width = `${this.itemRect.width}px`
183
- style.height = `${this.itemRect.height}px`
184
- // 通过translateX移动滑块,其移动的距离为索引*item的宽度
185
- // #ifndef APP-NVUE
186
- style.transform = `translateX(${this.val * this.itemRect.width}px)`
187
- // #endif
188
- if (this.mode === 'subsection') {
189
- // 在subsection模式下,需要动态设置滑块的圆角,因为移动滑块使用的是translateX,无法通过父元素设置overflow: hidden隐藏滑块的直角
190
- style.background = this.activeColor
191
- }
192
- // 按钮圆角模式 按钮模式生效
193
- if (this.round && this.mode === 'button') {
194
- style.borderRadius = `${this.itemRect.height}px`
195
- }
196
- return style
197
- },
198
- // 分段器item的样式
199
- itemStyle(index) {
200
- return index => {
201
- const style = {}
202
- if (this.mode === 'subsection') {
203
- // 设置border的样式
204
- style.borderColor = this.activeColor
205
- style.borderWidth = '2rpx'
206
- style.borderStyle = 'solid'
207
- if (this.round) {
208
- if (index === 0) {
209
- style.borderTopLeftRadius = `${this.itemRect.height}px`
210
- style.borderBottomLeftRadius = `${this.itemRect.height}px`
211
- } else if (index === this.items.length - 1) {
212
- // 最后一个
213
- style.borderTopRightRadius = `${this.itemRect.height}px`
214
- style.borderBottomRightRadius = `${this.itemRect.height}px`
215
- }
216
- }
217
- }
218
- return style
219
- }
220
- },
221
- // 分段器item的class
222
- itemClass(index) {
223
- return index => {
224
- let className = `pi-subsection__item--${index}`
225
- if (index < this.items.length - 1) {
226
- className += ' pi-subsection__item--no-border-right'
227
- } else if (index === 0) {
228
- className += ' pi-subsection__item--first'
229
- } else if (index === this.items.length - 1) {
230
- className += ' pi-subsection__item--last'
231
- }
232
- return className
233
- }
234
- },
235
- // 分段器文字颜色
236
- textStyle(index) {
237
- return index => {
238
- const style = {}
239
- style.fontWeight = this.bold && this.val === index ? 'bold' : 'normal'
240
- style.fontSize = this.$pi.common.addUnit(this.fontSize)
241
- // subsection模式下,激活时默认为白色的文字
242
- if (this.mode === 'subsection') {
243
- style.color = this.val === index ? '#fff' : this.inactiveColor
244
- } else {
245
- // button模式下,激活时文字颜色默认为activeColor
246
- style.color = this.val === index ? this.activeColor : this.inactiveColor
247
- }
248
- return style
249
- }
250
- }
251
- },
252
- watch: {
253
- items(newValue, oldValue) {
254
- this.init()
255
- },
256
- current: {
257
- immediate: true,
258
- handler(n) {
259
- // #ifdef APP-NVUE
260
- // 在安卓nvue上,如果通过translateX进行位移,到最后一个时,会导致右侧无法绘制圆角
261
- // 故用animation模块进行位移
262
- const ref = this.$refs?.['pi-subsection__bar']?.ref
263
- // 不存在ref的时候(理解为第一次初始化时,需要渲染dom,进行一定延时再获取ref),这里的100ms是经过测试得出的结果(某些安卓需要延时久一点),勿随意修改
264
- this.sleep(ref ? 0 : 100).then(() => {
265
- animation.transition(this.$refs['pi-subsection__bar'].ref, {
266
- styles: {
267
- transform: `translateX(${n * this.itemRect.width}px)`,
268
- transformOrigin: 'center center'
269
- },
270
- duration: 300
271
- })
272
- })
273
- // #endif
274
- }
275
- }
276
- },
277
- mounted() {
278
- this.init()
279
- },
280
- methods: {
281
- init() {
282
- this.sleep().then(() => this.getRect())
283
- },
284
- // 判断展示文本
285
- getText(item) {
286
- return typeof item === 'object' ? item[this.keyField] : item
287
- },
288
- // 获取组件的尺寸
289
- getRect() {
290
- // #ifndef APP-NVUE
291
- this.$pi.common.queryRect(this, '.pi-subsection__item--0', false).then(size => {
292
- this.itemRect = size
293
- })
294
- // #endif
295
- // #ifdef APP-NVUE
296
- const ref = this.$refs['pi-subsection__item--0'][0]
297
- ref &&
298
- dom.getComponentRect(ref, res => {
299
- this.itemRect = res.size
300
- })
301
- // #endif
302
- },
303
- /**
304
- * @description 进行延时,以达到可以简写代码的目的
305
- * @param {number} value 堵塞时间 单位ms 毫秒
306
- * @returns {Promise} 返回promise
307
- */
308
- sleep(value = 30) {
309
- return new Promise(resolve => {
310
- setTimeout(() => {
311
- resolve()
312
- }, value)
313
- })
314
- },
315
-
316
- handleSelectItem(index) {
317
- this.val = index
318
- this.handleEmitChange()
319
- }
320
- }
321
- }
322
- </script>
323
- <style lang="scss" scoped>
324
- .pi-subsection {
325
- display: flex;
326
- position: relative;
327
- overflow: hidden;
328
- /* #ifndef APP-NVUE */
329
- width: 100%;
330
- box-sizing: border-box;
331
- /* #endif */
332
- &.round {
333
- border-radius: 60rpx;
334
- }
335
- &--button {
336
- height: 64rpx;
337
- background-color: rgb(238, 238, 239);
338
- padding: 6rpx;
339
- border-radius: 6rpx;
340
- align-items: stretch;
341
- &__bar {
342
- background-color: #ffffff;
343
- border-radius: 6rpx;
344
- }
345
- }
346
- &--subsection {
347
- height: 60rpx;
348
- }
349
- &__bar {
350
- position: absolute;
351
- /* #ifndef APP-NVUE */
352
- transition-property: transform, color;
353
- transition-duration: 0.3s;
354
- transition-timing-function: ease-in-out;
355
- /* #endif */
356
- &--first {
357
- border-top-left-radius: 6rpx;
358
- border-bottom-left-radius: 6rpx;
359
- border-top-right-radius: 0px;
360
- border-bottom-right-radius: 0px;
361
- }
362
- &--center {
363
- border-top-left-radius: 0px;
364
- border-bottom-left-radius: 0px;
365
- border-top-right-radius: 0px;
366
- border-bottom-right-radius: 0px;
367
- }
368
- &--last {
369
- border-top-left-radius: 0px;
370
- border-bottom-left-radius: 0px;
371
- border-top-right-radius: 6rpx;
372
- border-bottom-right-radius: 6rpx;
373
- }
374
- }
375
- &__item {
376
- display: flex;
377
- flex: 1;
378
- justify-content: center;
379
- align-items: center;
380
- // vue环境下,需要设置相对定位,因为滑块为绝对定位,item需要在滑块的上面
381
- position: relative;
382
- &--no-border-right {
383
- border-right-width: 0 !important;
384
- }
385
- &--first {
386
- border-top-left-radius: 6rpx;
387
- border-bottom-left-radius: 6rpx;
388
- }
389
- &--last {
390
- border-top-right-radius: 6rpx;
391
- border-bottom-right-radius: 6rpx;
392
- }
393
- &__text {
394
- font-size: 24rpx;
395
- line-height: 24rpx;
396
- display: flex;
397
- align-items: center;
398
- transition-property: color;
399
- transition-duration: 0.3s;
400
- }
401
- }
402
- }
403
- </style>
1
+ <template>
2
+ <view
3
+ ref="pi-subsection"
4
+ class="pi-subsection"
5
+ :class="[`pi-subsection--${mode}`, { round: round }, customClass]"
6
+ :style="[customStyle, wrapperStyle]"
7
+ >
8
+ <view
9
+ ref="pi-subsection__bar"
10
+ class="pi-subsection__bar"
11
+ :style="[barStyle]"
12
+ :class="[barClass]"
13
+ />
14
+ <view
15
+ v-for="(item, index) in items"
16
+ :ref="`pi-subsection__item--${index}`"
17
+ :key="index"
18
+ class="pi-subsection__item"
19
+ :class="[itemClass(index)]"
20
+ :style="[itemStyle(index)]"
21
+ @tap="handleSelectItem(index)"
22
+ >
23
+ <text class="pi-subsection__item__text" :style="[textStyle(index)]">{{ getText(item) }}</text>
24
+ </view>
25
+ </view>
26
+ </template>
27
+
28
+ <script>
29
+ // #ifdef APP-NVUE
30
+ const dom = uni.requireNativePlugin('dom')
31
+ const animation = uni.requireNativePlugin('animation')
32
+ // #endif
33
+ import ValueSync from '../../mixin/value-sync'
34
+ import { getConfig } from '../../config'
35
+ const { subsection } = getConfig()
36
+ /**
37
+ * Subsection 分段器
38
+ * @description 该分段器一般用于用户从几个选项中选择某一个的场景
39
+ * @tutorial http://piui.sadais.com/docs/components/Subsection.html
40
+ * @event {Function} change 分段器选项发生改变时触发 回调 index:选项的index索引值,从0开始
41
+ * @example <pi-subsection v-model="current" :items="items" @change="sectionChange" />
42
+ */
43
+ export default {
44
+ name: 'PiSubsection',
45
+ // 混入v-model
46
+ mixins: [ValueSync],
47
+ props: {
48
+ // 当前活动的tab的index
49
+ value: {
50
+ type: Number,
51
+ // 0
52
+ default() {
53
+ return subsection.value
54
+ }
55
+ },
56
+ // 自定义样式,对象形式
57
+ customStyle: {
58
+ type: Object,
59
+ // `{}`
60
+ default() {
61
+ return subsection.customStyle
62
+ }
63
+ },
64
+ // 自定义样式类,字符串形式
65
+ customClass: {
66
+ type: String,
67
+ // ''
68
+ default() {
69
+ return subsection.customClass
70
+ }
71
+ },
72
+ // tab的数据
73
+ items: {
74
+ type: Array,
75
+ // []
76
+ default() {
77
+ return subsection.items
78
+ }
79
+ },
80
+ // 激活时的颜色
81
+ activeColor: {
82
+ type: String,
83
+ // #5a7dff
84
+ default() {
85
+ return subsection.activeColor
86
+ }
87
+ },
88
+ // 未激活时的颜色
89
+ inactiveColor: {
90
+ type: String,
91
+ // #303133
92
+ default() {
93
+ return subsection.inactiveColor
94
+ }
95
+ },
96
+ // 模式选择,mode=button为按钮形式,mode=subsection时为分段模式
97
+ mode: {
98
+ type: String,
99
+ // button
100
+ default() {
101
+ return subsection.mode
102
+ }
103
+ },
104
+ // 圆角模式
105
+ round: {
106
+ type: Boolean,
107
+ // false
108
+ default() {
109
+ return subsection.round
110
+ }
111
+ },
112
+ // 字体大小,单位px
113
+ fontSize: {
114
+ type: [String, Number],
115
+ // 24rpx
116
+ default() {
117
+ return subsection.fontSize
118
+ }
119
+ },
120
+ // 激活选项的字体是否加粗
121
+ bold: {
122
+ type: Boolean,
123
+ // true
124
+ default() {
125
+ return subsection.bold
126
+ }
127
+ },
128
+ // 组件背景颜色,mode为button时有效
129
+ bgColor: {
130
+ type: String,
131
+ // #eeeeef
132
+ default() {
133
+ return subsection.bgColor
134
+ }
135
+ },
136
+ // 从`items`元素对象中读取的键名
137
+ keyField: {
138
+ type: String,
139
+ // name
140
+ default() {
141
+ return subsection.keyField
142
+ }
143
+ }
144
+ },
145
+ data() {
146
+ return {
147
+ // 组件尺寸
148
+ itemRect: {
149
+ width: 0,
150
+ height: 0
151
+ }
152
+ }
153
+ },
154
+ computed: {
155
+ // 滑块指定class
156
+ barClass() {
157
+ // 默认按钮模式
158
+ let className = 'pi-subsection--button__bar'
159
+ // 分段器模式
160
+ if (this.mode === 'subsection') {
161
+ if (this.val === 0) {
162
+ className = 'pi-subsection__bar--first'
163
+ } else if (this.val > 0 && this.val < this.items.length - 1) {
164
+ className = 'pi-subsection__bar--center'
165
+ } else if (this.val === this.items.length - 1) {
166
+ className = 'pi-subsection__bar--last'
167
+ }
168
+ }
169
+ return className
170
+ },
171
+ wrapperStyle() {
172
+ const style = {}
173
+ // button模式时,设置背景色
174
+ if (this.mode === 'button') {
175
+ style.background = this.bgColor
176
+ }
177
+ return style
178
+ },
179
+ // 滑块的样式
180
+ barStyle() {
181
+ const style = {}
182
+ style.width = `${this.itemRect.width}px`
183
+ style.height = `${this.itemRect.height}px`
184
+ // 通过translateX移动滑块,其移动的距离为索引*item的宽度
185
+ // #ifndef APP-NVUE
186
+ style.transform = `translateX(${this.val * this.itemRect.width}px)`
187
+ // #endif
188
+ if (this.mode === 'subsection') {
189
+ // 在subsection模式下,需要动态设置滑块的圆角,因为移动滑块使用的是translateX,无法通过父元素设置overflow: hidden隐藏滑块的直角
190
+ style.background = this.activeColor
191
+ }
192
+ // 按钮圆角模式 按钮模式生效
193
+ if (this.round && this.mode === 'button') {
194
+ style.borderRadius = `${this.itemRect.height}px`
195
+ }
196
+ return style
197
+ },
198
+ // 分段器item的样式
199
+ itemStyle(index) {
200
+ return index => {
201
+ const style = {}
202
+ if (this.mode === 'subsection') {
203
+ // 设置border的样式
204
+ style.borderColor = this.activeColor
205
+ style.borderWidth = '2rpx'
206
+ style.borderStyle = 'solid'
207
+ if (this.round) {
208
+ if (index === 0) {
209
+ style.borderTopLeftRadius = `${this.itemRect.height}px`
210
+ style.borderBottomLeftRadius = `${this.itemRect.height}px`
211
+ } else if (index === this.items.length - 1) {
212
+ // 最后一个
213
+ style.borderTopRightRadius = `${this.itemRect.height}px`
214
+ style.borderBottomRightRadius = `${this.itemRect.height}px`
215
+ }
216
+ }
217
+ }
218
+ return style
219
+ }
220
+ },
221
+ // 分段器item的class
222
+ itemClass(index) {
223
+ return index => {
224
+ let className = `pi-subsection__item--${index}`
225
+ if (index < this.items.length - 1) {
226
+ className += ' pi-subsection__item--no-border-right'
227
+ } else if (index === 0) {
228
+ className += ' pi-subsection__item--first'
229
+ } else if (index === this.items.length - 1) {
230
+ className += ' pi-subsection__item--last'
231
+ }
232
+ return className
233
+ }
234
+ },
235
+ // 分段器文字颜色
236
+ textStyle(index) {
237
+ return index => {
238
+ const style = {}
239
+ style.fontWeight = this.bold && this.val === index ? 'bold' : 'normal'
240
+ style.fontSize = this.$pi.common.addUnit(this.fontSize)
241
+ // subsection模式下,激活时默认为白色的文字
242
+ if (this.mode === 'subsection') {
243
+ style.color = this.val === index ? '#fff' : this.inactiveColor
244
+ } else {
245
+ // button模式下,激活时文字颜色默认为activeColor
246
+ style.color = this.val === index ? this.activeColor : this.inactiveColor
247
+ }
248
+ return style
249
+ }
250
+ }
251
+ },
252
+ watch: {
253
+ items(newValue, oldValue) {
254
+ this.init()
255
+ },
256
+ current: {
257
+ immediate: true,
258
+ handler(n) {
259
+ // #ifdef APP-NVUE
260
+ // 在安卓nvue上,如果通过translateX进行位移,到最后一个时,会导致右侧无法绘制圆角
261
+ // 故用animation模块进行位移
262
+ const ref = this.$refs?.['pi-subsection__bar']?.ref
263
+ // 不存在ref的时候(理解为第一次初始化时,需要渲染dom,进行一定延时再获取ref),这里的100ms是经过测试得出的结果(某些安卓需要延时久一点),勿随意修改
264
+ this.sleep(ref ? 0 : 100).then(() => {
265
+ animation.transition(this.$refs['pi-subsection__bar'].ref, {
266
+ styles: {
267
+ transform: `translateX(${n * this.itemRect.width}px)`,
268
+ transformOrigin: 'center center'
269
+ },
270
+ duration: 300
271
+ })
272
+ })
273
+ // #endif
274
+ }
275
+ }
276
+ },
277
+ mounted() {
278
+ this.init()
279
+ },
280
+ methods: {
281
+ init() {
282
+ this.sleep().then(() => this.getRect())
283
+ },
284
+ // 判断展示文本
285
+ getText(item) {
286
+ return typeof item === 'object' ? item[this.keyField] : item
287
+ },
288
+ // 获取组件的尺寸
289
+ getRect() {
290
+ // #ifndef APP-NVUE
291
+ this.$pi.common.queryRect(this, '.pi-subsection__item--0', false).then(size => {
292
+ this.itemRect = size
293
+ })
294
+ // #endif
295
+ // #ifdef APP-NVUE
296
+ const ref = this.$refs['pi-subsection__item--0'][0]
297
+ ref &&
298
+ dom.getComponentRect(ref, res => {
299
+ this.itemRect = res.size
300
+ })
301
+ // #endif
302
+ },
303
+ /**
304
+ * @description 进行延时,以达到可以简写代码的目的
305
+ * @param {number} value 堵塞时间 单位ms 毫秒
306
+ * @returns {Promise} 返回promise
307
+ */
308
+ sleep(value = 30) {
309
+ return new Promise(resolve => {
310
+ setTimeout(() => {
311
+ resolve()
312
+ }, value)
313
+ })
314
+ },
315
+
316
+ handleSelectItem(index) {
317
+ this.val = index
318
+ this.handleEmitChange()
319
+ }
320
+ }
321
+ }
322
+ </script>
323
+ <style lang="scss" scoped>
324
+ .pi-subsection {
325
+ display: flex;
326
+ position: relative;
327
+ overflow: hidden;
328
+ /* #ifndef APP-NVUE */
329
+ width: 100%;
330
+ box-sizing: border-box;
331
+ /* #endif */
332
+ &.round {
333
+ border-radius: 60rpx;
334
+ }
335
+ &--button {
336
+ height: 64rpx;
337
+ background-color: rgb(238, 238, 239);
338
+ padding: 6rpx;
339
+ border-radius: 6rpx;
340
+ align-items: stretch;
341
+ &__bar {
342
+ background-color: #ffffff;
343
+ border-radius: 6rpx;
344
+ }
345
+ }
346
+ &--subsection {
347
+ height: 60rpx;
348
+ }
349
+ &__bar {
350
+ position: absolute;
351
+ /* #ifndef APP-NVUE */
352
+ transition-property: transform, color;
353
+ transition-duration: 0.3s;
354
+ transition-timing-function: ease-in-out;
355
+ /* #endif */
356
+ &--first {
357
+ border-top-left-radius: 6rpx;
358
+ border-bottom-left-radius: 6rpx;
359
+ border-top-right-radius: 0px;
360
+ border-bottom-right-radius: 0px;
361
+ }
362
+ &--center {
363
+ border-top-left-radius: 0px;
364
+ border-bottom-left-radius: 0px;
365
+ border-top-right-radius: 0px;
366
+ border-bottom-right-radius: 0px;
367
+ }
368
+ &--last {
369
+ border-top-left-radius: 0px;
370
+ border-bottom-left-radius: 0px;
371
+ border-top-right-radius: 6rpx;
372
+ border-bottom-right-radius: 6rpx;
373
+ }
374
+ }
375
+ &__item {
376
+ display: flex;
377
+ flex: 1;
378
+ justify-content: center;
379
+ align-items: center;
380
+ // vue环境下,需要设置相对定位,因为滑块为绝对定位,item需要在滑块的上面
381
+ position: relative;
382
+ &--no-border-right {
383
+ border-right-width: 0 !important;
384
+ }
385
+ &--first {
386
+ border-top-left-radius: 6rpx;
387
+ border-bottom-left-radius: 6rpx;
388
+ }
389
+ &--last {
390
+ border-top-right-radius: 6rpx;
391
+ border-bottom-right-radius: 6rpx;
392
+ }
393
+ &__text {
394
+ font-size: 24rpx;
395
+ line-height: 24rpx;
396
+ display: flex;
397
+ align-items: center;
398
+ transition-property: color;
399
+ transition-duration: 0.3s;
400
+ }
401
+ }
402
+ }
403
+ </style>