11import * as React from 'react' ;
2- import { InjectedHeadingAttributes } from '../helpers/AccordionStore' ;
32import DisplayName from '../helpers/DisplayName' ;
4- import { DivAttributes } from '../helpers/types' ;
53import { assertValidHtmlId } from '../helpers/id' ;
4+ import { HeadingAttributes } from '../helpers/types' ;
65
7- import { Consumer as ItemConsumer , ItemContext } from './ItemContext' ;
8-
9- type Props = DivAttributes ;
10-
11- const defaultProps = {
12- className : 'accordion__heading' ,
13- 'aria-level' : 3 ,
14- } ;
6+ interface AccordianItemHeadingProps extends HeadingAttributes {
7+ className ?: string ;
8+ headingLevel ?: number ;
9+ }
1510
1611export const SPEC_ERROR = `AccordionItemButton may contain only one child element, which must be an instance of AccordionItemButton.
1712
@@ -21,12 +16,31 @@ From the WAI-ARIA spec (https://www.w3.org/TR/wai-aria-practices-1.1/#accordion)
2116
2217` ;
2318
24- export class AccordionItemHeading extends React . PureComponent < Props > {
25- static defaultProps : typeof defaultProps = defaultProps ;
19+ const Heading = React . forwardRef < HTMLHeadingElement , AccordianItemHeadingProps > (
20+ (
21+ {
22+ headingLevel = 3 ,
23+ className = 'accordion__heading' ,
24+ ...props
25+ } : AccordianItemHeadingProps ,
26+ ref ,
27+ ) => {
28+ const HeadingTag = `h${ headingLevel } ` ;
29+ return React . createElement ( HeadingTag , {
30+ className,
31+ ...props ,
32+ ref,
33+ 'data-accordion-component' : 'AccordionItemHeading' ,
34+ } ) ;
35+ } ,
36+ ) ;
37+
38+ Heading . displayName = 'Heading' ;
2639
27- ref : HTMLDivElement | undefined ;
40+ export class AccordionItemHeading extends React . PureComponent < AccordianItemHeadingProps > {
41+ ref : HTMLHeadingElement | undefined ;
2842
29- static VALIDATE ( ref : HTMLDivElement | undefined ) : void | never {
43+ static VALIDATE ( ref : HTMLHeadingElement | undefined ) : void | never {
3044 if ( ref === undefined ) {
3145 throw new Error ( 'ref is undefined' ) ;
3246 }
@@ -43,7 +57,7 @@ export class AccordionItemHeading extends React.PureComponent<Props> {
4357 }
4458 }
4559
46- setRef = ( ref : HTMLDivElement ) : void => {
60+ setRef = ( ref : HTMLHeadingElement ) : void => {
4761 this . ref = ref ;
4862 } ;
4963
@@ -56,36 +70,19 @@ export class AccordionItemHeading extends React.PureComponent<Props> {
5670 }
5771
5872 render ( ) : JSX . Element {
59- return (
60- < div
61- data-accordion-component = "AccordionItemHeading"
62- { ...this . props }
63- ref = { this . setRef }
64- />
65- ) ;
73+ return < Heading ref = { this . setRef } { ...this . props } /> ;
6674 }
6775}
6876
69- type WrapperProps = Pick <
70- DivAttributes ,
71- Exclude < keyof DivAttributes , keyof InjectedHeadingAttributes >
72- > ;
73-
74- const AccordionItemHeadingWrapper : React . SFC < DivAttributes > = (
75- props : WrapperProps ,
76- ) : JSX . Element => (
77- < ItemConsumer >
78- { ( itemContext : ItemContext ) : JSX . Element => {
79- const { headingAttributes } = itemContext ;
80-
81- if ( props . id ) {
82- assertValidHtmlId ( props . id ) ;
83- }
84-
85- return < AccordionItemHeading { ...props } { ...headingAttributes } /> ;
86- } }
87- </ ItemConsumer >
88- ) ;
77+ const AccordionItemHeadingWrapper : React . FC < AccordianItemHeadingProps > = (
78+ props : AccordianItemHeadingProps ,
79+ ) : JSX . Element => {
80+ if ( props . id ) {
81+ assertValidHtmlId ( props . id ) ;
82+ }
83+
84+ return < AccordionItemHeading { ...props } /> ;
85+ } ;
8986
9087AccordionItemHeadingWrapper . displayName = DisplayName . AccordionItemHeading ;
9188
0 commit comments