-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.tsx
More file actions
83 lines (76 loc) · 1.69 KB
/
Text.tsx
File metadata and controls
83 lines (76 loc) · 1.69 KB
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
import React, { useContext } from 'react';
import { Text as RNText } from 'react-native';
import { ThemeContext, Theme } from '../theme';
const HEADING = 'heading';
const SUB_HEADING = 'subheading';
const BODY = 'body';
const LABEL = 'label';
const CAPTION = 'caption';
type TextType = 'heading' | 'subheading' | 'body' | 'label' | 'caption';
type TextProps = {
/**
* @type prop helps style Text with pre-defined styling defined in
* typography.js. Possible value of type can be:
* 1. 'heading'
* 2. 'subheading'
* 3. 'body'
* 4. 'label'
* 5. 'caption'
*
* default value: 'body'
*/
type?: TextType,
/**
* @bold if enabled will use bold version of the
* type mentioned.
*/
bold?: boolean,
/**
* @style prop will overwrite the predefined styling for Text defined by
* @type prop
*/
style?: any,
};
const Text: React.SFC<TextProps> = ({
type = 'body',
bold = false,
style = {},
...props
}) => {
const theme = useContext(ThemeContext);
return (
<RNText
style={[styles.text(type, bold, theme), style]}
{...props}
/>
);
};
const getTextStyle = (type: TextType, bold: boolean, theme: Theme) => {
let style = '';
switch (type) {
case HEADING:
style = 'headingText';
break;
case SUB_HEADING:
style = 'subheadingText';
break;
case LABEL:
style = 'labelText';
break;
case CAPTION:
style = 'captionText';
break;
default:
style = 'bodyText';
}
if (bold) {
style += 'Bold';
}
return theme.typography[style];
};
const styles = {
text: (type: TextType, bold: boolean, theme: Theme) => ({
...getTextStyle(type, bold, theme),
}),
};
export default Text;