Skip to content

Commit

Permalink
chore: rm useless second param (ant-design#49426)
Browse files Browse the repository at this point in the history
* chore: rm useless param

* Update components/watermark/index.tsx

Signed-off-by: lijianan <[email protected]>

* fix: fix

---------

Signed-off-by: lijianan <[email protected]>
  • Loading branch information
li-jia-nan authored Jun 15, 2024
1 parent 3799630 commit ecc78e1
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 38 deletions.
23 changes: 9 additions & 14 deletions components/_util/__tests__/getScroll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ import getScroll from '../getScroll';

describe('getScroll', () => {
it('getScroll target null', () => {
expect(getScroll(null, true)).toBe(0);
expect(getScroll(null, false)).toBe(0);
expect(getScroll(null)).toBe(0);
});

it('getScroll window', () => {
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
window.pageXOffset = x;
window.pageYOffset = y;
});
window.scrollTo(200, 400);
expect(getScroll(window, true)).toBe(400);
expect(getScroll(window, false)).toBe(200);
window.scrollTo(0, 400);
expect(getScroll(window)).toBe(400);
scrollToSpy.mockRestore();
});

Expand All @@ -22,9 +20,8 @@ describe('getScroll', () => {
document.documentElement.scrollLeft = x;
document.documentElement.scrollTop = y;
});
window.scrollTo(200, 400);
expect(getScroll(document, true)).toBe(400);
expect(getScroll(document, false)).toBe(200);
window.scrollTo(0, 400);
expect(getScroll(document)).toBe(400);
scrollToSpy.mockRestore();
});

Expand All @@ -34,9 +31,8 @@ describe('getScroll', () => {
div.scrollLeft = x;
div.scrollTop = y;
});
window.scrollTo(200, 400);
expect(getScroll(div, true)).toBe(400);
expect(getScroll(div, false)).toBe(200);
window.scrollTo(0, 400);
expect(getScroll(div)).toBe(400);
scrollToSpy.mockRestore();
});

Expand All @@ -49,9 +45,8 @@ describe('getScroll', () => {
div.documentElement.scrollLeft = x;
div.documentElement.scrollTop = y;
});
window.scrollTo(200, 400);
expect(getScroll(div, true)).toBe(400);
expect(getScroll(div, false)).toBe(200);
window.scrollTo(0, 400);
expect(getScroll(div)).toBe(400);
scrollToSpy.mockRestore();
});
});
9 changes: 0 additions & 9 deletions components/_util/__tests__/getScrollNode.test.ts

This file was deleted.

21 changes: 10 additions & 11 deletions components/_util/getScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,31 @@ export function isWindow(obj: any): obj is Window {
return obj !== null && obj !== undefined && obj === obj.window;
}

export default function getScroll(
target: HTMLElement | Window | Document | null,
top: boolean,
): number {
const getScroll = (target: HTMLElement | Window | Document | null): number => {
if (typeof window === 'undefined') {
return 0;
}
const method = top ? 'scrollTop' : 'scrollLeft';
let result = 0;
if (isWindow(target)) {
result = target[top ? 'pageYOffset' : 'pageXOffset'];
result = target.pageYOffset;
} else if (target instanceof Document) {
result = target.documentElement[method];
result = target.documentElement.scrollTop;
} else if (target instanceof HTMLElement) {
result = target[method];
result = target.scrollTop;
} else if (target) {
// According to the type inference, the `target` is `never` type.
// Since we configured the loose mode type checking, and supports mocking the target with such shape below::
// `{ documentElement: { scrollLeft: 200, scrollTop: 400 } }`,
// the program may falls into this branch.
// Check the corresponding tests for details. Don't sure what is the real scenario this happens.
result = target[method];
// eslint-disable-next-line dot-notation
result = target['scrollTop'];
}

if (target && !isWindow(target) && typeof result !== 'number') {
result = (target.ownerDocument ?? target).documentElement?.[method];
result = (target.ownerDocument ?? target).documentElement?.scrollTop;
}
return result;
}
};

export default getScroll;
2 changes: 1 addition & 1 deletion components/_util/scrollTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface ScrollToOptions {
export default function scrollTo(y: number, options: ScrollToOptions = {}) {
const { getContainer = () => window, callback, duration = 450 } = options;
const container = getContainer();
const scrollTop = getScroll(container, true);
const scrollTop = getScroll(container);
const startTime = Date.now();

const frameFunc = () => {
Expand Down
2 changes: 1 addition & 1 deletion components/anchor/Anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ const Anchor: React.FC<AnchorProps> = (props) => {
}

const container = getCurrentContainer();
const scrollTop = getScroll(container, true);
const scrollTop = getScroll(container);
const eleOffsetTop = getOffsetTop(targetElement, container);
let y = scrollTop + eleOffsetTop;
y -= targetOffset !== undefined ? targetOffset : offsetTop || 0;
Expand Down
2 changes: 1 addition & 1 deletion components/back-top/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const BackTop: React.FC<BackTopProps> = (props) => {

const handleScroll = throttleByAnimationFrame(
(e: React.UIEvent<HTMLElement, UIEvent> | { target: any }) => {
const scrollTop = getScroll(e.target, true);
const scrollTop = getScroll(e.target);
setVisible(scrollTop >= visibilityHeight);
},
);
Expand Down
2 changes: 1 addition & 1 deletion components/float-button/BackTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const BackTop = React.forwardRef<FloatButtonRef, BackTopProps>((props, ref) => {

const handleScroll = throttleByAnimationFrame(
(e: React.UIEvent<HTMLElement, UIEvent> | { target: any }) => {
const scrollTop = getScroll(e.target, true);
const scrollTop = getScroll(e.target);
setVisible(scrollTop >= visibilityHeight);
},
);
Expand Down

0 comments on commit ecc78e1

Please sign in to comment.