Skip to content

fix: trigger "onMotionEnd" when element is hidden #161

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

Merged
merged 4 commits into from
Jan 17, 2023
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
2 changes: 1 addition & 1 deletion src/MotionThumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default function MotionThumb(props: MotionThumbInterface) {
`.${prefixCls}-item`,
)[index];

return ele;
return ele?.offsetParent && ele;
};

const [prevStyle, setPrevStyle] = React.useState<ThumbReact>(null);
Expand Down
48 changes: 47 additions & 1 deletion tests/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { act, fireEvent, render } from '@testing-library/react';
import * as React from 'react';
import { render, act, fireEvent } from '@testing-library/react';
import Segmented from '../src';

jest.mock('rc-motion/lib/util/motion', () => {
Expand Down Expand Up @@ -231,6 +231,12 @@ describe('rc-segmented', () => {
});

it('render segmented with controlled mode', () => {
const offsetParentSpy = jest
.spyOn(HTMLElement.prototype, 'offsetParent', 'get')
.mockImplementation(() => {
return container;
});

const Demo = () => {
const options = ['iOS', 'Android', 'Web3'];

Expand All @@ -250,6 +256,7 @@ describe('rc-segmented', () => {
);
};
const { container } = render(<Demo />);

fireEvent.click(container.querySelectorAll('.rc-segmented-item-input')[0]);
expect(container.querySelector('.value')?.textContent).toBe('iOS');

Expand Down Expand Up @@ -284,10 +291,17 @@ describe('rc-segmented', () => {

// invalid changes: Should not active any item to make sure it's single source of truth
expect(container.querySelector('.rc-segmented-item-selected')).toBeFalsy();

offsetParentSpy.mockRestore();
});

describe('render segmented with CSSMotion', () => {
it('basic', () => {
const offsetParentSpy = jest
.spyOn(HTMLElement.prototype, 'offsetParent', 'get')
.mockImplementation(() => {
return container;
});
const handleValueChange = jest.fn();
const { container, asFragment } = render(
<Segmented
Expand Down Expand Up @@ -366,9 +380,16 @@ describe('rc-segmented', () => {

// thumb should disappear
expect(container.querySelector('.rc-segmented-thumb')).toBeFalsy();

offsetParentSpy.mockRestore();
});

it('quick switch', () => {
const offsetParentSpy = jest
.spyOn(HTMLElement.prototype, 'offsetParent', 'get')
.mockImplementation(() => {
return container;
});
const { container } = render(
<Segmented
options={['IOS', 'Android', 'Web3']}
Expand Down Expand Up @@ -403,6 +424,31 @@ describe('rc-segmented', () => {
'--thumb-active-left': '0px',
'--thumb-active-width': '62px',
});

offsetParentSpy.mockRestore();
});

it('stop animation early in hidden parent', () => {
const offsetParentSpy = jest
.spyOn(HTMLElement.prototype, 'offsetParent', 'get')
.mockImplementation(() => null);
const Demo = () => {
const [value, setValue] = React.useState<string>('iOS');
React.useEffect(() => setValue('Web3'), []);
return <Segmented options={['iOS', 'Android', 'Web3']} value={value} />;
};

const { container } = render(<Demo />);

// stop animation early and place "selected" class
expect(container.querySelectorAll('.rc-segmented-item')[2]).toHaveClass(
'rc-segmented-item-selected',
);

// thumb should disappear
expect(container.querySelector('.rc-segmented-thumb')).toBeFalsy();

offsetParentSpy.mockRestore();
});
});

Expand Down