-
Notifications
You must be signed in to change notification settings - Fork 113
/
AccordionItemHeading.wrapper.tsx
77 lines (64 loc) · 2.28 KB
/
AccordionItemHeading.wrapper.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import * as React from 'react';
import {
CONTEXT_KEY as ACCORDION_CONTEXT_KEY,
getAccordionStore,
Item,
} from '../AccordionContainer/AccordionContainer';
import * as propTypes from '../helpers/propTypes';
import {
CONTEXT_KEY as ITEM_CONTEXT_KEY,
getItemStore,
} from '../ItemContainer/ItemContainer';
import { default as AccordionItemHeading } from './AccordionItemHeading';
type AccordionItemHeadingWrapperProps = React.HTMLAttributes<HTMLDivElement> & {
expandedClassName: string;
};
type AccordionItemHeadingWrapperState = {};
type AccordionItemHeadingWrapperContext = {
[ACCORDION_CONTEXT_KEY](): null;
[ITEM_CONTEXT_KEY](): null;
};
export default class AccordionItemHeadingWrapper extends React.Component<
AccordionItemHeadingWrapperProps
> {
static contextTypes: AccordionItemHeadingWrapperContext = {
[ACCORDION_CONTEXT_KEY]: propTypes.wildcard,
[ITEM_CONTEXT_KEY]: propTypes.wildcard,
};
static defaultProps: AccordionItemHeadingWrapperProps = {
className: 'accordion__heading',
expandedClassName: '',
};
render(): JSX.Element {
const accordionStore = getAccordionStore(this.context);
if (!accordionStore) {
// tslint:disable-next-line:no-console
console.error(
'AccordionItemHeading component cannot render because it has not been nested inside an Accordion component.',
);
return null;
}
const itemStore = getItemStore(this.context);
if (!itemStore) {
// tslint:disable-next-line:no-console
console.error(
'AccordionItemHeading component cannot render because it has not been nested inside an AccordionItem component.',
);
return null;
}
const { uuid } = itemStore;
const { items, allowZeroExpanded, isItemDisabled } = accordionStore;
const item = items.filter(
(stateItem: Item) => stateItem.uuid === uuid,
)[0];
const disabled = isItemDisabled(uuid);
return (
<AccordionItemHeading
{...this.props}
{...item}
disabled={disabled}
setExpanded={accordionStore.setExpanded}
/>
);
}
}