Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(swiper): 修复current受控时表现 #1661

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/swiper/demos/current.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</t-swiper-item>
</t-swiper>
<div class="tdesign-demo-block-row">
<t-button size="small" theme="primary" @click="current = current + 2 > 5 ? 0 : current + 1">
<t-button size="small" theme="primary" @click="changeCurrent">
跳转到第 {{ current + 2 >= 6 ? 1 : current + 2 }} 项
</t-button>
</div>
Expand All @@ -33,10 +33,16 @@ const current = ref(0);

const handleChange = (index: number, context: any) => {
console.log('基础示例,页数变化到》》》', index, context);
current.value = index;
};

const handleClick = (value: number) => {
console.log('click: ', value);
current.value = value;
};

const changeCurrent = () => {
current.value = current.value + 2 > 5 ? 0 : current.value + 1;
};
</script>

Expand Down
19 changes: 13 additions & 6 deletions src/swiper/swiper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default defineComponent({
const { current: value, modelValue } = toRefs(props);
const [currentIndex, setCurrent] = useVModel(value, modelValue, props.defaultCurrent);
const swiperContainer = ref<HTMLElement | null>(null);
const previous = ref(currentIndex.value ?? 0);

const animating = ref(false);
const disabled = ref(false);
Expand Down Expand Up @@ -77,9 +78,10 @@ export default defineComponent({
props.onClick?.(currentIndex.value ?? 0);
};

const move = (step: number, source: SwiperChangeSource, isReset = false) => {
const move = (step: number, source: SwiperChangeSource, isReset = false, targetValue?: number) => {
animating.value = true;
processIndex(isReset ? step : (currentIndex.value as number) + step, source);
const innerTargetValue = targetValue ?? (isReset ? step : currentIndex.value + step);
processIndex(innerTargetValue, source);

const moveDirection = !isVertical.value ? 'X' : 'Y';
const distance = root.value?.[isVertical.value ? 'offsetHeight' : 'offsetWidth'] ?? 0;
Expand Down Expand Up @@ -117,6 +119,11 @@ export default defineComponent({
move(1, source);
};

const innerSetCurrent = (val: number) => {
setCurrent(val);
previous.value = val;
};

const processIndex = (index: number, source: SwiperChangeSource) => {
const max = items.value.length;
let val = index;
Expand All @@ -127,7 +134,7 @@ export default defineComponent({
if (index >= max) {
val = props.loop ? 0 : max - 1;
}
setCurrent(val);
innerSetCurrent(val);
context.emit('update:current', val);
context.emit('change', val, { source });
};
Expand Down Expand Up @@ -219,11 +226,11 @@ export default defineComponent({
watch(currentIndex, updateContainerHeight);
watch(
() => props.current,
() => {
(val, oldVal) => {
// v-model动态更新时不触发move逻辑
if (props.current === currentIndex.value) return;
if (val === previous.value) return;
stopAutoplay();
move(props.current - currentIndex.value, 'autoplay');
move(val - oldVal, 'autoplay', false, val);
startAutoplay();
},
);
Expand Down