-
Notifications
You must be signed in to change notification settings - Fork 152
/
collapsible-component-cards.tsx
98 lines (87 loc) · 3.23 KB
/
collapsible-component-cards.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import { css } from '@uifabric/utilities';
import { NamedFC } from 'common/react/named-fc';
import { ActionButton } from 'office-ui-fabric-react';
import * as React from 'react';
import { SetFocusVisibility } from 'types/set-focus-visibility';
import * as styles from './collapsible-component-cards.scss';
export const collapsibleButtonAutomationId = 'collapsible-component-cards-button';
export type CollapsibleComponentCardsDeps = {
setFocusVisibility: SetFocusVisibility;
};
export interface CollapsibleComponentCardsProps {
id: string;
header: JSX.Element;
content: JSX.Element;
headingLevel: number;
contentClassName?: string;
containerAutomationId?: string;
containerClassName?: string;
buttonAriaLabel?: string;
deps: CollapsibleComponentCardsDeps;
onExpandToggle: (event: React.MouseEvent<HTMLDivElement>) => void;
isExpanded?: boolean;
}
const CollapsibleComponentCards = NamedFC<CollapsibleComponentCardsProps>(
'CollapsibleComponentCards',
(props: CollapsibleComponentCardsProps) => {
const {
headingLevel,
contentClassName,
content,
isExpanded,
deps,
buttonAriaLabel,
containerAutomationId,
containerClassName,
header,
onExpandToggle: onExpandToggle,
} = props;
const containerProps = { role: 'heading', 'aria-level': headingLevel };
let contentWrapper: JSX.Element | null = null;
let collapsedCSSClassName: string | null = 'collapsed';
const showContent = isExpanded || false;
if (showContent) {
contentWrapper = (
<div className={css(contentClassName, styles.collapsibleContainerContent)}>
{content}
</div>
);
collapsedCSSClassName = null;
}
const onClick = (event: React.MouseEvent<HTMLDivElement>) => {
if (event.nativeEvent.detail === 0) {
// 0 => keyboard event
deps.setFocusVisibility(true);
}
onExpandToggle(event);
};
return (
<div
data-automation-id={containerAutomationId}
className={css(
containerClassName,
styles.collapsibleContainer,
collapsedCSSClassName,
)}
>
<div {...containerProps}>
<ActionButton
data-automation-id={collapsibleButtonAutomationId}
className={styles.collapsibleControl}
onClick={onClick}
aria-expanded={showContent}
ariaLabel={buttonAriaLabel}
>
<span className={styles.collapsibleTitle}>{header}</span>
</ActionButton>
</div>
{contentWrapper}
</div>
);
},
);
export const CardsCollapsibleControl = (
collapsibleControlProps: CollapsibleComponentCardsProps,
) => <CollapsibleComponentCards {...collapsibleControlProps} />;