forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpandableSectionToggle.tsx
More file actions
102 lines (97 loc) · 3.93 KB
/
Copy pathExpandableSectionToggle.tsx
File metadata and controls
102 lines (97 loc) · 3.93 KB
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
99
100
101
102
import styles from '@patternfly/react-styles/css/components/ExpandableSection/expandable-section';
import { css } from '@patternfly/react-styles';
import RhUiCaretDownIcon from '@patternfly/react-icons/dist/esm/icons/rh-ui-caret-down-icon';
import { Button } from '../Button';
/** Acts as the toggle sub-component when the main expandable section component has the isDetached
* property passed in. Allows for more custom control over the expandable section's toggle.
*/
export interface ExpandableSectionToggleProps extends Omit<React.HTMLProps<HTMLDivElement>, 'onToggle'> {
/** Content rendered inside the expandable toggle. */
children?: React.ReactNode;
/** Additional classes added to the expandable toggle. */
className?: string;
/** Id of the toggle's respective expandable section content. The value passed into this
* property should match the contentId property of the main expandable section component.
*/
contentId?: string;
/** Id of the toggle. The value passed into this property should match the aria-labelledby
* property of the main expandable section component.
*/
toggleId?: string;
/** Direction the toggle arrow should point when the expandable section is expanded. */
direction?: 'up' | 'down';
/** Flag to determine toggle styling when the expandable content is truncated. */
hasTruncatedContent?: boolean;
/** Flag indicating if the expandable section is expanded. */
isExpanded?: boolean;
/** Callback function to toggle the expandable content. */
onToggle?: (isExpanded: boolean) => void;
/** Flag indicating that the expandable section and expandable toggle are detached from one another. */
isDetached?: boolean;
/** Accessible name via human readable string for the expandable section toggle. */
toggleAriaLabel?: string;
/** Accessible name via space delimtted list of IDs for the expandable section toggle. */
toggleAriaLabelledBy?: string;
/** The HTML element to use for the toggle wrapper. Can be 'div' (default) or any heading level.
* When using heading elements, the button will be rendered inside the heading for proper semantics.
* This is useful when the toggle text should function as a heading in the document structure.
*/
toggleWrapper?: 'div' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6';
}
export const ExpandableSectionToggle: React.FunctionComponent<ExpandableSectionToggleProps> = ({
children,
className = '',
isExpanded = false,
onToggle,
contentId,
toggleId,
direction = 'down',
hasTruncatedContent = false,
isDetached,
toggleAriaLabel,
toggleAriaLabelledBy,
toggleWrapper = 'div',
...props
}: ExpandableSectionToggleProps) => {
const ToggleWrapper = toggleWrapper as any;
return (
<div
className={css(
styles.expandableSection,
isExpanded && styles.modifiers.expanded,
hasTruncatedContent && styles.modifiers.truncate,
isDetached && 'pf-m-detached',
className
)}
{...props}
>
<ToggleWrapper className={`${styles.expandableSection}__toggle`}>
<Button
variant="link"
{...(hasTruncatedContent && { isInline: true })}
aria-expanded={isExpanded}
aria-controls={contentId}
onClick={() => onToggle(!isExpanded)}
id={toggleId}
{...(!hasTruncatedContent && {
icon: (
<span
className={css(
styles.expandableSectionToggleIcon,
isExpanded && direction === 'up' && styles.modifiers.expandTop // TODO: next breaking change move this class to the outer styles.expandableSection wrapper
)}
>
<RhUiCaretDownIcon />
</span>
)
})}
aria-label={toggleAriaLabel}
aria-labelledby={toggleAriaLabelledBy}
>
{children}
</Button>
</ToggleWrapper>
</div>
);
};
ExpandableSectionToggle.displayName = 'ExpandableSectionToggle';