|
| 1 | +import React from 'react'; |
| 2 | +import { Text as RNText, StyleSheet, TextProps as RNTextProps, TextStyle, Animated, StyleProp } from 'react-native'; |
| 3 | +import _ from 'lodash'; |
| 4 | +import { rnTextType, getTextPartsByHighlight } from '../utils/utils'; |
| 5 | + |
| 6 | +export type RnTextProps = RNTextProps & { |
| 7 | + // header:Header标题 | title:列表/From标题 | label:正文,说明,备注label | subLabel:辅助性文字 |
| 8 | + type?: 'header' | 'title' | 'label' | 'subLabel'; |
| 9 | + // 文本正文 |
| 10 | + label?: string; |
| 11 | + // 文本颜色 |
| 12 | + color?: string; |
| 13 | + // 文本居中 |
| 14 | + center?: boolean; |
| 15 | + // 将文本转换为全大写 |
| 16 | + uppercase?: boolean; |
| 17 | + animated?: boolean; |
| 18 | + // 高亮的文本 |
| 19 | + highlightText?: string; |
| 20 | + // 高亮文本样式 |
| 21 | + highlightTextStyle?: TextStyle; |
| 22 | + // 背景色 |
| 23 | + backgroundColor?: any; |
| 24 | + children?: React.ReactNode; |
| 25 | + style?: StyleProp<TextStyle | Animated.AnimatedProps<TextStyle>>; |
| 26 | +}; |
| 27 | + |
| 28 | +export default (props: RnTextProps) => { |
| 29 | + const { |
| 30 | + type, |
| 31 | + color, |
| 32 | + center, |
| 33 | + uppercase, |
| 34 | + animated, |
| 35 | + backgroundColor, |
| 36 | + style, |
| 37 | + children, |
| 38 | + label, |
| 39 | + highlightText, |
| 40 | + highlightTextStyle, |
| 41 | + ...others |
| 42 | + } = props; |
| 43 | + |
| 44 | + const TextContainer: React.ClassType<any, any, any> = animated ? Animated.createAnimatedComponent(RNText) : RNText; |
| 45 | + |
| 46 | + /** |
| 47 | + * 文本渲染 |
| 48 | + * @param children |
| 49 | + * @returns |
| 50 | + */ |
| 51 | + const renderText = (children: any): any => { |
| 52 | + // 递归渲染子级的Text |
| 53 | + if (!_.isEmpty(highlightText)) { |
| 54 | + if (_.isArray(children)) { |
| 55 | + return children.map((child) => renderText(child)); |
| 56 | + } |
| 57 | + if (_.isString(children)) { |
| 58 | + const textParts = getTextPartsByHighlight(children, highlightText); |
| 59 | + return textParts.map((text, index) => { |
| 60 | + const shouldHighlight = _.lowerCase(text) === _.lowerCase(highlightText); |
| 61 | + return ( |
| 62 | + <RNText key={index} style={shouldHighlight ? [styles.highlight, highlightTextStyle] : styles.notHighlight}> |
| 63 | + {text} |
| 64 | + </RNText> |
| 65 | + ); |
| 66 | + }); |
| 67 | + } |
| 68 | + } |
| 69 | + return children; |
| 70 | + }; |
| 71 | + |
| 72 | + const textStyle = [ |
| 73 | + { |
| 74 | + ...rnTextType(type), |
| 75 | + }, |
| 76 | + styles.container, |
| 77 | + color && { color }, |
| 78 | + backgroundColor && { backgroundColor }, |
| 79 | + center && styles.centered, |
| 80 | + uppercase && styles.uppercase, |
| 81 | + style, |
| 82 | + ]; |
| 83 | + |
| 84 | + return ( |
| 85 | + <TextContainer {...others} style={textStyle}> |
| 86 | + {renderText(label || children)} |
| 87 | + </TextContainer> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +const styles = StyleSheet.create({ |
| 92 | + container: { |
| 93 | + backgroundColor: 'transparent', |
| 94 | + textAlign: 'left', |
| 95 | + }, |
| 96 | + centered: { |
| 97 | + textAlign: 'center', |
| 98 | + }, |
| 99 | + uppercase: { |
| 100 | + textTransform: 'uppercase', |
| 101 | + }, |
| 102 | + highlight: { |
| 103 | + color: '#333333', |
| 104 | + fontSize: 14, |
| 105 | + }, |
| 106 | + notHighlight: { |
| 107 | + color: undefined, |
| 108 | + }, |
| 109 | +}); |
0 commit comments