-
Notifications
You must be signed in to change notification settings - Fork 2.9k
refactor(react-card): make base hooks tabster-free #36004
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
Open
dmytrokirpa
wants to merge
2
commits into
microsoft:master
Choose a base branch
from
dmytrokirpa:refactor/react-card-headless-base-hooks
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+260
−84
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-card-477da387-3e1b-4b6b-86bc-657ea08c4310.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "minor", | ||
| "comment": "Make Card base hooks tabster-free; expose shouldRestrictTriggerAction on CardBaseProps", | ||
| "packageName": "@fluentui/react-card", | ||
| "email": "dmytrokirpa@microsoft.com", | ||
| "dependentChangeType": "patch" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
packages/react-components/react-card/library/src/components/Card/useCard.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import * as React from 'react'; | ||
| import { renderHook } from '@testing-library/react-hooks'; | ||
| import { useFocusableGroup, useFocusFinders, useFocusWithin } from '@fluentui/react-tabster'; | ||
|
|
||
| import { useCard_unstable, useCardBase_unstable } from './useCard'; | ||
|
|
||
| jest.mock('@fluentui/react-tabster', () => ({ | ||
| useFocusWithin: jest.fn(), | ||
| useFocusFinders: jest.fn(), | ||
| useFocusableGroup: jest.fn(), | ||
| })); | ||
|
|
||
| const mockFocusWithinRef = React.createRef<HTMLDivElement>(); | ||
| const mockFindAllFocusable = jest.fn().mockReturnValue([]); | ||
|
|
||
| beforeEach(() => { | ||
| (useFocusWithin as jest.Mock).mockReturnValue(mockFocusWithinRef); | ||
| (useFocusFinders as jest.Mock).mockReturnValue({ findAllFocusable: mockFindAllFocusable }); | ||
| (useFocusableGroup as jest.Mock).mockReturnValue({ 'data-tabster': '{"groupper":{}}' }); | ||
| mockFindAllFocusable.mockReturnValue([]); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // useCardBase_unstable — interactive is now computed from event props only, | ||
| // without any @fluentui/react-tabster dependency. | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('useCardBase_unstable', () => { | ||
| it('returns interactive: false when no pointer/mouse event props are provided', () => { | ||
| const { result } = renderHook(() => useCardBase_unstable({}, React.createRef())); | ||
| expect(result.current.interactive).toBe(false); | ||
| }); | ||
|
|
||
| it('returns interactive: true when onClick is provided', () => { | ||
| const { result } = renderHook(() => useCardBase_unstable({ onClick: jest.fn() }, React.createRef())); | ||
| expect(result.current.interactive).toBe(true); | ||
| }); | ||
|
|
||
| it('returns interactive: true for other pointer event props', () => { | ||
| const { result: r1 } = renderHook(() => useCardBase_unstable({ onPointerDown: jest.fn() }, React.createRef())); | ||
| expect(r1.current.interactive).toBe(true); | ||
|
|
||
| const { result: r2 } = renderHook(() => useCardBase_unstable({ onMouseUp: jest.fn() }, React.createRef())); | ||
| expect(r2.current.interactive).toBe(true); | ||
| }); | ||
|
|
||
| it('returns interactive: false when disabled even if onClick is provided', () => { | ||
| const { result } = renderHook(() => | ||
| useCardBase_unstable({ onClick: jest.fn(), disabled: true }, React.createRef()), | ||
| ); | ||
| expect(result.current.interactive).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // useCard_unstable — focus management (tabIndex, focusable-group attrs) moved | ||
| // here from useCardBase_unstable; applied only when appropriate. | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| describe('useCard_unstable', () => { | ||
| it('applies tabIndex: 0 and focusable-group attrs when interactive and not selectable', () => { | ||
| const { result } = renderHook(() => useCard_unstable({ onClick: jest.fn() }, React.createRef())); | ||
| expect(result.current.root.tabIndex).toBe(0); | ||
| expect((result.current.root as Record<string, unknown>)['data-tabster']).toBe('{"groupper":{}}'); | ||
| }); | ||
|
|
||
| it('does not apply focus attrs when disabled', () => { | ||
| const { result } = renderHook(() => useCard_unstable({ onClick: jest.fn(), disabled: true }, React.createRef())); | ||
| expect(result.current.root.tabIndex).toBeUndefined(); | ||
| expect((result.current.root as Record<string, unknown>)['data-tabster']).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('does not apply focus attrs when focusMode is off', () => { | ||
| const { result } = renderHook(() => useCard_unstable({ onClick: jest.fn(), focusMode: 'off' }, React.createRef())); | ||
| expect(result.current.root.tabIndex).toBeUndefined(); | ||
| expect((result.current.root as Record<string, unknown>)['data-tabster']).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('does not apply focus attrs when card is selectable', () => { | ||
| const { result } = renderHook(() => useCard_unstable({ onSelectionChange: jest.fn() }, React.createRef())); | ||
| expect(result.current.root.tabIndex).toBeUndefined(); | ||
| expect((result.current.root as Record<string, unknown>)['data-tabster']).toBeUndefined(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/react-components/react-card/library/src/components/Card/useCardSelectable.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import type * as React from 'react'; | ||
| import { renderHook, act } from '@testing-library/react-hooks'; | ||
|
|
||
| import { useCardSelectable } from './useCardSelectable'; | ||
|
|
||
| const makeA11yProps = () => ({ referenceId: undefined, referenceLabel: undefined }); | ||
|
|
||
| // useCardSelectable — shouldRestrictTriggerAction is a new optional predicate | ||
| // on CardBaseProps; the checkbox target always bypasses it. | ||
|
|
||
| describe('useCardSelectable', () => { | ||
| it('blocks selection when shouldRestrictTriggerAction returns true', () => { | ||
| const onSelectionChange = jest.fn(); | ||
| const shouldRestrictTriggerAction = jest.fn().mockReturnValue(true); | ||
| const { result } = renderHook(() => | ||
| useCardSelectable({ onSelectionChange, shouldRestrictTriggerAction }, makeA11yProps()), | ||
| ); | ||
|
|
||
| const event = { target: document.createElement('span') } as unknown as React.MouseEvent<HTMLDivElement>; | ||
| act(() => { | ||
| result.current.selectableCardProps!.onClick(event); | ||
| }); | ||
|
|
||
| expect(onSelectionChange).not.toHaveBeenCalled(); | ||
| expect(shouldRestrictTriggerAction).toHaveBeenCalledWith(event); | ||
| }); | ||
|
|
||
| it('allows selection when shouldRestrictTriggerAction returns false', () => { | ||
| const onSelectionChange = jest.fn(); | ||
| const shouldRestrictTriggerAction = jest.fn().mockReturnValue(false); | ||
| const { result } = renderHook(() => | ||
| useCardSelectable({ onSelectionChange, shouldRestrictTriggerAction }, makeA11yProps()), | ||
| ); | ||
|
|
||
| const event = { target: document.createElement('span') } as unknown as React.MouseEvent<HTMLDivElement>; | ||
| act(() => { | ||
| result.current.selectableCardProps!.onClick(event); | ||
| }); | ||
|
|
||
| expect(onSelectionChange).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('bypasses shouldRestrictTriggerAction when the checkbox itself is the event target', () => { | ||
| const onSelectionChange = jest.fn(); | ||
| const shouldRestrictTriggerAction = jest.fn().mockReturnValue(true); | ||
| const { result } = renderHook(() => | ||
| useCardSelectable({ onSelectionChange, shouldRestrictTriggerAction }, makeA11yProps()), | ||
| ); | ||
|
|
||
| // slot.optional spreads defaultProps directly into the result object. | ||
| const checkboxSlot = result.current.checkboxSlot! as unknown as { | ||
| ref: React.RefObject<HTMLInputElement>; | ||
| onChange: React.ChangeEventHandler<HTMLInputElement>; | ||
| }; | ||
| const checkboxEl = document.createElement('input'); | ||
| checkboxSlot.ref.current = checkboxEl; | ||
|
|
||
| const changeEvent = { target: checkboxEl } as unknown as React.ChangeEvent<HTMLInputElement>; | ||
| act(() => { | ||
| checkboxSlot.onChange(changeEvent); | ||
| }); | ||
|
|
||
| expect(shouldRestrictTriggerAction).not.toHaveBeenCalled(); | ||
| expect(onSelectionChange).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🕵🏾♀️ visual changes to review in the Visual Change Report
vr-tests-react-components/Charts-DonutChart 3 screenshots
vr-tests-react-components/Positioning 2 screenshots
vr-tests-react-components/ProgressBar converged 3 screenshots
vr-tests-react-components/TagPicker 3 screenshots
There were 3 duplicate changes discarded. Check the build logs for more information.