generated from wednesday-solutions/react-template
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.tsx
65 lines (55 loc) · 1.59 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
63
64
65
/**
*
* T
*
*/
import React, { memo } from 'react';
import styled, { FlattenSimpleInterpolation } from 'styled-components';
import { Trans } from '@lingui/react';
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 = (props: TProps) => {
const { type = 'standard', text, id, marginBottom, values = {}, ...otherProps } = props;
return (
<StyledText data-testid="t" font={getFontStyle(type)} marginBottom={marginBottom} {...otherProps}>
<If condition={!!id} otherwise={text}>
<Trans 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;