-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Text.js
76 lines (64 loc) · 1.85 KB
/
Text.js
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
import React from 'react';
import PropTypes from 'prop-types';
import _ from 'underscore';
import {Text as RNText} from 'react-native';
import fontFamily from '../styles/fontFamily';
import themeColors from '../styles/themes/default';
import variables from '../styles/variables';
const propTypes = {
/** The color of the text */
color: PropTypes.string,
/** The size of the text */
fontSize: PropTypes.number,
/** The alignment of the text */
// eslint-disable-next-line react/forbid-prop-types
textAlign: PropTypes.any,
/** Any children to display */
children: PropTypes.node,
/** The family of the font to use */
family: PropTypes.string,
/** Any additional styles to apply */
// eslint-disable-next-line react/forbid-prop-types
style: PropTypes.any,
};
const defaultProps = {
color: themeColors.text,
fontSize: variables.fontSizeNormal,
family: 'GTA',
textAlign: null,
children: null,
style: {},
};
const Text = React.forwardRef(({
color,
fontSize,
textAlign,
children,
family,
style,
...props
}, ref) => {
// If the style prop is an array of styles, we need to mix them all together
const mergedStyles = !_.isArray(style) ? style : _.reduce(style, (finalStyles, s) => ({
...finalStyles,
...s,
}), {});
const componentStyle = {
color,
fontSize,
textAlign,
fontFamily: fontFamily[family],
...mergedStyles,
};
if (componentStyle.fontSize === variables.fontSizeNormal) {
componentStyle.lineHeight = 20;
}
return (
// eslint-disable-next-line react/jsx-props-no-spreading
<RNText ref={ref} style={[componentStyle]} {...props}>{children}</RNText>
);
});
Text.propTypes = propTypes;
Text.defaultProps = defaultProps;
Text.displayName = 'Text';
export default Text;