-
Notifications
You must be signed in to change notification settings - Fork 126
/
MenuItem.tsx
236 lines (203 loc) · 6.2 KB
/
MenuItem.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import React from 'react';
import PropTypes from 'prop-types';
import { isFunction, isReactUIComponent } from '../../lib/utils';
import { ThemeContext } from '../../lib/theming/ThemeContext';
import { Theme } from '../../lib/theming/Theme';
import { CommonProps, CommonWrapper, CommonWrapperRestProps } from '../../internal/CommonWrapper';
import { cx } from '../../lib/theming/Emotion';
import { rootNode, TSetRootNode } from '../../lib/rootNode/rootNodeDecorator';
import { styles } from './MenuItem.styles';
export type MenuItemState = null | 'hover' | 'selected' | void;
export interface MenuItemProps extends CommonProps {
/**
* @ignore
*/
_enableIconPadding?: boolean;
/**
* Добавляет описание для элемента меню.
*/
comment?: React.ReactNode;
/**
* Отключенное состояние.
*/
disabled?: boolean;
/**
* Добавляет элементу меню иконку.
*/
icon?: React.ReactElement<any>;
/**
* Меняет цвет текста на синий.
*/
link?: boolean;
/**
* @ignore
*/
loose?: boolean;
/**
* @ignore
*/
state?: MenuItemState;
/**
* HTML-событие `onclick`.
*/
onClick?: (event: React.SyntheticEvent<HTMLElement>) => void;
/**
* HTML-событие `mouseenter`.
*/
onMouseEnter?: React.MouseEventHandler;
/**
* HTML-событие `mouseleave`.
*/
onMouseLeave?: React.MouseEventHandler;
children?: React.ReactNode | ((state: MenuItemState) => React.ReactNode);
/**
* HTML-атрибут `target`.
*/
target?: React.AnchorHTMLAttributes<HTMLAnchorElement>['target'];
/**
* HTML-атрибут `title`.
*/
title?: React.AnchorHTMLAttributes<HTMLAnchorElement>['title'];
/**
* HTML-атрибут `href`.
*/
href?: React.AnchorHTMLAttributes<HTMLAnchorElement>['href'];
/**
* Заменяет корневой элемент, на компонент переданный в проп.
*
* По умолчанию корневой элемент рендерится как `button`. <br />Если передан `href`, то вместо `button` рендерится `a`.
*/
component?: React.ComponentType<any>;
isMobile?: boolean;
}
export const MenuItemDataTids = {
root: 'MenuItem__root',
comment: 'MenuItem__comment',
} as const;
/**
*
* `MenuItem` - это вложенный компонент, задающий базовые стили для элемента меню и позволяющий навигироваться по элементам меню с помощью клавиатуры.
*
* Сущности в которых может быть использован `MenuItem`: [`DropdownMenu`](#/Components/DropdownMenu), [`Kebab`](#/Components/Kebab), [`TooltipMenu`](#/Components/TooltipMenu) и [`Select`](#/Components/Select).
*/
@rootNode
export class MenuItem extends React.Component<MenuItemProps> {
public static __KONTUR_REACT_UI__ = 'MenuItem';
public static __MENU_ITEM__ = true;
public static propTypes = {
comment: PropTypes.node,
disabled: PropTypes.bool,
href: PropTypes.string,
icon: PropTypes.node,
loose: PropTypes.bool,
state: PropTypes.string,
target: PropTypes.string,
onClick: PropTypes.func,
};
private theme!: Theme;
private mouseEntered = false;
private setRootNode!: TSetRootNode;
public render() {
return (
<ThemeContext.Consumer>
{(theme) => {
this.theme = theme;
return (
<CommonWrapper rootNodeRef={this.setRootNode} {...this.props}>
{this.renderMain}
</CommonWrapper>
);
}}
</ThemeContext.Consumer>
);
}
private renderMain = (props: CommonWrapperRestProps<MenuItemProps>) => {
const {
link,
comment,
icon,
loose,
state,
_enableIconPadding,
component,
onMouseEnter,
onMouseLeave,
isMobile,
...rest
} = props;
const hover = state === 'hover' && !this.props.disabled;
let iconElement = null;
if (icon) {
iconElement = <div className={styles.icon(this.theme)}>{icon}</div>;
}
const className = cx({
[styles.root(this.theme)]: true,
[styles.rootMobile(this.theme)]: isMobile,
[styles.loose()]: !!loose,
[styles.hover(this.theme)]: hover,
[styles.selected(this.theme)]: state === 'selected',
[styles.link(this.theme)]: !!link,
[styles.withIcon(this.theme)]: Boolean(iconElement) || !!_enableIconPadding,
[styles.disabled(this.theme)]: !!this.props.disabled,
});
const { children } = this.props;
let content = children;
if (isFunction(children)) {
content = children(this.props.state);
}
const Component = this.getComponent();
return (
<Component
data-tid={MenuItemDataTids.root}
{...rest}
state={state}
onMouseOver={this.handleMouseEnterFix}
onMouseLeave={this.handleMouseLeave}
className={className}
tabIndex={-1}
>
{iconElement}
{content}
{this.props.comment && (
<div
data-tid={MenuItemDataTids.comment}
className={cx({
[styles.comment(this.theme)]: true,
[styles.commentHover(this.theme)]: hover,
})}
>
{comment}
</div>
)}
</Component>
);
};
// https://github.com/facebook/react/issues/10109
// Mouseenter event not triggered when cursor moves from disabled button
private handleMouseEnterFix = (e: React.MouseEvent<HTMLElement>) => {
if (!this.mouseEntered && this.props.onMouseEnter) {
this.mouseEntered = true;
this.props.onMouseEnter(e);
}
};
private handleMouseLeave = (e: React.MouseEvent<HTMLElement>) => {
this.mouseEntered = false;
if (this.props.onMouseLeave) {
this.props.onMouseLeave(e);
}
};
private getComponent = () => {
const { disabled, component, href } = this.props;
if (disabled) {
return 'button';
}
if (component) {
return component;
}
if (href) {
return 'a';
}
return 'button';
};
}
export const isMenuItem = isReactUIComponent('MenuItem');