-
Notifications
You must be signed in to change notification settings - Fork 126
/
Tab.tsx
309 lines (265 loc) · 7.58 KB
/
Tab.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import React from 'react';
import PropTypes from 'prop-types';
import invariant from 'invariant';
import { ResizeDetector } from '../../internal/ResizeDetector';
import { isKeyArrow, isKeyArrowLeft, isKeyArrowUp } from '../../lib/events/keyboard/identifiers';
import { keyListener } from '../../lib/events/keyListener';
import { Nullable } from '../../typings/utility-types';
import { ComponentType, isFunctionalComponent } from '../../lib/utils';
import { ThemeContext } from '../../lib/theming/ThemeContext';
import { Theme } from '../../lib/theming/Theme';
import { CommonProps, CommonWrapper } from '../../internal/CommonWrapper';
import { cx } from '../../lib/theming/Emotion';
import { rootNode, TSetRootNode } from '../../lib/rootNode';
import { TabsContext, TabsContextType, TabsContextDefaultValue } from './TabsContext';
import { styles, horizontalStyles, verticalStyles, globalClasses } from './Tab.styles';
export interface TabIndicators {
error: boolean;
warning: boolean;
success: boolean;
primary: boolean;
disabled: boolean;
}
export interface TabProps<T extends string = string> extends CommonProps {
/**
* Tab content
*/
children?: React.ReactNode;
/**
* Component to use as a tab
*/
component?: React.ComponentType<any> | string;
/**
* Link href
*/
href?: string;
/**
* Tab identifier
*/
id?: T;
/**
* Click event
*/
onClick?: (event: React.MouseEvent<HTMLElement>) => void;
/**
* Click event
*/
onKeyDown?: (event: React.KeyboardEvent<HTMLElement>) => void;
/**
* Disabled indicator
*/
disabled?: boolean;
/**
* Состояние валидации при ошибке.
*/
error?: boolean;
/**
* Состояние валидации при предупреждении.
*/
warning?: boolean;
/**
* Success indicator
*/
success?: boolean;
/**
* Primary indicator
*/
primary?: boolean;
/**
* Style property
*/
style?: React.CSSProperties;
}
export interface TabState {
focusedByKeyboard: boolean;
}
/**
* Tab element of Tabs component
*
* Can be used for creating custom tabs
* ```js
*
* const RouteTab = (props) => (
* <Tab id={props.to} component={RouteLink} {...props}/>
* )
*
* const MyAwesomeTab = (props) => <Tab id={props.id}>8) {props.children}</Tab>
* ```
*
* Works only inside Tabs component, otherwise throws
*/
@rootNode
export class Tab<T extends string = string> extends React.Component<TabProps<T>, TabState> {
public static __KONTUR_REACT_UI__ = 'Tab';
public static contextType = TabsContext;
public context: TabsContextType = this.context;
public static propTypes = {
children: ComponentType,
disabled: PropTypes.bool,
href: PropTypes.string.isRequired,
onClick: PropTypes.func,
onKeyDown: PropTypes.func,
};
public static defaultProps = {
component: 'a',
href: '',
};
public state: TabState = {
focusedByKeyboard: false,
};
private theme!: Theme;
private tabComponent: Nullable<React.ReactElement<Tab<T>>> = null;
private setRootNode!: TSetRootNode;
constructor(props: TabProps<T>) {
super(props);
invariant(this.context !== TabsContextDefaultValue, 'Tab should be placed inside Tabs component');
}
public componentDidMount() {
const id = this.getId();
if (typeof id === 'string') {
this.context.addTab(id, this.getTabInstance);
}
}
public componentDidUpdate() {
if (this.context.activeTab === this.props.id) {
this.context.notifyUpdate();
}
}
public componentWillUnmount() {
const id = this.getId();
if (typeof id === 'string') {
this.context.removeTab(id);
}
}
public render() {
return (
<ThemeContext.Consumer>
{(theme) => {
this.theme = theme;
return this.renderMain();
}}
</ThemeContext.Consumer>
);
}
public getIndicators() {
return {
error: Boolean(this.props.error),
warning: Boolean(this.props.warning),
success: Boolean(this.props.success),
primary: Boolean(this.props.primary),
disabled: Boolean(this.props.disabled),
};
}
public getUnderlyingNode = () => this.tabComponent;
private renderMain() {
const {
children,
disabled,
error,
warning,
success,
primary,
component: Component = Tab.defaultProps.component,
href,
} = this.props;
let isActive = false;
let isVertical = false;
const id = this.getId();
if (typeof id === 'string') {
isActive = this.context.activeTab === this.getId();
isVertical = this.context.vertical;
}
const orientationStyles = isVertical ? verticalStyles : horizontalStyles;
return (
<CommonWrapper rootNodeRef={this.setRootNode} {...this.props}>
<Component
className={cx({
[styles.root(this.theme)]: true,
[styles.vertical(this.theme)]: !!isVertical,
[orientationStyles.primary(this.theme)]: !!primary,
[orientationStyles.success(this.theme)]: !!success,
[orientationStyles.warning(this.theme)]: !!warning,
[orientationStyles.error(this.theme)]: !!error,
[styles.active()]: !!isActive,
[orientationStyles.active(this.theme)]: !!isActive,
[styles.disabled(this.theme)]: !!disabled,
[orientationStyles.disabled()]: !!disabled,
})}
onBlur={this.handleBlur}
onClick={this.switchTab}
onFocus={this.handleFocus}
onKeyDown={this.handleKeyDown}
tabIndex={disabled ? -1 : 0}
ref={isFunctionalComponent(Component) ? null : this.refTabComponent}
href={href}
>
<ResizeDetector onResize={this.context.notifyUpdate}>{children}</ResizeDetector>
{this.state.focusedByKeyboard && <div className={cx(styles.focus(this.theme), globalClasses.focus)} />}
</Component>
</CommonWrapper>
);
}
private getId = () => this.props.id || this.props.href;
private refTabComponent = (instance: React.ReactElement<any>) => {
this.tabComponent = instance;
};
private getTabInstance = () => this;
private switchTab = (event: React.MouseEvent<HTMLElement>) => {
if (this.props.disabled) {
event.preventDefault();
return;
}
const id = this.props.id || this.props.href;
if (this.props.onClick) {
this.props.onClick(event);
if (event.defaultPrevented) {
return;
}
}
if (typeof id === 'string') {
this.context.switchTab(id);
}
if (this.props.component === 'a' && !this.props.href) {
event.preventDefault();
}
};
private handleKeyDown = (e: React.KeyboardEvent<HTMLElement>) => {
if (this.props.disabled) {
return;
}
if (this.props.onKeyDown) {
this.props.onKeyDown(e);
if (e.defaultPrevented) {
return;
}
}
const id = this.getId();
if (typeof id !== 'string') {
return;
}
if (!isKeyArrow(e)) {
return;
}
e.preventDefault();
const delta = isKeyArrowLeft(e) || isKeyArrowUp(e) ? -1 : 1;
this.context.shiftFocus(id, delta);
};
private handleFocus = () => {
if (this.props.disabled) {
return;
}
// focus event fires before keyDown eventlistener
// so we should check focusKeyPressed in async way
requestAnimationFrame(() => {
if (keyListener.isTabPressed || keyListener.isArrowPressed) {
this.setState({ focusedByKeyboard: true });
}
});
};
private handleBlur = () => {
if (this.props.disabled) {
return;
}
this.setState({ focusedByKeyboard: false });
};
}