tooot/src/components/Text.tsx

63 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-05-07 13:23:08 +02:00
import { useAccessibility } from '@utils/accessibility/AccessibilityManager'
import { StyleConstants } from '@utils/styles/constants'
2022-05-07 13:23:08 +02:00
import { Text, TextProps, TextStyle } from 'react-native'
type Props =
| {
style?: Omit<TextStyle, 'fontSize' | 'lineHeight' | 'fontWeight'>
fontStyle?: undefined
fontSize?: 'S' | 'M' | 'L'
lineHeight?: 'S' | 'M' | 'L'
fontWeight?: 'Normal' | 'Bold'
}
| {
style?: Omit<TextStyle, 'fontSize' | 'lineHeight' | 'fontWeight'>
fontStyle: 'S' | 'M' | 'L'
fontSize?: undefined
lineHeight?: undefined
fontWeight?: 'Normal' | 'Bold'
}
const CustomText: React.FC<Props & TextProps> = ({
children,
style,
fontStyle,
fontSize,
2022-05-10 23:19:26 +02:00
fontWeight,
lineHeight,
...rest
}) => {
2022-05-07 13:23:08 +02:00
const { boldTextEnabled } = useAccessibility()
enum BoldMapping {
'Normal' = '600',
'Bold' = '800'
}
return (
<Text
style={[
style,
{ ...(fontStyle && StyleConstants.FontStyle[fontStyle]) },
{ ...(fontSize && { fontSize: StyleConstants.Font.Size[fontSize] }) },
{
...(lineHeight && {
lineHeight: StyleConstants.Font.LineHeight[lineHeight]
})
},
{
2022-05-10 23:19:26 +02:00
fontWeight: fontWeight
? boldTextEnabled
? BoldMapping[fontWeight]
: StyleConstants.Font.Weight[fontWeight]
: undefined
}
]}
{...rest}
2022-05-07 13:23:08 +02:00
children={children}
/>
)
}
export default CustomText