-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
[material-ui][SpeedDial] Fix navigation with arrow keys when slotProps.fab is defined #46508
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
sai6855
wants to merge
16
commits into
mui:master
Choose a base branch
from
sai6855:speedial-nav
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.
+205
−5
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
a28536d
[material-ui][SpeedDial] Fix navigation with arrow keys when slotPro…
sai6855 42361ec
fix issue
sai6855 3863d49
comment out code
sai6855 87c5129
fix
sai6855 e889f27
Merge branch 'master' into speedial-nav
sai6855 62f150f
add logs
sai6855 d7002e5
Merge branch 'speedial-nav' of https://github.com/sai6855/material-ui…
sai6855 3f9f5fa
fix
sai6855 a270381
add tests
sai6855 873ac72
fix
sai6855 7e3e4d2
Update SpeedDial.js
sai6855 9a0db2f
merge slotprops
sai6855 445b899
Merge branch 'speedial-nav' of https://github.com/sai6855/material-ui…
sai6855 8aa3970
trigger ci
sai6855 cca58f5
fix
sai6855 cd806bd
add type
sai6855 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -424,6 +424,191 @@ describe('<SpeedDial />', () => { | |
}); | ||
}); | ||
|
||
describe('dial focus with slotProps.fab', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
let actionButtons; | ||
let fabButton; | ||
|
||
function NoTransition(props) { | ||
const { children, in: inProp } = props; | ||
|
||
if (!inProp) { | ||
return null; | ||
} | ||
return children; | ||
} | ||
|
||
const renderSpeedDial = async (direction = 'up', actionCount = 4) => { | ||
actionButtons = []; | ||
fabButton = undefined; | ||
|
||
render( | ||
<SpeedDial | ||
ariaLabel={`${direction}-actions-${actionCount}`} | ||
FabProps={{ | ||
ref: (element) => { | ||
fabButton = element; | ||
}, | ||
}} | ||
open | ||
direction={direction} | ||
TransitionComponent={NoTransition} | ||
> | ||
{Array.from({ length: actionCount }, (_, index) => ( | ||
<SpeedDialAction | ||
key={index} | ||
slotProps={{ | ||
fab: { | ||
ref: (element) => { | ||
actionButtons[index] = element; | ||
}, | ||
}, | ||
}} | ||
icon={icon} | ||
tooltipTitle={`action${index}`} | ||
/> | ||
))} | ||
</SpeedDial>, | ||
); | ||
await act(async () => { | ||
fabButton.focus(); | ||
}); | ||
}; | ||
|
||
/** | ||
* | ||
* @param actionIndex | ||
* @returns the button of the nth SpeedDialAction or the Fab if -1 | ||
*/ | ||
const getActionButton = (actionIndex) => { | ||
if (actionIndex === -1) { | ||
return fabButton; | ||
} | ||
return actionButtons[actionIndex]; | ||
}; | ||
/** | ||
* @returns true if the button of the nth action is focused | ||
*/ | ||
const isActionFocused = (index) => { | ||
const expectedFocusedElement = index === -1 ? fabButton : actionButtons[index]; | ||
return expectedFocusedElement === document.activeElement; | ||
}; | ||
|
||
it('displays the actions on focus gain', async () => { | ||
await renderSpeedDial(); | ||
expect(screen.getAllByRole('menuitem')).to.have.lengthOf(4); | ||
expect(fabButton).to.have.attribute('aria-expanded', 'true'); | ||
}); | ||
|
||
it('considers arrow keys with the same initial orientation', async () => { | ||
await renderSpeedDial(); | ||
fireEvent.keyDown(fabButton, { key: 'left' }); | ||
expect(isActionFocused(0)).to.equal(true); | ||
fireEvent.keyDown(getActionButton(0), { key: 'up' }); | ||
expect(isActionFocused(0)).to.equal(true); | ||
fireEvent.keyDown(getActionButton(0), { key: 'left' }); | ||
expect(isActionFocused(1)).to.equal(true); | ||
fireEvent.keyDown(getActionButton(1), { key: 'right' }); | ||
expect(isActionFocused(0)).to.equal(true); | ||
}); | ||
|
||
describe('actions navigation', () => { | ||
/** | ||
* tests a combination of arrow keys on a focused SpeedDial | ||
*/ | ||
const itTestCombination = (dialDirection, keys, expected) => { | ||
it(`start dir ${dialDirection} with keys ${keys.join(',')}`, async () => { | ||
const [firstKey, ...combination] = keys; | ||
const [firstFocusedAction, ...foci] = expected; | ||
|
||
await renderSpeedDial(dialDirection); | ||
|
||
fireEvent.keyDown(fabButton, { key: firstKey }); | ||
expect(isActionFocused(firstFocusedAction)).to.equal( | ||
true, | ||
`focused action initial ${firstKey} should be ${firstFocusedAction}`, | ||
); | ||
|
||
for (let i = 0; i < combination.length; i += 1) { | ||
const arrowKey = combination[i]; | ||
const previousFocusedAction = foci[i - 1] || firstFocusedAction; | ||
const expectedFocusedAction = foci[i]; | ||
const combinationUntilNot = [firstKey, ...combination.slice(0, i + 1)]; | ||
|
||
fireEvent.keyDown(getActionButton(previousFocusedAction), { | ||
key: arrowKey, | ||
}); | ||
expect(isActionFocused(expectedFocusedAction)).to.equal( | ||
true, | ||
`focused action after ${combinationUntilNot.join( | ||
',', | ||
)} should be ${expectedFocusedAction}`, | ||
); | ||
} | ||
}); | ||
}; | ||
|
||
describe('considers the first arrow key press as forward navigation', () => { | ||
itTestCombination('up', ['ArrowUp', 'ArrowUp', 'ArrowUp', 'ArrowDown'], [0, 1, 2, 1]); | ||
itTestCombination('up', ['ArrowDown', 'ArrowDown', 'ArrowDown', 'ArrowUp'], [0, 1, 2, 1]); | ||
|
||
itTestCombination( | ||
'right', | ||
['ArrowRight', 'ArrowRight', 'ArrowRight', 'ArrowLeft'], | ||
[0, 1, 2, 1], | ||
); | ||
itTestCombination( | ||
'right', | ||
['ArrowLeft', 'ArrowLeft', 'ArrowLeft', 'ArrowRight'], | ||
[0, 1, 2, 1], | ||
); | ||
|
||
itTestCombination('down', ['ArrowDown', 'ArrowDown', 'ArrowDown', 'ArrowUp'], [0, 1, 2, 1]); | ||
itTestCombination('down', ['ArrowUp', 'ArrowUp', 'ArrowUp', 'ArrowDown'], [0, 1, 2, 1]); | ||
|
||
itTestCombination( | ||
'left', | ||
['ArrowLeft', 'ArrowLeft', 'ArrowLeft', 'ArrowRight'], | ||
[0, 1, 2, 1], | ||
); | ||
itTestCombination( | ||
'left', | ||
['ArrowRight', 'ArrowRight', 'ArrowRight', 'ArrowLeft'], | ||
[0, 1, 2, 1], | ||
); | ||
}); | ||
|
||
describe('ignores array keys orthogonal to the direction', () => { | ||
itTestCombination('up', ['ArrowUp', 'ArrowLeft', 'ArrowRight', 'ArrowUp'], [0, 0, 0, 1]); | ||
itTestCombination( | ||
'right', | ||
['ArrowRight', 'ArrowUp', 'ArrowDown', 'ArrowRight'], | ||
[0, 0, 0, 1], | ||
); | ||
itTestCombination( | ||
'down', | ||
['ArrowDown', 'ArrowLeft', 'ArrowRight', 'ArrowDown'], | ||
[0, 0, 0, 1], | ||
); | ||
itTestCombination('left', ['ArrowLeft', 'ArrowUp', 'ArrowDown', 'ArrowLeft'], [0, 0, 0, 1]); | ||
}); | ||
|
||
describe('does not wrap around', () => { | ||
itTestCombination('up', ['ArrowUp', 'ArrowDown', 'ArrowDown', 'ArrowUp'], [0, -1, -1, 0]); | ||
itTestCombination( | ||
'right', | ||
['ArrowRight', 'ArrowLeft', 'ArrowLeft', 'ArrowRight'], | ||
[0, -1, -1, 0], | ||
); | ||
itTestCombination('down', ['ArrowDown', 'ArrowUp', 'ArrowUp', 'ArrowDown'], [0, -1, -1, 0]); | ||
itTestCombination( | ||
'left', | ||
['ArrowLeft', 'ArrowRight', 'ArrowRight', 'ArrowLeft'], | ||
[0, -1, -1, 0], | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('prop: transitionDuration', () => { | ||
it('should render the default theme values by default', function test() { | ||
if (/jsdom/.test(window.navigator.userAgent)) { | ||
|
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.
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.
Don't we need to destructure
ChildFabProps
here to keepFabProps
working correctly?I wonder why did test not break without it 🤔
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.
It doesn't need to be passed, as this logic is already handled directly within SpeedDialAction.
material-ui/packages/mui-material/src/SpeedDialAction/SpeedDialAction.js
Lines 169 to 172 in 12ae603
In above code,
FabProps
meansChildFabProps