forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuToggle.tsx
219 lines (203 loc) · 6.87 KB
/
MenuToggle.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import * as React from 'react';
import styles from '@patternfly/react-styles/css/components/MenuToggle/menu-toggle';
import { css } from '@patternfly/react-styles';
import CaretDownIcon from '@patternfly/react-icons/dist/esm/icons/caret-down-icon';
import { BadgeProps } from '../Badge';
import CheckCircleIcon from '@patternfly/react-icons/dist/esm/icons/check-circle-icon';
import ExclamationCircleIcon from '@patternfly/react-icons/dist/esm/icons/exclamation-circle-icon';
import ExclamationTriangleIcon from '@patternfly/react-icons/dist/esm/icons/exclamation-triangle-icon';
export enum MenuToggleStatus {
success = 'success',
danger = 'danger',
warning = 'warning'
}
export type MenuToggleElement = HTMLDivElement | HTMLButtonElement;
export interface SplitButtonOptions {
/** Elements to display before the toggle button. When included, renders the menu toggle as a split button. */
items: React.ReactNode[];
/** Variant of split button toggle */
variant?: 'action' | 'checkbox';
}
export interface MenuToggleProps
extends Omit<
React.DetailedHTMLProps<
React.ButtonHTMLAttributes<HTMLButtonElement> & React.HTMLAttributes<HTMLDivElement>,
MenuToggleElement
>,
'ref'
> {
/** Content rendered inside the toggle */
children?: React.ReactNode;
/** Additional classes added to the toggle */
className?: string;
/** Flag indicating the toggle has expanded styling */
isExpanded?: boolean;
/** Flag indicating the toggle is disabled */
isDisabled?: boolean;
/** Flag indicating the toggle is full height */
isFullHeight?: boolean;
/** Flag indicating the toggle takes up the full width of its parent */
isFullWidth?: boolean;
/** Object used to configure a split button menu toggle */
splitButtonOptions?: SplitButtonOptions;
/** Variant styles of the menu toggle */
variant?: 'default' | 'plain' | 'primary' | 'plainText' | 'secondary' | 'typeahead';
/** @beta Status styles of the menu toggle */
status?: 'success' | 'warning' | 'danger';
/** Overrides the status icon */
statusIcon?: React.ReactNode;
/** Optional icon or image rendered inside the toggle, before the children content. It is
* recommended to wrap most basic icons in our icon component.
*/
icon?: React.ReactNode;
/** Optional badge rendered inside the toggle, after the children content */
badge?: BadgeProps | React.ReactNode;
/** @hide Forwarded ref */
innerRef?: React.Ref<MenuToggleElement>;
}
class MenuToggleBase extends React.Component<MenuToggleProps> {
displayName = 'MenuToggleBase';
static defaultProps: MenuToggleProps = {
className: '',
isExpanded: false,
isDisabled: false,
isFullWidth: false,
isFullHeight: false
};
render() {
const {
children,
className,
icon,
badge,
isExpanded,
isDisabled,
isFullHeight,
isFullWidth,
splitButtonOptions,
variant,
status,
statusIcon,
innerRef,
onClick,
'aria-label': ariaLabel,
...otherProps
} = this.props;
const isPlain = variant === 'plain';
const isPlainText = variant === 'plainText';
const isTypeahead = variant === 'typeahead';
let _statusIcon = statusIcon;
if (!statusIcon) {
switch (status) {
case MenuToggleStatus.success:
_statusIcon = <CheckCircleIcon aria-hidden="true" />;
break;
case MenuToggleStatus.warning:
_statusIcon = <ExclamationTriangleIcon aria-hidden="true" />;
break;
case MenuToggleStatus.danger:
_statusIcon = <ExclamationCircleIcon aria-hidden="true" />;
break;
}
}
const toggleControls = (
<span className={css(styles.menuToggleControls)}>
{status !== undefined && <span className={css(styles.menuToggleStatusIcon)}>{_statusIcon}</span>}
<span className={css(styles.menuToggleToggleIcon)}>
<CaretDownIcon aria-hidden />
</span>
</span>
);
const content = (
<>
{icon && <span className={css(styles.menuToggleIcon)}>{icon}</span>}
{isTypeahead ? children : <span className={css(styles.menuToggleText)}>{children}</span>}
{React.isValidElement(badge) && <span className={css(styles.menuToggleCount)}>{badge}</span>}
{isTypeahead ? (
<button
type="button"
className={css(styles.menuToggleButton)}
aria-expanded={isExpanded}
onClick={onClick}
aria-label={ariaLabel || 'Menu toggle'}
tabIndex={-1}
>
{toggleControls}
</button>
) : (
toggleControls
)}
</>
);
const commonStyles = css(
styles.menuToggle,
isExpanded && styles.modifiers.expanded,
variant === 'primary' && styles.modifiers.primary,
variant === 'secondary' && styles.modifiers.secondary,
status && styles.modifiers[status],
(isPlain || isPlainText) && styles.modifiers.plain,
isPlainText && styles.modifiers.text,
isFullHeight && styles.modifiers.fullHeight,
isFullWidth && styles.modifiers.fullWidth,
isDisabled && styles.modifiers.disabled,
className
);
const componentProps = {
children: isPlain ? children : content,
...(isDisabled && { disabled: true }),
...otherProps
};
if (isTypeahead) {
return (
<div
ref={innerRef as React.Ref<HTMLDivElement>}
className={css(commonStyles, styles.modifiers.typeahead)}
{...componentProps}
/>
);
}
if (splitButtonOptions) {
return (
<div
ref={innerRef as React.Ref<HTMLDivElement>}
className={css(
commonStyles,
styles.modifiers.splitButton,
splitButtonOptions?.variant === 'action' && styles.modifiers.action
)}
>
{splitButtonOptions?.items}
<button
className={css(styles.menuToggleButton)}
type="button"
aria-expanded={isExpanded}
aria-label={ariaLabel}
disabled={isDisabled}
onClick={onClick}
{...(children && { style: { display: 'flex', paddingLeft: 'var(--pf-v5-global--spacer--sm)' } })}
{...otherProps}
>
{children && <span className={css(styles.menuToggleText)}>{children}</span>}
{toggleControls}
</button>
</div>
);
}
return (
<button
className={css(commonStyles)}
type="button"
aria-label={ariaLabel}
aria-expanded={isExpanded}
ref={innerRef as React.Ref<HTMLButtonElement>}
disabled={isDisabled}
onClick={onClick}
{...componentProps}
/>
);
}
}
export const MenuToggle = React.forwardRef((props: MenuToggleProps, ref: React.Ref<MenuToggleElement>) => (
<MenuToggleBase innerRef={ref} {...props} />
));
MenuToggle.displayName = 'MenuToggle';