Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/unlucky-mugs-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/polaris': minor
---

IndexFilters tabs were sometimes not selected if they were placed in the more view menu dropdown, modified this such that onSelect is used instead if the user is attempting to select a tab that is in the more views dropdown menu
13 changes: 12 additions & 1 deletion polaris-react/src/components/IndexFilters/IndexFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,17 @@ export function IndexFilters({
beginEdit(IndexFiltersMode.Filtering);
}

const handleTabSelect = useCallback(
(selectedTabIndex: number) => {
onSelect?.(selectedTabIndex);
const selectedTab = tabs[selectedTabIndex];
if (selectedTab?.onAction) {
selectedTab.onAction();
}
},
[onSelect, tabs],
);

return (
<div
className={styles.IndexFiltersWrapper}
Expand Down Expand Up @@ -399,7 +410,7 @@ export function IndexFilters({
<Tabs
tabs={tabs}
selected={selected}
onSelect={onSelect}
onSelect={handleTabSelect}
disabled={Boolean(
mode !== IndexFiltersMode.Default || disabled,
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,76 @@ describe('IndexFilters', () => {
expect(onEditStart).toHaveBeenCalledWith(IndexFiltersMode.EditingColumns);
});
});

describe('tab actions', () => {
it('calls both onSelect and onAction when a tab is selected', () => {
const onSelect = jest.fn();
const onAction = jest.fn();
const tabs = [
{
id: 'tab1',
content: 'Tab 1',
onAction,
},
];

const wrapper = mountWithApp(
<IndexFilters {...defaultProps} tabs={tabs} onSelect={onSelect} />,
);

wrapper.find(Tabs)!.trigger('onSelect', 0);

expect(onSelect).toHaveBeenCalledWith(0);
expect(onAction).toHaveBeenCalled();
});

it('calls onSelect but not onAction when selected tab has no onAction', () => {
const onSelect = jest.fn();
const tabs = [
{
id: 'tab1',
content: 'Tab 1',
},
];

const wrapper = mountWithApp(
<IndexFilters {...defaultProps} tabs={tabs} onSelect={onSelect} />,
);

wrapper.find(Tabs)!.trigger('onSelect', 0);

expect(onSelect).toHaveBeenCalledWith(0);
});

it('calls the valid onAction when switching between tabs', () => {
const onAction1 = jest.fn();
const onAction2 = jest.fn();
const tabs = [
{
id: 'tab1',
content: 'Tab 1',
onAction: onAction1,
},
{
id: 'tab2',
content: 'Tab 2',
onAction: onAction2,
},
];

const wrapper = mountWithApp(
<IndexFilters {...defaultProps} tabs={tabs} />,
);

// Select first tab
wrapper.find(Tabs)!.trigger('onSelect', 0);
expect(onAction1).toHaveBeenCalledTimes(1);
expect(onAction2).not.toHaveBeenCalled();

// Select second tab
wrapper.find(Tabs)!.trigger('onSelect', 1);
expect(onAction1).toHaveBeenCalledTimes(1);
expect(onAction2).toHaveBeenCalledTimes(1);
});
});
});