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: fix swiper's current dynamically update failed #1662

Open
wants to merge 3 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
5 changes: 3 additions & 2 deletions src/swiper/demos/current.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
</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">
跳转到第 {{ current + 2 >= 6 ? 1 : current + 2 }} 项
<t-button size="small" theme="primary" @click="current = (current + 1) % 5">
跳转到第 {{ 1 + ((current + 1) % 5) }} 项
</t-button>
</div>
</div>
Expand All @@ -33,6 +33,7 @@ const current = ref(0);

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

const handleClick = (value: number) => {
Expand Down
32 changes: 24 additions & 8 deletions src/swiper/swiper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const { prefix } = config;
export default defineComponent({
name: `${prefix}-swiper`,
props,
emits: ['change', 'update:current', 'update:modelValue', 'transitionenter', 'transitionleave'],
emits: ['change', 'update:value', 'update:modelValue', 'transitionenter', 'transitionleave'],
setup(props, context) {
const swiperClass = usePrefixClass('swiper');
const swiperNavClass = usePrefixClass('swiper-nav');
Expand All @@ -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 previousIndex = ref(currentIndex.value || 0);

const animating = ref(false);
const disabled = ref(false);
Expand All @@ -40,6 +41,7 @@ export default defineComponent({
const containerHeight = ref('auto');

const navigation = computed((): SwiperNavigation => props.navigation);
const swiperSource = ref<SwiperChangeSource>('autoplay');

const isBottomPagination = computed(() => {
let isShowSwiperNav = false;
Expand Down Expand Up @@ -78,20 +80,25 @@ export default defineComponent({
};

const move = (step: number, source: SwiperChangeSource, isReset = false) => {
moveByIndex(isReset ? step : (currentIndex.value as number) + step, source, step, isReset);
};

const moveByIndex = (index: number, source: SwiperChangeSource, step = 1, isReset = false) => {
animating.value = true;
processIndex(isReset ? step : (currentIndex.value as number) + step, source);
processIndex(index, source);
playMoveTransition(step, isReset);
};

const playMoveTransition = (step: number, isReset = false) => {
const moveDirection = !isVertical.value ? 'X' : 'Y';
const distance = root.value?.[isVertical.value ? 'offsetHeight' : 'offsetWidth'] ?? 0;

translateContainer.value = `translate${moveDirection}(${isReset ? 0 : -1 * distance * step}px)`;
};

const handleAnimationEnd = () => {
disabled.value = false;
animating.value = false;
translateContainer.value = 'translateX(0)';

updateItemPosition();
};

Expand Down Expand Up @@ -127,8 +134,11 @@ export default defineComponent({
if (index >= max) {
val = props.loop ? 0 : max - 1;
}

swiperSource.value = source;
previousIndex.value = val;
setCurrent(val);
context.emit('update:current', val);
context.emit('update:value', val);
context.emit('change', val, { source });
};

Expand Down Expand Up @@ -217,13 +227,19 @@ export default defineComponent({
};

watch(currentIndex, updateContainerHeight);

watch(
() => props.current,
() => {
(value, oldValue) => {
// v-model动态更新时不触发move逻辑
if (props.current === currentIndex.value) return;
if (value === previousIndex.value) return;
console.log(value, previousIndex.value);
stopAutoplay();
move(props.current - currentIndex.value, 'autoplay');
const max = items.value.length;
const diff = (value + max - previousIndex.value) % max;
// 检测step的方向
const step = diff <= items.value.length / 2 ? 1 : -1;
moveByIndex(value, swiperSource.value, step);
startAutoplay();
},
);
Expand Down