Skip to content

Commit

Permalink
fix(RAC): support hover events for Tabs (#6742)
Browse files Browse the repository at this point in the history
support hover events for Tab
  • Loading branch information
lithdew authored Jul 30, 2024
1 parent 826d9d4 commit 6dfa366
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
9 changes: 6 additions & 3 deletions packages/react-aria-components/src/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* governing permissions and limitations under the License.
*/

import {AriaLabelingProps, forwardRefType, Key, LinkDOMProps, RefObject} from '@react-types/shared';
import {AriaLabelingProps, forwardRefType, HoverEvents, Key, LinkDOMProps, RefObject} from '@react-types/shared';
import {AriaTabListProps, AriaTabPanelProps, mergeProps, Orientation, useFocusRing, useHover, useTab, useTabList, useTabPanel} from 'react-aria';
import {Collection, CollectionBuilder, createHideableComponent, createLeafComponent} from '@react-aria/collections';
import {CollectionProps, CollectionRendererContext, usePersistedKeys} from './Collection';
Expand Down Expand Up @@ -43,7 +43,7 @@ export interface TabListRenderProps {
state: TabListState<unknown>
}

export interface TabProps extends RenderProps<TabRenderProps>, AriaLabelingProps, LinkDOMProps {
export interface TabProps extends RenderProps<TabRenderProps>, AriaLabelingProps, LinkDOMProps, HoverEvents {
/** The unique id of the tab. */
id?: Key,
/** Whether the tab is disabled. */
Expand Down Expand Up @@ -252,7 +252,10 @@ export const Tab = /*#__PURE__*/ createLeafComponent('item', (props: TabProps, f
let {tabProps, isSelected, isDisabled, isPressed} = useTab({key: item.key, ...props}, state, ref);
let {focusProps, isFocused, isFocusVisible} = useFocusRing();
let {hoverProps, isHovered} = useHover({
isDisabled
isDisabled,
onHoverStart: props.onHoverStart,
onHoverEnd: props.onHoverEnd,
onHoverChange: props.onHoverChange
});

let renderProps = useRenderProps({
Expand Down
30 changes: 29 additions & 1 deletion packages/react-aria-components/test/Tabs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ describe('Tabs', () => {
});

it('should support hover', async () => {
let {getAllByRole} = renderTabs({}, {}, {className: ({isHovered}) => isHovered ? 'hover' : ''});
let onHoverStart = jest.fn();
let onHoverChange = jest.fn();
let onHoverEnd = jest.fn();
let {getAllByRole} = renderTabs({}, {}, {className: ({isHovered}) => isHovered ? 'hover' : '', onHoverStart, onHoverChange, onHoverEnd});
let tab = getAllByRole('tab')[0];

expect(tab).not.toHaveAttribute('data-hovered');
Expand All @@ -125,10 +128,35 @@ describe('Tabs', () => {
await user.hover(tab);
expect(tab).toHaveAttribute('data-hovered', 'true');
expect(tab).toHaveClass('hover');
expect(onHoverStart).toHaveBeenCalledTimes(1);
expect(onHoverChange).toHaveBeenCalledTimes(1);

await user.unhover(tab);
expect(tab).not.toHaveAttribute('data-hovered');
expect(tab).not.toHaveClass('hover');
expect(onHoverEnd).toHaveBeenCalledTimes(1);
expect(onHoverChange).toHaveBeenCalledTimes(2);
});

it('should not show hover state when item is not interactive', async () => {
let onHoverStart = jest.fn();
let onHoverChange = jest.fn();
let onHoverEnd = jest.fn();
let {getAllByRole} = renderTabs({disabledKeys: ['a', 'b', 'c']}, {}, {className: ({isHovered}) => isHovered ? 'hover' : '', onHoverStart, onHoverChange, onHoverEnd});
let tab = getAllByRole('tab')[0];

expect(tab).not.toHaveAttribute('data-hovered');
expect(tab).not.toHaveClass('hover');
expect(onHoverStart).not.toHaveBeenCalled();
expect(onHoverChange).not.toHaveBeenCalled();
expect(onHoverEnd).not.toHaveBeenCalled();

await user.hover(tab);
expect(tab).not.toHaveAttribute('data-hovered');
expect(tab).not.toHaveClass('hover');
expect(onHoverStart).not.toHaveBeenCalled();
expect(onHoverChange).not.toHaveBeenCalled();
expect(onHoverEnd).not.toHaveBeenCalled();
});

it('should support focus ring', async () => {
Expand Down

1 comment on commit 6dfa366

@rspbot
Copy link

@rspbot rspbot commented on 6dfa366 Jul 31, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.