-
Notifications
You must be signed in to change notification settings - Fork 126
/
Button.tsx
337 lines (270 loc) · 8.81 KB
/
Button.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import * as React from 'react';
import * as events from 'add-event-listener';
import classNames from 'classnames';
import Upgrades from '../../lib/Upgrades';
import Corners from './Corners';
import '../ensureOldIEClassName';
const isFlatDesign = Upgrades.isFlatDesignEnabled();
import CssStyles from './Button.less';
const classes: typeof CssStyles = isFlatDesign ? require('./Button.flat.less') : require('./Button.less');
const KEYCODE_TAB = 9;
let isListening: boolean;
let tabPressed: boolean;
function listenTabPresses() {
if (!isListening) {
events.addEventListener(window, 'keydown', event => {
tabPressed = event.keyCode === KEYCODE_TAB;
});
isListening = true;
}
}
export const isButton = (child: React.ReactChild): child is React.ReactElement<ButtonProps> => {
return React.isValidElement<ButtonProps>(child) ? child.type.hasOwnProperty('__BUTTON__') : false;
};
export type ButtonSize = 'small' | 'medium' | 'large';
export type ButtonType = 'button' | 'submit' | 'reset';
export type ButtonArrow = boolean | 'left';
export type ButtonUse = 'default' | 'primary' | 'success' | 'danger' | 'pay' | 'link';
export interface ButtonProps {
/** @ignore */
_noPadding?: boolean;
/** @ignore */
_noRightPadding?: boolean;
/**
* Визуально нажатое состояние.
*/
active?: boolean;
// tslint:disable-next-line:max-line-length
/** `type TextAlignProperty = "inherit" | "initial" | "unset" | "center" | "end" | "justify" | "left" | "match-parent" | "right" | "start"` */
align?: React.CSSProperties['textAlign'];
/**
* Кнопка со стрелкой.
*
* `type ButtonArrow = boolean | "left"`
*/
arrow?: ButtonArrow;
autoFocus?: boolean;
borderless?: boolean;
checked?: boolean;
children?: React.ReactNode;
/** @ignore */
corners?: number;
disabled?: boolean;
/** @ignore */
disableFocus?: boolean;
error?: boolean;
focused?: boolean;
/**
* Иконка слева от текста кнопки.
*/
icon?: React.ReactElement<any>;
loading?: boolean;
narrow?: boolean;
onBlur?: React.FocusEventHandler<HTMLButtonElement>;
onClick?: React.MouseEventHandler<HTMLButtonElement>;
onFocus?: React.FocusEventHandler<HTMLButtonElement>;
onKeyDown?: React.KeyboardEventHandler<HTMLButtonElement>;
onMouseEnter?: React.MouseEventHandler<HTMLButtonElement>;
onMouseLeave?: React.MouseEventHandler<HTMLButtonElement>;
onMouseOver?: React.MouseEventHandler<HTMLButtonElement>;
/** `type ButtonSize = "small" | "medium" | "large"` */
size?: ButtonSize;
/** `type ButtonType = "button" | "submit" | "reset"` */
type?: ButtonType;
/**
* Вариант использования. Влияет на цвет кнопки.
*
* `type ButtonUse = "default" | "primary" | "success" | "danger" | "pay" | "link"`
*/
use?: ButtonUse;
/** @ignore */
visuallyFocused?: boolean;
warning?: boolean;
width?: number | string;
}
export interface ButtonState {
focusedByTab: boolean;
}
class Button extends React.Component<ButtonProps, ButtonState> {
public static __BUTTON__ = true;
public static TOP_LEFT = Corners.TOP_LEFT;
public static TOP_RIGHT = Corners.TOP_RIGHT;
public static BOTTOM_RIGHT = Corners.BOTTOM_RIGHT;
public static BOTTOM_LEFT = Corners.BOTTOM_LEFT;
public static defaultProps = {
use: 'default',
size: 'small',
type: 'button',
};
public state = {
focusedByTab: false,
};
private _node: HTMLButtonElement | null = null;
public componentDidMount() {
listenTabPresses();
if (this.props.autoFocus) {
tabPressed = true;
this.focus();
}
}
/**
* @public
*/
public focus() {
if (this._node) {
this._node.focus();
}
}
/**
* @public
*/
public blur() {
if (this._node) {
this._node.blur();
}
}
public render(): JSX.Element {
const { corners = 0 } = this.props;
const RADIUS = this.props.size === 'small' ? '1px' : '2px';
const SIZE_CLASSES = {
small: classes.sizeSmall,
medium: Upgrades.isSizeMedium16pxEnabled() ? classes.sizeMedium : classes.DEPRECATED_sizeMedium,
large: classes.sizeLarge,
};
const rootProps = {
// By default the type attribute is 'submit'. IE8 will fire a click event
// on this button if somewhere on the page user presses Enter while some
// input is focused. So we set type to 'button' by default.
type: this.props.type,
className: classNames({
[classes.root]: true,
[(classes as { [name: string]: string })[this.props.use!] || classes.default]: true,
[classes.active]: this.props.active,
[classes.checked]: this.props.checked,
[classes.disabled]: this.props.disabled || this.props.loading,
[classes.narrow]: this.props.narrow,
[classes.noPadding]: this.props._noPadding,
[classes.noRightPadding]: this.props._noRightPadding,
[classes.buttonWithIcon]: !!this.props.icon,
[SIZE_CLASSES[this.props.size!]]: true,
[classes.focus]: this.state.focusedByTab || this.props.visuallyFocused,
[classes.borderless]: this.props.borderless,
}),
style: {
borderRadius:
`${corners & Corners.TOP_LEFT ? 0 : RADIUS}` +
` ${corners & Corners.TOP_RIGHT ? 0 : RADIUS}` +
` ${corners & Corners.BOTTOM_RIGHT ? 0 : RADIUS}` +
` ${corners & Corners.BOTTOM_LEFT ? 0 : RADIUS}`,
textAlign: this.props.align,
},
disabled: this.props.disabled || this.props.loading,
onClick: this.props.onClick,
onFocus: this.handleFocus,
onBlur: this.handleBlur,
onKeyDown: this.props.onKeyDown,
onMouseEnter: this.props.onMouseEnter,
onMouseLeave: this.props.onMouseLeave,
onMouseOver: this.props.onMouseOver,
tabIndex: this.props.disableFocus ? -1 : 0,
};
const wrapProps = {
className: classNames({
[classes.wrap]: true,
[classes.wrap_arrow]: this.props.arrow,
[classes.wrap_arrow_left]: this.props.arrow === 'left',
}),
style: {
width: this.props.width,
},
};
let error = null;
if (this.props.error) {
error = <div className={classes.error} />;
} else if (this.props.warning) {
error = <div className={classes.warning} />;
}
let loading = null;
if (this.props.loading) {
loading = <div className={classes.loading} />;
}
let icon = this.props.icon;
if (this.props.icon) {
icon = <span className={classes.icon}>{this.props.icon}</span>;
}
let arrow = null;
if (this.props.arrow) {
arrow = (
<div
className={classNames({
[classes.arrow || '']: true,
[classes.arrow_left || '']: this.props.arrow === 'left',
[classes.arrow_loading || '']: this.props.loading,
[classes.arrow_error || '']: this.props.error,
[classes.arrow_warning || '']: this.props.warning,
})}
/>
);
}
// Force disable all props and features, that cannot be use with Link
if (this.props.use === 'link') {
rootProps.className = classNames({
[classes.root]: true,
[classes.link]: true,
[classes.disabled]: this.props.disabled,
[classes.buttonWithIcon]: !!this.props.icon,
[SIZE_CLASSES[this.props.size!]]: true,
[classes.focus]: this.state.focusedByTab || this.props.visuallyFocused,
});
Object.assign(wrapProps, {
className: classNames(classes.wrap, {
[classes.wrap_link]: this.props.use === 'link',
}),
style: { width: wrapProps.style.width },
});
rootProps.style.textAlign = undefined;
loading = null;
arrow = null;
}
return (
<span {...wrapProps}>
<button ref={this._ref} {...rootProps}>
{error}
{loading}
{arrow}
<div className={classes.caption}>
{icon}
{this.props.children}
</div>
</button>
</span>
);
}
private handleFocus = (e: React.FocusEvent<HTMLButtonElement>) => {
if (!this.props.disabled && !this.props.disableFocus) {
// focus event fires before keyDown eventlistener
// so we should check tabPressed in async way
process.nextTick(() => {
if (tabPressed) {
this.setState({ focusedByTab: true });
tabPressed = false;
}
});
if (this.props.onFocus) {
this.props.onFocus(e);
}
}
};
private handleBlur = (e: React.FocusEvent<HTMLButtonElement>) => {
this.setState({ focusedByTab: false });
if (!this.props.disabled && !this.props.disableFocus) {
if (this.props.onBlur) {
this.props.onBlur(e);
}
}
};
private _ref = (node: HTMLButtonElement | null) => {
this._node = node;
};
}
export default Button;