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(Indexes): fix IndexesAnchor invalid when indexList is async data #1622

Merged
merged 1 commit into from
Oct 16, 2024
Merged
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
3 changes: 2 additions & 1 deletion src/indexes/__test__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,9 @@ describe('Indexes', () => {
});
await wrapper.find(`.${indexesClass}__sidebar-item`).trigger('click');
expect(selectFn).toBeCalledWith(list[0].index);
expect(changeFn).toBeCalledWith(list[0].index);

const items = wrapper.findAll(`.${indexesClass}__sidebar-item`);
const items = await wrapper.findAll(`.${indexesClass}__sidebar-item`);
await items[1].trigger('click');
expect(selectFn).toBeCalledWith(list[1].index);
expect(changeFn).toBeCalledWith(list[1].index);
Expand Down
23 changes: 15 additions & 8 deletions src/indexes/indexes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
computed,
watch,
ComponentPublicInstance,
nextTick,
} from 'vue';
import throttle from 'lodash/throttle';
import { preventDefault } from '../shared/dom';
Expand Down Expand Up @@ -191,16 +192,22 @@ export default defineComponent({
},
);

onMounted(() => {
parentRect.value = indexesRoot.value?.getBoundingClientRect() || { top: 0 };
getAnchorsRect().then(() => {
groupTop.forEach((item, index) => {
const next = groupTop[index + 1];
item.totalHeight = (next?.top || Infinity) - item.top;
const init = () => {
nextTick(() => {
parentRect.value = indexesRoot.value?.getBoundingClientRect() || { top: 0 };
getAnchorsRect().then(() => {
groupTop.forEach((item, index) => {
const next = groupTop[index + 1];
item.totalHeight = (next?.top || Infinity) - item.top;
});
setAnchorOnScroll(0);
});
setAnchorOnScroll(0);
});
});
};

onMounted(init);

watch(() => props.indexList, init);

onBeforeUnmount(() => {
timeOut && clearTimeout(timeOut);
Expand Down
Loading