-
Notifications
You must be signed in to change notification settings - Fork 843
/
Copy pathcontext_menu_item.tsx
218 lines (199 loc) Β· 5.39 KB
/
context_menu_item.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
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, {
PropsWithChildren,
AnchorHTMLAttributes,
ButtonHTMLAttributes,
HTMLAttributes,
FunctionComponent,
ReactElement,
ReactNode,
Ref,
} from 'react';
import classNames from 'classnames';
import {
useEuiMemoizedStyles,
getSecureRelForTarget,
cloneElementWithCss,
} from '../../services';
import { validateHref } from '../../services/security/href_validator';
import { CommonProps, keysOf } from '../common';
import { EuiIcon } from '../icon';
import { EuiToolTip, EuiToolTipProps } from '../tool_tip';
import { euiContextMenuItemStyles } from './context_menu_item.styles';
export type EuiContextMenuItemIcon = ReactElement<any> | string | HTMLElement;
export type EuiContextMenuItemLayoutAlignment = 'center' | 'top' | 'bottom';
export const SIZES = ['s', 'm'] as const;
export interface EuiContextMenuItemProps
extends PropsWithChildren,
CommonProps {
icon?: EuiContextMenuItemIcon;
hasPanel?: boolean;
disabled?: boolean;
onClick?: (event: React.MouseEvent) => void;
buttonRef?: Ref<HTMLButtonElement>;
/**
* Required if using a tooltip. Add an optional tooltip on hover
*/
toolTipContent?: ReactNode;
/**
* Optional configuration to pass to the underlying [EuiToolTip](/#/display/tooltip).
* Accepts any prop that EuiToolTip does, except for `content` and `children`.
*/
toolTipProps?: Partial<Omit<EuiToolTipProps, 'content' | 'children'>>;
href?: string;
target?: string;
rel?: string;
/**
* How to align icon with content of button
*/
layoutAlign?: EuiContextMenuItemLayoutAlignment;
/**
* Reduce the size to `s` when in need of a more compressed menu
*/
size?: (typeof SIZES)[number];
}
type Props = CommonProps &
Omit<
ButtonHTMLAttributes<HTMLButtonElement>,
'type' | 'onClick' | 'disabled'
> &
EuiContextMenuItemProps;
const layoutAlignToClassNames: {
[align in EuiContextMenuItemLayoutAlignment]: string | null;
} = {
center: null,
top: 'euiContextMenu__itemLayout--top',
bottom: 'euiContextMenu__itemLayout--bottom',
};
export const LAYOUT_ALIGN = keysOf(layoutAlignToClassNames);
export const EuiContextMenuItem: FunctionComponent<Props> = ({
children,
className,
hasPanel,
icon,
buttonRef,
disabled: _disabled,
layoutAlign = 'center',
toolTipContent,
toolTipProps,
href,
target,
rel,
size = 'm',
...rest
}) => {
const isHrefValid = !href || validateHref(href);
const disabled = _disabled || !isHrefValid;
const classes = classNames('euiContextMenuItem', className);
const styles = useEuiMemoizedStyles(euiContextMenuItemStyles);
const cssStyles = [
styles.euiContextMenuItem,
styles.sizes[size],
styles.layoutAlign[layoutAlign],
disabled && styles.disabled,
];
const iconInstance =
icon &&
(typeof icon === 'string' ? (
<EuiIcon
type={icon}
size="m"
className="euiContextMenu__icon"
css={styles.euiContextMenu__icon}
color="inherit" // forces the icon to inherit its parent color
/>
) : (
// Assume it's already an instance of an icon.
cloneElementWithCss(icon as ReactElement, {
css: styles.euiContextMenu__icon,
})
));
const arrow = hasPanel && (
<EuiIcon
type="arrowRight"
size="m"
className="euiContextMenu__arrow"
css={styles.euiContextMenuItem__arrow}
/>
);
const textStyles = [
styles.text.euiContextMenuItem__text,
size === 's' && styles.text.s,
];
const buttonContent = (
<>
{iconInstance}
<span className="euiContextMenuItem__text" css={textStyles}>
{children}
</span>
{arrow}
</>
);
let button;
// <a> elements don't respect the `disabled` attribute. So if we're disabled, we'll just pretend
// this is a button and piggyback off its disabled styles.
if (href && !disabled) {
const secureRel = getSecureRelForTarget({ href, target, rel });
button = (
<a
css={cssStyles}
className={classes}
href={href}
target={target}
rel={secureRel}
ref={buttonRef as Ref<HTMLAnchorElement>}
{...(rest as AnchorHTMLAttributes<HTMLAnchorElement>)}
>
{buttonContent}
</a>
);
} else if (href || rest.onClick || toolTipContent) {
button = (
<button
disabled={disabled}
css={cssStyles}
className={classes}
type="button"
ref={buttonRef}
{...rest}
>
{buttonContent}
</button>
);
} else {
button = (
<div
css={cssStyles}
className={classes}
ref={buttonRef as Ref<HTMLDivElement>}
{...(rest as HTMLAttributes<HTMLDivElement>)}
>
{buttonContent}
</div>
);
}
if (toolTipContent) {
const anchorClasses = classNames(
'eui-displayBlock',
toolTipProps?.anchorClassName
);
return (
<EuiToolTip
position="right"
{...toolTipProps}
anchorClassName={anchorClasses}
content={toolTipContent}
>
{button}
</EuiToolTip>
);
} else {
return button;
}
};