Skip to content

Commit dd17a40

Browse files
committed
fix: 修复 tour 组件无法根据 current 指定展示的步骤以及 change 事件得到的值有误
1 parent a415d0b commit dd17a40

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

src/packages/__VUE/tour/index.taro.vue

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<view :class="classes">
2+
<view class="nut-tour">
33
<view v-if="showTour" class="nut-tour-masked" @click="handleClickMask"></view>
44

55
<view v-for="(step, i) in steps" :key="i" style="height: 0">
@@ -42,17 +42,15 @@
4242
@click="changeStep('prev')"
4343
>
4444
{{ prevStepTxt }}
45-
</view
46-
>
45+
</view>
4746
</slot>
4847
<view
4948
v-if="steps.length - 1 == active"
5049
class="nut-tour-content-bottom-operate-btn active"
5150
@click="close"
5251
>
5352
{{ completeTxt }}
54-
</view
55-
>
53+
</view>
5654

5755
<slot name="next-step">
5856
<view
@@ -61,8 +59,7 @@
6159
@click="changeStep('next')"
6260
>
6361
{{ nextStepTxt }}
64-
</view
65-
>
62+
</view>
6663
</slot>
6764
</view>
6865
</view>
@@ -80,7 +77,7 @@
8077
</view>
8178
</template>
8279
<script lang="ts">
83-
import { computed, watch, ref, reactive, toRefs, PropType, onMounted } from 'vue'
80+
import { watch, ref, reactive, toRefs, PropType, onMounted, CSSProperties } from 'vue'
8481
import { PopoverLocation, PopoverTheme } from '../popover/type'
8582
import { createComponent } from '@/packages/utils/create'
8683
import { rectTaro, useTaroRectById } from '@/packages/utils/useTaroRect'
@@ -179,17 +176,12 @@ export default create({
179176
180177
let maskRect: rectTaro[] = []
181178
182-
let maskStyles = ref<any[]>([])
183-
184-
const classes = computed(() => {
185-
const prefixCls = 'nut-tour'
186-
return `${prefixCls}`
187-
})
179+
let maskStyles = ref<CSSProperties[]>([])
188180
189181
const maskStyle = (index: number) => {
190182
const { offset, maskWidth, maskHeight } = props
191183
192-
if (!maskRect[index]) return {}
184+
if (!maskRect[index]) return
193185
const { width, height, left, top } = maskRect[index]
194186
195187
const center = [left + width / 2, top + height / 2] // 中心点 【横,纵】
@@ -209,31 +201,26 @@ export default create({
209201
const current = state.active
210202
let next = current
211203
212-
if (type == 'next') {
213-
next = current + 1
214-
} else {
215-
next = current - 1
216-
}
204+
next = type == 'next' ? current + 1 : current - 1
217205
showPopup.value[current] = false
218206
219207
setTimeout(() => {
220-
showPopup.value[next] = true
221208
state.active = next
209+
emit('change', state.active)
210+
showPopup.value[state.active] = true
222211
}, 300)
223-
224-
emit('change', state.active)
225212
}
226213
227-
const getRootPosition = () => {
228-
props.steps.forEach(async (item, i) => {
229-
useTaroRectById(item.target).then(
230-
(rect: any) => {
231-
maskRect[i] = rect
232-
maskStyle(i)
233-
},
234-
() => {}
235-
)
236-
})
214+
const getRootPosition = async () => {
215+
for (const [index, step] of props.steps.entries()) {
216+
try {
217+
const rect = await useTaroRectById(step.target)
218+
maskRect[index] = rect as rectTaro
219+
maskStyle(index)
220+
} catch (error) {
221+
console.warn(`[NutUI] Failed to get rect for step ${index}:`, error)
222+
}
223+
}
237224
}
238225
239226
const close = () => {
@@ -248,33 +235,30 @@ export default create({
248235
}
249236
250237
onMounted(() => {
251-
setTimeout(() => {
252-
getRootPosition()
253-
}, 500)
238+
getRootPosition()
254239
})
255240
256241
watch(
257242
() => props.modelValue,
258-
(val) => {
259-
if (val) {
260-
state.active = 0
243+
(visible) => {
244+
if (visible) {
245+
state.active = props.type === 'step' ? Math.min(props.current, props.steps.length - 1) : 0
261246
getRootPosition()
262247
}
263-
state.showTour = val
264-
showPopup.value[state.active] = val
248+
state.showTour = visible
249+
showPopup.value[state.active] = visible
265250
}
266251
)
267252
268253
const refRandomId = Math.random().toString(36).slice(-8)
269254
270255
return {
271256
...toRefs(state),
272-
classes,
273257
maskStyle,
274258
changeStep,
275-
showPopup,
276259
close,
277260
handleClickMask,
261+
showPopup,
278262
maskStyles,
279263
refRandomId
280264
}

0 commit comments

Comments
 (0)