From 806872bfce32abe17e5ed5451aa21c7beb6b4773 Mon Sep 17 00:00:00 2001 From: Alexa Vargas <239999+axelavargas@users.noreply.github.com> Date: Thu, 21 Aug 2025 13:50:22 +0200 Subject: [PATCH] Grafana UI: Tabs - add missing style for disabled tabs (#109907) * Grafana UI: Tabs - add disabled style * apply PR feedback --- .../grafana-ui/src/components/Tabs/Tab.tsx | 38 +++++++++++++++++-- .../src/components/Tabs/Tabs.story.tsx | 33 ++++++++++++++++ .../src/components/Tabs/Tabs.test.tsx | 27 +++++++++++++ 3 files changed, 94 insertions(+), 4 deletions(-) diff --git a/packages/grafana-ui/src/components/Tabs/Tab.tsx b/packages/grafana-ui/src/components/Tabs/Tab.tsx index 10f2ae2ea6f..6eb695df437 100644 --- a/packages/grafana-ui/src/components/Tabs/Tab.tsx +++ b/packages/grafana-ui/src/components/Tabs/Tab.tsx @@ -27,11 +27,26 @@ export interface TabProps extends HTMLProps { suffix?: NavModelItem['tabSuffix']; truncate?: boolean; tooltip?: string; + /** When true, the tab will be disabled and not clickable */ + disabled?: boolean; } export const Tab = React.forwardRef( ( - { label, active, icon, onChangeTab, counter, suffix: Suffix, className, href, truncate, tooltip, ...otherProps }, + { + label, + active, + icon, + onChangeTab, + counter, + suffix: Suffix, + className, + href, + truncate, + tooltip, + disabled, + ...otherProps + }, ref ) => { const tabsStyles = useStyles2(getStyles); @@ -50,16 +65,19 @@ export const Tab = React.forwardRef( clearStyles, tabsStyles.link, active ? tabsStyles.activeStyle : tabsStyles.notActive, - truncate && tabsStyles.linkTruncate + truncate && tabsStyles.linkTruncate, + disabled && tabsStyles.disabled ); const commonProps = { className: linkClass, 'data-testid': selectors.components.Tab.title(label), ...otherProps, - onClick: onChangeTab, + onClick: disabled ? undefined : onChangeTab, role: 'tab', 'aria-selected': active, + 'aria-disabled': disabled, + tabIndex: disabled ? -1 : undefined, title: !!tooltip ? undefined : otherProps.title, // If tooltip is provided, don't set the title on the link or button, it looks weird }; @@ -70,7 +88,7 @@ export const Tab = React.forwardRef(
} @@ -169,5 +187,17 @@ const getStyles = (theme: GrafanaTheme2) => { suffix: css({ marginLeft: theme.spacing(1), }), + disabled: css({ + color: theme.colors.text.disabled, + cursor: 'not-allowed', + + '&:hover, &:focus': { + color: theme.colors.text.disabled, + + '&::before': { + backgroundColor: 'transparent', + }, + }, + }), }; }; diff --git a/packages/grafana-ui/src/components/Tabs/Tabs.story.tsx b/packages/grafana-ui/src/components/Tabs/Tabs.story.tsx index 8f630d37346..c170b2492ee 100644 --- a/packages/grafana-ui/src/components/Tabs/Tabs.story.tsx +++ b/packages/grafana-ui/src/components/Tabs/Tabs.story.tsx @@ -58,4 +58,37 @@ Counter.args = { value: 10, }; +export const WithDisabled: StoryFn = () => { + const [state, updateState] = useState([ + { label: 'Enabled Tab', key: 'first', active: true }, + { label: 'Disabled Tab', key: 'second', active: false, disabled: true }, + { label: 'Another Tab', key: 'third', active: false }, + ]); + + return ( + + + {state.map((tab, index) => { + return ( + + !tab.disabled && updateState(state.map((tab, idx) => ({ ...tab, active: idx === index }))) + } + /> + ); + })} + + + {state[0].active &&
First tab content
} + {state[1].active &&
Second tab content (disabled)
} + {state[2].active &&
Third tab content
} +
+
+ ); +}; + export default meta; diff --git a/packages/grafana-ui/src/components/Tabs/Tabs.test.tsx b/packages/grafana-ui/src/components/Tabs/Tabs.test.tsx index 78594fc083a..91b0aabce60 100644 --- a/packages/grafana-ui/src/components/Tabs/Tabs.test.tsx +++ b/packages/grafana-ui/src/components/Tabs/Tabs.test.tsx @@ -14,6 +14,9 @@ const setup = (jsx: JSX.Element) => { const onChangeTab = jest.fn(); describe('Tabs', () => { + beforeEach(() => { + onChangeTab.mockClear(); + }); it('should call onChangeTab when clicking a tab', async () => { const { user } = setup( @@ -96,4 +99,28 @@ describe('Tabs', () => { expect(screen.getByTestId('tab-suffix')).toBeInTheDocument(); }); + + it('should render disabled tab correctly', () => { + render( + + + + ); + + const disabledTab = screen.getByRole('tab', { name: 'Disabled Tab' }); + expect(disabledTab).toHaveAttribute('aria-disabled', 'true'); + }); + + it('should not call onChangeTab when disabled tab is clicked', async () => { + const { user } = setup( + + + + ); + + const disabledTab = screen.getByRole('tab', { name: 'Disabled Tab' }); + await user.click(disabledTab); + + expect(onChangeTab).not.toHaveBeenCalled(); + }); });