Skip to content

Commit afd74c9

Browse files
committed
fix: table sticky scroll bar not reactive
1 parent 8e37ffb commit afd74c9

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

components/vc-table/Table.tsx

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import useSticky from './hooks/useSticky';
3333
import FixedHolder from './FixedHolder';
3434
import type { CSSProperties } from 'vue';
3535
import {
36+
onUpdated,
3637
computed,
3738
defineComponent,
3839
nextTick,
@@ -327,6 +328,10 @@ export default defineComponent<TableProps<DefaultRecordType>>({
327328
const fullTableRef = ref<HTMLDivElement>();
328329
const scrollHeaderRef = ref<HTMLDivElement>();
329330
const scrollBodyRef = ref<HTMLDivElement>();
331+
const scrollBodySizeInfo = ref<{ scrollWidth: number; clientWidth: number }>({
332+
scrollWidth: 0,
333+
clientWidth: 0,
334+
});
330335
const scrollSummaryRef = ref<HTMLDivElement>();
331336
const [pingedLeft, setPingedLeft] = useState(false);
332337
const [pingedRight, setPingedRight] = useState(false);
@@ -495,6 +500,18 @@ export default defineComponent<TableProps<DefaultRecordType>>({
495500
nextTick(() => {
496501
triggerOnScroll();
497502
setScrollbarSize(getTargetScrollBarSize(scrollBodyRef.value).width);
503+
scrollBodySizeInfo.value = {
504+
scrollWidth: scrollBodyRef.value?.scrollWidth || 0,
505+
clientWidth: scrollBodyRef.value?.clientWidth || 0,
506+
};
507+
});
508+
});
509+
onUpdated(() => {
510+
nextTick(() => {
511+
scrollBodySizeInfo.value = {
512+
scrollWidth: scrollBodyRef.value?.scrollWidth || 0,
513+
clientWidth: scrollBodyRef.value?.clientWidth || 0,
514+
};
498515
});
499516
});
500517

@@ -762,6 +779,7 @@ export default defineComponent<TableProps<DefaultRecordType>>({
762779
scrollBodyRef={scrollBodyRef}
763780
onScroll={onScroll}
764781
container={container}
782+
scrollBodySizeInfo={scrollBodySizeInfo.value}
765783
/>
766784
)}
767785
</>

components/vc-table/stickyScrollBar.tsx

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { Ref } from 'vue';
22
import {
3+
watchEffect,
34
getCurrentInstance,
4-
onBeforeUpdate,
5-
onBeforeMount,
65
defineComponent,
76
onBeforeUnmount,
87
onMounted,
@@ -22,31 +21,29 @@ interface StickyScrollBarProps {
2221
onScroll: (params: { scrollLeft?: number }) => void;
2322
offsetScroll: number;
2423
container: HTMLElement | Window;
24+
scrollBodySizeInfo: { scrollWidth: number; clientWidth: number };
2525
}
2626

2727
export default defineComponent<StickyScrollBarProps>({
2828
name: 'StickyScrollBar',
2929
inheritAttrs: false,
30-
props: ['offsetScroll', 'container', 'scrollBodyRef'] as any,
30+
props: ['offsetScroll', 'container', 'scrollBodyRef', 'scrollBodySizeInfo'] as any,
3131
emits: ['scroll'],
3232
setup(props, { emit, expose }) {
3333
const tableContext = useInjectTable();
3434
const bodyScrollWidth = ref(0);
3535
const bodyWidth = ref(0);
3636
const scrollBarWidth = ref(0);
3737
const instance = getCurrentInstance();
38-
const updateSomeValue = () => {
39-
bodyScrollWidth.value = props.scrollBodyRef.value.scrollWidth || 0;
40-
bodyWidth.value = props.scrollBodyRef.value.clientWidth || 0;
41-
scrollBarWidth.value =
42-
bodyScrollWidth.value && bodyWidth.value * (bodyWidth.value / bodyScrollWidth.value);
43-
};
44-
onBeforeMount(() => {
45-
updateSomeValue();
46-
});
47-
onBeforeUpdate(() => {
48-
updateSomeValue();
49-
});
38+
watchEffect(
39+
() => {
40+
bodyScrollWidth.value = props.scrollBodySizeInfo.scrollWidth || 0;
41+
bodyWidth.value = props.scrollBodySizeInfo.clientWidth || 0;
42+
scrollBarWidth.value =
43+
bodyScrollWidth.value && bodyWidth.value * (bodyWidth.value / bodyScrollWidth.value);
44+
},
45+
{ flush: 'post' },
46+
);
5047

5148
const scrollBarRef = ref();
5249

0 commit comments

Comments
 (0)