Skip to content

Commit

Permalink
fix(dropdown): fix stale search keyword
Browse files Browse the repository at this point in the history
fix the dropdown so when the searchable prop changes dynamically the search keyword is reset

fix #601
  • Loading branch information
mario-subo committed May 5, 2021
1 parent 6bc9ae5 commit c2ed857
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/src/Dropdown/Dropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ describe("Component: Dropdown", () => {
act(() => Simulate.change(searchField, { target: { value: "second" } as any }));

expect(document.body.querySelectorAll(".custom-control")).toHaveLength(1);

// if the searchable prop is changed back to false
act(() => {
render(<Dropdown>{testOptions}</Dropdown>, container);
});

// The search field should be reset to empty string
// so all the elements should be visible again
expect(document.body.querySelectorAll(".custom-control")).toHaveLength(4);
});

it("Should allow the value to be cleared when clearable is enabled", () => {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ export const Dropdown: React.FC<DropdownProps> = React.forwardRef(
!isMobile && props.multiple && setAllSelected(isAllSelected());
}, [props.value]);

React.useEffect(() => {
!searchable && setSearchKeyword("");
}, [searchable]);

React.useEffect(() => {
if (!isMobile) {
const detectBlur = (event: MouseEvent) => {
Expand Down

0 comments on commit c2ed857

Please sign in to comment.