forked from wednesday-solutions/react-graphql-ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
62 lines (52 loc) · 1.56 KB
/
index.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
/**
*
* T
*
*/
import React, { memo } from 'react';
import styled, { FlattenSimpleInterpolation } from 'styled-components';
import { FormattedMessage } from 'react-intl';
import PropTypes from 'prop-types';
import If from '@components/If';
import { fonts } from '@themes/index';
interface StyledTextProps {
marginBottom?: string | number;
font?: () => FlattenSimpleInterpolation;
}
const StyledText = styled.p<StyledTextProps>`
&& {
${(props) =>
props.marginBottom &&
`margin-bottom: ${typeof props.marginBottom === 'string' ? props.marginBottom : `${props.marginBottom}rem`};`}
${(props) => props.font && props.font()};
}
`;
type FontStyleType = keyof typeof fonts.style;
const getFontStyle = (type: FontStyleType) => fonts.style[type];
interface TProps {
type?: FontStyleType;
text?: string;
id?: string;
marginBottom?: string | number;
values?: Record<string, React.ReactNode>;
}
export const T = ({ type = 'standard', text, id, marginBottom, values = {}, ...otherProps }: TProps) => (
<StyledText data-testid="t" font={getFontStyle(type)} marginBottom={marginBottom} {...otherProps}>
<If condition={id} otherwise={text}>
<FormattedMessage id={id} values={values} />
</If>
</StyledText>
);
T.propTypes = {
id: PropTypes.string,
marginBottom: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
values: PropTypes.object,
text: PropTypes.string,
type: PropTypes.oneOf(Object.keys(fonts.style))
};
T.defaultProps = {
values: {},
type: 'standard'
};
const TextComponent = memo(T);
export default TextComponent;