Skip to content

Commit b995580

Browse files
authored
[material-ui][Accordion] Add region slot (#46659)
1 parent b572201 commit b995580

File tree

5 files changed

+55
-21
lines changed

5 files changed

+55
-21
lines changed

docs/pages/material-ui/api/accordion.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
"slotProps": {
1717
"type": {
1818
"name": "shape",
19-
"description": "{ heading?: func<br>&#124;&nbsp;object, root?: func<br>&#124;&nbsp;object, transition?: func<br>&#124;&nbsp;object }"
19+
"description": "{ heading?: func<br>&#124;&nbsp;object, region?: func<br>&#124;&nbsp;object, root?: func<br>&#124;&nbsp;object, transition?: func<br>&#124;&nbsp;object }"
2020
},
2121
"default": "{}"
2222
},
2323
"slots": {
2424
"type": {
2525
"name": "shape",
26-
"description": "{ heading?: elementType, root?: elementType, transition?: elementType }"
26+
"description": "{ heading?: elementType, region?: elementType, root?: elementType, transition?: elementType }"
2727
},
2828
"default": "{}"
2929
},
@@ -69,6 +69,12 @@
6969
"description": "The component that renders the transition.\n[Follow this guide](https://mui.com/material-ui/transitions/#transitioncomponent-prop) to learn more about the requirements for this component.",
7070
"default": "Collapse",
7171
"class": null
72+
},
73+
{
74+
"name": "region",
75+
"description": "The component that renders the region.",
76+
"default": "'div'",
77+
"class": "MuiAccordion-region"
7278
}
7379
],
7480
"classes": [
@@ -90,12 +96,6 @@
9096
"description": "Styles applied to the root element unless `disableGutters={true}`.",
9197
"isGlobal": false
9298
},
93-
{
94-
"key": "region",
95-
"className": "MuiAccordion-region",
96-
"description": "Styles applied to the region element, the container of the children.",
97-
"isGlobal": false
98-
},
9999
{
100100
"key": "rounded",
101101
"className": "MuiAccordion-rounded",

docs/translations/api-docs/accordion/accordion.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@
5353
"nodeName": "the root element",
5454
"conditions": "<code>disableGutters={true}</code>"
5555
},
56-
"region": {
57-
"description": "Styles applied to {{nodeName}}, {{conditions}}.",
58-
"nodeName": "the region element",
59-
"conditions": "the container of the children"
60-
},
6156
"rounded": {
6257
"description": "Styles applied to {{nodeName}} unless {{conditions}}.",
6358
"nodeName": "the root element",
@@ -66,6 +61,7 @@
6661
},
6762
"slotDescriptions": {
6863
"heading": "The component that renders the heading.",
64+
"region": "The component that renders the region.",
6965
"root": "The component that renders the root.",
7066
"transition": "The component that renders the transition. <a href=\"https://mui.com/material-ui/transitions/#transitioncomponent-prop\">Follow this guide</a> to learn more about the requirements for this component."
7167
}

packages/mui-material/src/Accordion/Accordion.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@ export interface AccordionSlots {
2424
* @default Collapse
2525
*/
2626
transition: React.ElementType;
27+
/**
28+
* The component that renders the region.
29+
* @default 'div'
30+
*/
31+
region: React.ElementType;
2732
}
2833

2934
export interface AccordionRootSlotPropsOverrides {}
3035
export interface AccordionHeadingSlotPropsOverrides {}
3136
export interface AccordionTransitionSlotPropsOverrides {}
37+
export interface AccordionRegionSlotPropsOverrides {}
3238

3339
export type AccordionSlotsAndSlotProps = CreateSlotsAndSlotProps<
3440
AccordionSlots,
@@ -56,6 +62,11 @@ export type AccordionSlotsAndSlotProps = CreateSlotsAndSlotProps<
5662
TransitionProps & AccordionTransitionSlotPropsOverrides,
5763
AccordionOwnerState
5864
>;
65+
/**
66+
* Props forwarded to the region slot.
67+
* By default, the available props are based on the div element.
68+
*/
69+
region: SlotProps<'div', AccordionRegionSlotPropsOverrides, AccordionOwnerState>;
5970
}
6071
>;
6172

packages/mui-material/src/Accordion/Accordion.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ const AccordionHeading = styled('h3', {
133133
all: 'unset',
134134
});
135135

136+
const AccordionRegion = styled('div', {
137+
name: 'MuiAccordion',
138+
slot: 'Region',
139+
})({});
140+
136141
const Accordion = React.forwardRef(function Accordion(inProps, ref) {
137142
const props = useDefaultProps({ props: inProps, name: 'MuiAccordion' });
138143
const {
@@ -221,20 +226,25 @@ const Accordion = React.forwardRef(function Accordion(inProps, ref) {
221226
ownerState,
222227
});
223228

229+
const [AccordionRegionSlot, accordionRegionProps] = useSlot('region', {
230+
elementType: AccordionRegion,
231+
externalForwardedProps,
232+
ownerState,
233+
className: classes.region,
234+
additionalProps: {
235+
'aria-labelledby': summary.props.id,
236+
id: summary.props['aria-controls'],
237+
role: 'region',
238+
},
239+
});
240+
224241
return (
225242
<RootSlot {...rootProps}>
226243
<AccordionHeadingSlot {...accordionProps}>
227244
<AccordionContext.Provider value={contextValue}>{summary}</AccordionContext.Provider>
228245
</AccordionHeadingSlot>
229246
<TransitionSlot in={expanded} timeout="auto" {...transitionProps}>
230-
<div
231-
aria-labelledby={summary.props.id}
232-
id={summary.props['aria-controls']}
233-
role="region"
234-
className={classes.region}
235-
>
236-
{children}
237-
</div>
247+
<AccordionRegionSlot {...accordionRegionProps}>{children}</AccordionRegionSlot>
238248
</TransitionSlot>
239249
</RootSlot>
240250
);
@@ -304,6 +314,7 @@ Accordion.propTypes /* remove-proptypes */ = {
304314
*/
305315
slotProps: PropTypes.shape({
306316
heading: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
317+
region: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
307318
root: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
308319
transition: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
309320
}),
@@ -313,6 +324,7 @@ Accordion.propTypes /* remove-proptypes */ = {
313324
*/
314325
slots: PropTypes.shape({
315326
heading: PropTypes.elementType,
327+
region: PropTypes.elementType,
316328
root: PropTypes.elementType,
317329
transition: PropTypes.elementType,
318330
}),

packages/mui-material/src/Accordion/Accordion.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ describe('<Accordion />', () => {
4848
expectedClassName: classes.root,
4949
testWithElement: CustomPaper,
5050
},
51+
region: {
52+
expectedClassName: classes.region,
53+
testWithElement: 'div',
54+
},
5155
},
5256
skip: ['componentProp', 'componentsProp'],
5357
}));
@@ -316,4 +320,15 @@ describe('<Accordion />', () => {
316320
});
317321
});
318322
});
323+
324+
it('should allow custom role for region slot via slotProps', () => {
325+
render(
326+
<Accordion expanded slotProps={{ region: { role: 'list', 'data-testid': 'region-slot' } }}>
327+
<AccordionSummary>Summary</AccordionSummary>
328+
Details
329+
</Accordion>,
330+
);
331+
332+
expect(screen.getByTestId('region-slot')).to.have.attribute('role', 'list');
333+
});
319334
});

0 commit comments

Comments
 (0)