Skip to content

Commit 252940a

Browse files
solimantLFDanLu
andauthored
Make ActionGroup honor disabledKeys when collapsed (#2463)
Co-authored-by: Daniel Lu <[email protected]>
1 parent 43bd5e7 commit 252940a

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

packages/@react-spectrum/actiongroup/src/ActionGroup.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,7 @@ function ActionGroupMenu<T>({state, isDisabled, isEmphasized, staticColor, items
456456
</SlotProvider>
457457
<Menu
458458
items={items}
459+
disabledKeys={state.disabledKeys}
459460
selectionMode={state.selectionManager.selectionMode}
460461
selectedKeys={state.selectionManager.selectedKeys}
461462
disallowEmptySelection={state.selectionManager.disallowEmptySelection}

packages/@react-spectrum/actiongroup/stories/ActionGroup.stories.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,17 @@ storiesOf('ActionGroup', module)
313313
</div>
314314
)
315315
)
316+
.add(
317+
'overflowMode: collapse, disabledKeys',
318+
() => (
319+
<div style={{padding: '10px', resize: 'horizontal', overflow: 'auto', width: 250, backgroundColor: 'var(--spectrum-global-color-gray-50)'}}>
320+
{renderCollapsible({disabledKeys: ['edit', 'duplicate']})}
321+
{renderCollapsible({density: 'compact', disabledKeys: ['edit', 'duplicate']})}
322+
{renderCollapsible({density: 'compact', disabledKeys: ['edit', 'duplicate'], isJustified: true})}
323+
{renderCollapsible({disabledKeys: ['edit', 'duplicate'], isQuiet: true})}
324+
</div>
325+
)
326+
)
316327
.add(
317328
'buttonLabelBehavior: hide',
318329
() => (

packages/@react-spectrum/actiongroup/test/ActionGroup.test.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* governing permissions and limitations under the License.
1111
*/
1212

13-
import {act, fireEvent, render, within} from '@testing-library/react';
13+
import {act, fireEvent, render, screen, within} from '@testing-library/react';
1414
import {ActionGroup} from '../';
1515
import {Button} from '@react-spectrum/button';
1616
import {Dialog, DialogTrigger} from '@react-spectrum/dialog';
@@ -892,6 +892,54 @@ describe('ActionGroup', function () {
892892
expect(buttons[1]).toHaveAttribute('aria-haspopup', 'true');
893893
expect(buttons[1]).toBeDisabled();
894894
});
895+
896+
it('menu items should be disabled for items listed in disabledKeys', function () {
897+
const handleOnAction = jest.fn();
898+
899+
render(
900+
<Provider theme={theme}>
901+
<ActionGroup overflowMode="collapse" onAction={handleOnAction} disabledKeys={['two', 'four']}>
902+
<Item key="one">One</Item>
903+
<Item key="two">Two</Item>
904+
<Item key="three">Three</Item>
905+
<Item key="four">Four</Item>
906+
</ActionGroup>
907+
</Provider>
908+
);
909+
910+
const actionGroup = screen.getByRole('toolbar');
911+
expect(within(actionGroup).getAllByRole('button')).toHaveLength(2);
912+
expect(within(actionGroup).getByRole('button', {name: 'One'})).toBeVisible();
913+
914+
const moreButton = within(actionGroup).getByRole('button', {name: '…'});
915+
expect(moreButton).toBeVisible();
916+
917+
triggerPress(moreButton);
918+
919+
const menu = screen.getByRole('menu');
920+
expect(within(menu).getAllByRole('menuitem')).toHaveLength(3);
921+
922+
const itemTwo = within(menu).getByRole('menuitem', {name: 'Two'});
923+
expect(itemTwo).toBeVisible();
924+
expect(itemTwo).toHaveAttribute('aria-disabled', 'true');
925+
926+
const itemThree = within(menu).getByRole('menuitem', {name: 'Three'});
927+
expect(itemThree).toBeVisible();
928+
expect(itemThree).toHaveAttribute('aria-disabled', 'false');
929+
930+
const itemFour = within(menu).getByRole('menuitem', {name: 'Four'});
931+
expect(itemFour).toBeVisible();
932+
expect(itemFour).toHaveAttribute('aria-disabled', 'true');
933+
934+
triggerPress(itemTwo);
935+
expect(handleOnAction).not.toHaveBeenCalled();
936+
937+
triggerPress(itemFour);
938+
expect(handleOnAction).not.toHaveBeenCalled();
939+
940+
triggerPress(itemThree);
941+
expect(handleOnAction).toHaveBeenCalled();
942+
});
895943
});
896944

897945
describe('buttonLabelBehavior', function () {

0 commit comments

Comments
 (0)