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: fix selectPicker when search is controlled and exit Dropdown wit… #2411

Merged
merged 7 commits into from Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/SelectPicker/SelectPicker.tsx
Expand Up @@ -83,7 +83,7 @@ export interface SelectProps<T> {
onGroupTitleClick?: (event: React.SyntheticEvent) => void;

/** Called when searching */
onSearch?: (searchKeyword: string, event: React.SyntheticEvent) => void;
onSearch?: (searchKeyword: string, event?: React.SyntheticEvent) => void;

/** Called when clean */
onClean?: (event: React.SyntheticEvent) => void;
Expand Down Expand Up @@ -288,8 +288,9 @@ const SelectPicker = React.forwardRef(
const handleExited = useCallback(() => {
setSearchKeyword('');
setActive(false);
onSearch?.('');
onClose?.();
}, [onClose, setSearchKeyword]);
}, [onClose, setSearchKeyword, onSearch]);

const handleEntered = useCallback(() => {
setActive(true);
Expand Down
20 changes: 19 additions & 1 deletion src/SelectPicker/test/SelectPickerSpec.js
@@ -1,5 +1,5 @@
import React from 'react';
import { render } from '@testing-library/react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import ReactTestUtils from 'react-dom/test-utils';
import { getDOMNode, getInstance } from '@test/testUtils';
import SelectPicker from '../SelectPicker';
Expand Down Expand Up @@ -400,4 +400,22 @@ describe('SelectPicker', () => {
expect(getByTestId('content')).to.have.text('Not selected');
});
});
it('Should call onSearch when closed', async () => {
const handleSearch = sinon.spy();
const handleClose = sinon.spy();
let { container } = render(
<>
<button id="exit">exit</button>
<SelectPicker onClose={handleClose} defaultOpen onSearch={handleSearch} data={data} />
</>
);
const exit = container.querySelector('#exit');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use getElementById when possible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The container doesn't have the getElementById method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document should be available in test scripts.


// close select
fireEvent.mouseDown(exit, { bubbles: true });
await waitFor(() => assert.isTrue(handleClose.calledOnce));

assert.isTrue(handleSearch.calledOnce);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated assertions. You could wrap multiple assertions within waitFor.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great writing style

assert.equal(handleSearch.args[0][0], '');
});
});