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: revert the code to fix infinite loop on controlled multiselect #16793

Merged
merged 1 commit into from
Jun 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -324,34 +324,6 @@ describe('MultiSelect', () => {
).toBeInstanceOf(HTMLElement);
});

it('should trigger onChange with selected items', async () => {
let selectedItems = [];
const testFunction = jest.fn((e) => (selectedItems = e?.selectedItems));
const items = generateItems(4, generateGenericItem);
const label = 'test-label';
const { container } = render(
<MultiSelect
id="custom-id"
onChange={testFunction}
selectedItems={selectedItems}
label={label}
items={items}
/>
);

// eslint-disable-next-line testing-library/prefer-screen-queries
const labelNode = getByText(container, label);
await userEvent.click(labelNode);

const [item] = items;
// eslint-disable-next-line testing-library/prefer-screen-queries
const itemNode = getByText(container, item.label);

await userEvent.click(itemNode);
// Assert that the onChange callback returned the selected items and assigned it to selectedItems
expect(testFunction.mock.results[0].value).toEqual(selectedItems);
});

it('should place the given id on the ___ node when passed in as a prop', () => {
const items = generateItems(4, generateGenericItem);
const label = 'test-label';
Expand Down Expand Up @@ -481,31 +453,6 @@ describe('MultiSelect', () => {

expect(testFunction).toHaveBeenCalledTimes(1);
});

it('should call onChange when the selection changes outside of the component', () => {
const handleChange = jest.fn();
const items = generateItems(4, generateGenericItem);
const props = {
id: 'custom-id',
onChange: handleChange,
selectedItems: [],
label: 'test-label',
items,
};
const { rerender } = render(<MultiSelect {...props} />);

expect(handleChange).not.toHaveBeenCalled();

act(() => {
rerender(<MultiSelect {...props} selectedItems={[items[0]]} />);
});

expect(handleChange).toHaveBeenCalledTimes(1);
expect(handleChange.mock.lastCall[0]).toMatchObject({
selectedItems: [items[0]],
});
});

it('should support an invalid state with invalidText that describes the field', () => {
const items = generateItems(4, generateGenericItem);
const label = 'test-label';
Expand Down
42 changes: 19 additions & 23 deletions packages/react/src/internal/Selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,7 @@ export function useSelection({
const [uncontrolledItems, setUncontrolledItems] =
useState(initialSelectedItems);
const isControlled = !!controlledItems;
const [selectedItems, setSelectedItems] = useState(
isControlled ? controlledItems : uncontrolledItems
);

useEffect(() => {
setSelectedItems(isControlled ? controlledItems : uncontrolledItems);
}, [isControlled, controlledItems, uncontrolledItems]);

useEffect(() => {
callOnChangeHandler({
isControlled,
isMounted: isMounted.current,
onChangeHandlerControlled: savedOnChange.current,
onChangeHandlerUncontrolled: setUncontrolledItems,
selectedItems: selectedItems,
});
}, [isControlled, isMounted, selectedItems]);

const selectedItems = isControlled ? controlledItems : uncontrolledItems;
const onItemChange = useCallback(
(item) => {
if (disabled) {
Expand All @@ -65,15 +48,28 @@ export function useSelection({
selectedIndex = index;
}
});
let newSelectedItems;
if (selectedIndex === undefined) {
setSelectedItems((selectedItems) => selectedItems.concat(item));
newSelectedItems = selectedItems.concat(item);
callOnChangeHandler({
isControlled,
isMounted: isMounted.current,
onChangeHandlerControlled: savedOnChange.current,
onChangeHandlerUncontrolled: setUncontrolledItems,
selectedItems: newSelectedItems,
});
return;
}
setSelectedItems((selectedItems) =>
removeAtIndex(selectedItems, selectedIndex)
);
newSelectedItems = removeAtIndex(selectedItems, selectedIndex);
callOnChangeHandler({
isControlled,
isMounted: isMounted.current,
onChangeHandlerControlled: savedOnChange.current,
onChangeHandlerUncontrolled: setUncontrolledItems,
selectedItems: newSelectedItems,
});
},
[disabled, selectedItems]
[disabled, isControlled, selectedItems]
);

const clearSelection = useCallback(() => {
Expand Down
Loading