-
Notifications
You must be signed in to change notification settings - Fork 126
/
Spinner.tsx
129 lines (111 loc) · 3.45 KB
/
Spinner.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
import React from 'react';
import PropTypes from 'prop-types';
import { ReactNodePropTypes } from '../../lib/utils';
import { locale } from '../../lib/locale/decorators';
import { Theme } from '../../lib/theming/Theme';
import { ThemeContext } from '../../lib/theming/ThemeContext';
import { SpinnerIcon } from '../../internal/icons/SpinnerIcon';
import { CommonProps, CommonWrapper } from '../../internal/CommonWrapper';
import { cx } from '../../lib/theming/Emotion';
import { rootNode, TSetRootNode } from '../../lib/rootNode';
import { styles } from './Spinner.styles';
import { SpinnerLocale, SpinnerLocaleHelper } from './locale';
const types: Record<SpinnerType, SpinnerType> = {
big: 'big',
mini: 'mini',
normal: 'normal',
};
export type SpinnerType = 'mini' | 'normal' | 'big';
export interface SpinnerProps extends CommonProps {
caption?: React.ReactNode;
dimmed?: boolean;
/**
* Тип спиннера
* @default normal
*/
type: SpinnerType;
inline?: boolean;
/**
* Толщина спиннера
*/
width?: number;
/**
* Цвет спиннера
*/
color?: React.CSSProperties['color'];
}
export const SpinnerDataTids = {
root: 'Spinner__root',
} as const;
/**
* DRAFT - инлайн-лоадер
*/
@rootNode
@locale('Spinner', SpinnerLocaleHelper)
export class Spinner extends React.Component<SpinnerProps> {
public static __KONTUR_REACT_UI__ = 'Spinner';
public static propTypes = {
/**
* Текст рядом с мини-лоадером.
*
* 'Загрузка' - значение по-умолчанию
*/
caption: ReactNodePropTypes,
dimmed: PropTypes.bool,
/**
* Тип спиннера: mini, normal, big
*
* Значение по-умолчанию - normal
*
* Spinner.types - все доступные типы
*/
type: PropTypes.oneOf(Object.keys(types)),
};
public static defaultProps: SpinnerProps = {
type: 'normal',
};
public static Types: typeof types = types;
private theme!: Theme;
private readonly locale!: SpinnerLocale;
private setRootNode!: TSetRootNode;
public render() {
return (
<ThemeContext.Consumer>
{(theme) => {
this.theme = theme;
return this.renderMain();
}}
</ThemeContext.Consumer>
);
}
private renderMain() {
const { type, caption = this.locale.loading, dimmed, inline } = this.props;
return (
<CommonWrapper rootNodeRef={this.setRootNode} {...this.props}>
<div data-tid={SpinnerDataTids.root} className={styles.spinner()}>
<span className={styles.inner()}>{this.renderSpinner(type, dimmed, inline)}</span>
{caption && this.renderCaption(type, caption)}
</div>
</CommonWrapper>
);
}
private renderSpinner = (type: SpinnerType, dimmed?: boolean, inline?: boolean) => {
return (
<SpinnerIcon
size={type}
className={cx({
[styles.circle(this.theme)]: !dimmed && !this.props.color,
[styles.circleDimmedColor(this.theme)]: dimmed,
[styles.circleWithoutColorAnimation(this.theme)]: dimmed || !!this.props.color,
})}
dimmed={dimmed}
width={this.props.width}
color={this.props.color}
inline={inline}
/>
);
};
private renderCaption = (type: SpinnerType, caption: React.ReactNode) => (
<span className={cx(styles[type](this.theme), styles.captionColor(this.theme))}>{caption}</span>
);
}