-
Notifications
You must be signed in to change notification settings - Fork 126
/
Kebab.tsx
234 lines (208 loc) · 6.75 KB
/
Kebab.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
import React from 'react';
import PropTypes from 'prop-types';
import { ReactNodePropTypes } from '../../lib/utils';
import { isKeyArrowVertical, isKeyEnter, isKeySpace, someKeys } from '../../lib/events/keyboard/identifiers';
import * as LayoutEvents from '../../lib/LayoutEvents';
import { keyListener } from '../../lib/events/keyListener';
import { PopupMenu, PopupMenuCaptionProps, PopupMenuProps } from '../../internal/PopupMenu';
import { Nullable } from '../../typings/utility-types';
import { PopupPositionsType } from '../../internal/Popup';
import { ThemeContext } from '../../lib/theming/ThemeContext';
import { Theme } from '../../lib/theming/Theme';
import { MenuKebabIcon } from '../../internal/icons/16px';
import { isTestEnv } from '../../lib/currentEnvironment';
import { ThemeFactory } from '../../lib/theming/ThemeFactory';
import { CommonWrapper, CommonProps } from '../../internal/CommonWrapper';
import { cx } from '../../lib/theming/Emotion';
import { rootNode, TSetRootNode } from '../../lib/rootNode';
import { styles } from './Kebab.styles';
export interface KebabProps extends CommonProps, Pick<PopupMenuProps, 'onOpen' | 'onClose'> {
disabled?: boolean;
size: 'small' | 'medium' | 'large';
/**
* Список позиций доступных для расположения выпадашки.
*
* Если во всех позициях выпадашка вылезает за пределы `viewport`, будет использована первая из этого списка.
*
* **Возможные значения**: `top left`, `top center`, `top right`, `right top`, `right middle`, `right bottom`, `bottom left`, `bottom center`, `bottom right`, `left top`, `left middle`, `left bottom`
* @default ['bottom left', 'bottom right', 'top left', 'top right']
*/
positions: PopupPositionsType[];
menuMaxHeight?: number | string;
/**
* Не показывать анимацию
*/
disableAnimations: boolean;
/**
* Кастомная иконка
*/
icon?: React.ReactNode;
}
export interface KebabState {
anchor: Nullable<HTMLElement>;
focusedByTab: boolean;
}
@rootNode
export class Kebab extends React.Component<KebabProps, KebabState> {
public static __KONTUR_REACT_UI__ = 'Kebab';
public static propTypes = {};
public static defaultProps = {
onOpen: () => undefined,
onClose: () => undefined,
positions: ['bottom left', 'bottom right', 'top left', 'top right'],
size: 'small',
disableAnimations: isTestEnv,
icon: <MenuKebabIcon />,
};
public state = {
focusedByTab: false,
anchor: null,
};
private theme!: Theme;
private setRootNode!: TSetRootNode;
private listener: {
remove: () => void;
} = {
remove: () => undefined,
};
public componentDidMount() {
/** addListener'у нужен колбэк в аргумент */
this.listener = LayoutEvents.addListener(() => undefined);
}
public componentWillUnmount() {
this.listener.remove();
}
public render(): JSX.Element {
return (
<ThemeContext.Consumer>
{(theme) => {
this.theme = theme;
return (
<ThemeContext.Provider
value={ThemeFactory.create(
{
popupPinOffset: theme.kebabPinOffset,
popupMargin: theme.kebabMargin,
popupPinSize: theme.kebabPinSize,
},
theme,
)}
>
{this.renderMain()}
</ThemeContext.Provider>
);
}}
</ThemeContext.Consumer>
);
}
private renderMain() {
const { disabled, positions } = this.props;
return (
<CommonWrapper rootNodeRef={this.setRootNode} {...this.props}>
<PopupMenu
popupHasPin
positions={positions}
onChangeMenuState={this.handleChangeMenuState}
caption={this.renderCaption}
disableAnimations={this.props.disableAnimations}
menuMaxHeight={this.props.menuMaxHeight}
onOpen={this.props.onOpen}
onClose={this.props.onClose}
>
{!disabled && this.props.children}
</PopupMenu>
</CommonWrapper>
);
}
private renderCaption = (captionProps: PopupMenuCaptionProps) => {
const { disabled } = this.props;
const handleCaptionKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
if (!disabled) {
this.handleCaptionKeyDown(event, captionProps.openMenu);
}
};
const handleCaptionClick = () => {
if (!disabled) {
captionProps.toggleMenu();
}
};
return (
<span
tabIndex={disabled ? -1 : 0}
onClick={handleCaptionClick}
onKeyDown={handleCaptionKeyDown}
onFocus={this.handleFocus}
onBlur={this.handleBlur}
className={cx({
[styles.kebab(this.theme)]: true,
[styles.opened(this.theme)]: captionProps.opened,
[styles.disabled()]: disabled,
[styles.focused(this.theme)]: this.state.focusedByTab,
})}
>
{this.renderIcon()}
</span>
);
};
private handleCaptionKeyDown = (
e: React.KeyboardEvent<HTMLDivElement>,
openMenu: PopupMenuCaptionProps['openMenu'],
) => {
if (someKeys(isKeyEnter, isKeySpace, isKeyArrowVertical)(e)) {
e.preventDefault();
openMenu(true);
}
};
private handleChangeMenuState = (isOpened: boolean, restoreFocus: boolean): void => {
this.setState({
focusedByTab: !isOpened && restoreFocus,
});
};
private handleFocus = () => {
if (!this.props.disabled) {
// focus event fires before keyDown eventlistener
// so we should check tabPressed in async way
requestAnimationFrame(() => {
if (keyListener.isTabPressed) {
this.setState({ focusedByTab: true });
}
});
}
};
private handleBlur = () => {
this.setState({
focusedByTab: false,
});
};
private renderIcon() {
return (
<div
className={cx({
[styles.icon()]: true,
[styles.iconsmall()]: this.props.size === 'small',
[styles.iconmedium()]: this.props.size === 'medium',
[styles.iconlarge()]: this.props.size === 'large',
})}
>
{this.props.icon}
</div>
);
}
}
Kebab.propTypes = {
children: ReactNodePropTypes,
disabled: PropTypes.bool,
menuMaxHeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
/**
* Размер кебаба small 14px | large 20px
*/
size: PropTypes.string,
/**
* Коллбек, вызывающийся перед закрытием кебаба
*/
onClose: PropTypes.func,
/**
* Коллбек, вызывающийся перед открытием кебаба
*/
onOpen: PropTypes.func,
};