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

refactor(affix): component optimization #5057

Merged
merged 2 commits into from
Feb 23, 2025
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
24 changes: 12 additions & 12 deletions packages/components/affix/__tests__/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ describe('Affix', () => {
const wrapper = mount({
render() {
return (
<Affix offsetTop={offsetTop}>
<Affix ref="affixRef" offsetTop={offsetTop}>
<div style={{ width: `${slotWidth}px`, height: `${slotHeight}px` }}>hello world</div>
</Affix>
);
},
}).findComponent(Affix);

});
const { affixRef } = wrapper.vm.$refs;
// 模拟 affixWrap 的位置
vi.spyOn(wrapper.vm.affixWrapRef, 'getBoundingClientRect').mockImplementation(() => ({
vi.spyOn(affixRef.affixWrapRef, 'getBoundingClientRect').mockImplementation(() => ({
top: 5,
width: slotWidth,
height: slotHeight,
}));

it('Test get container', async () => {
await nextTick();
expect(wrapper.vm.scrollContainer).toBe(window);
expect(affixRef.scrollContainer).toBe(window);
});

it('Test the scrolling state', async () => {
Expand Down Expand Up @@ -72,23 +72,23 @@ describe('Affix', () => {
render() {
return (
<div class="container" ref="container">
<Affix container={this.container} offsetTop={offsetTop}>
<Affix ref="affixRef" container={this.container} offsetTop={offsetTop}>
<div style="width: 100px; height: 20px">hello world</div>
</Affix>
</div>
);
},
});

const affixWrapper = wrapper.findComponent(Affix);
const { affixRef } = wrapper.vm.$refs;

it('Test get container', async () => {
await nextTick();
expect(affixWrapper.vm.scrollContainer).toBe(wrapper.vm.container());
expect(affixRef.scrollContainer).toBe(wrapper.vm.container());
});
// 模拟 affixWrap 的位置
beforeEach(() => {
vi.spyOn(affixWrapper.vm.affixWrapRef, 'getBoundingClientRect').mockImplementation(() => ({
vi.spyOn(affixRef.affixWrapRef, 'getBoundingClientRect').mockImplementation(() => ({
top: 5,
width: slotWidth,
height: slotHeight,
Expand All @@ -98,14 +98,14 @@ describe('Affix', () => {
it('Test the scrolling state', async () => {
// 模拟容器滚动
wrapper.vm.container().dispatchEvent(new CustomEvent('scroll'));
expect(affixWrapper.find('.t-affix').classes()).toContain('t-affix');
expect(wrapper.find('.t-affix').classes()).toContain('t-affix');
});

beforeEach(() => {
// 模拟绑定
window.addEventListener('scroll', affixWrapper.vm.handleScroll);
window.addEventListener('scroll', affixRef.handleScroll);
// 模拟容器的位置
vi.spyOn(affixWrapper.vm.scrollContainer, 'getBoundingClientRect').mockImplementation(() => ({
vi.spyOn(affixRef.scrollContainer, 'getBoundingClientRect').mockImplementation(() => ({
top: containerTop,
}));
});
Expand Down
32 changes: 13 additions & 19 deletions packages/components/affix/affix.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineComponent({
const affixRef = ref<HTMLElement>(null);
const placeholderEL = ref(document?.createElement('div')); // 占位节点
const ticking = ref(false);
const binded = ref(false);
const isBind = ref(false);

const scrollContainer = ref<ScrollContainerElement>();
const affixStyle = ref<Record<string, any>>();
Expand Down Expand Up @@ -96,21 +96,21 @@ export default defineComponent({

const bindScroll = async () => {
await nextTick();
if (binded.value) return;
if (isBind.value) return;
scrollContainer.value = getScrollContainer(props.container);
on(scrollContainer.value, 'scroll', handleScroll);
on(window, 'resize', handleScroll);
binded.value = true;
isBind.value = true;
};

const unbindScroll = () => {
if (!scrollContainer.value || !binded.value) return;
if (!scrollContainer.value || !isBind.value) return;
off(scrollContainer.value, 'scroll', handleScroll);
off(window, 'resize', handleScroll);
if (rAFId) {
window.cancelAnimationFrame(rAFId);
}
binded.value = false;
isBind.value = false;
};

watch(
Expand Down Expand Up @@ -142,22 +142,16 @@ export default defineComponent({

onBeforeUnmount(unbindScroll);

return {
context.expose({
scrollContainer,
affixWrapRef,
affixRef,
bindScroll,
unbindScroll,
handleScroll,
scrollContainer,
renderTNodeJSX,
affixStyle,
};
},
render() {
return (
<div ref="affixWrapRef">
<div ref="affixRef" style={this.affixStyle}>
{this.renderTNodeJSX('default')}
});

return () => (
<div ref={affixWrapRef}>
<div ref={affixRef} style={affixStyle.value}>
{renderTNodeJSX('default')}
</div>
</div>
);
Expand Down