Skip to content

Commit a902518

Browse files
committed
feat(RNText):增加文本截取提示功能tooltipProps
1 parent 49b25ea commit a902518

File tree

4 files changed

+114
-80
lines changed

4 files changed

+114
-80
lines changed

example/examples/src/routes/Typography/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ export default class TypographyView extends React.Component<TypographyViewProps>
7777
<Card title="可高亮文字<RnText />">
7878
<RnText type="header" label="react-native-uiw" />
7979
<RnText type="title" label="react-native-uiw" />
80-
<RnText type="label" label="react-native-uiw" />
80+
<RnText
81+
type="label"
82+
label="文字过长"
83+
tooltipProps={{number: 4, content: 'react-native-uiw'}}
84+
/>
8185
<RnText type="subLabel" label="react-native-uiw" />
8286
<RnText
8387
type="header"

packages/core/src/Typography/RnText.tsx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React from 'react';
22
import { Text as RNText, StyleSheet, TextProps as RNTextProps, TextStyle, Animated, StyleProp } from 'react-native';
33
import _ from 'lodash';
4-
import { rnTextType, getTextPartsByHighlight } from '../utils/utils';
4+
import Tooltip from '../Tooltip';
5+
import { rnTextType, getTextPartsByHighlight, sliceText } from '../utils/rn-text';
56

67
export type RnTextProps = RNTextProps & {
78
// header:Header标题 | title:列表/From标题 | label:正文,说明,备注label | subLabel:辅助性文字
@@ -21,10 +22,20 @@ export type RnTextProps = RNTextProps & {
2122
highlightTextStyle?: TextStyle;
2223
// 背景色
2324
backgroundColor?: any;
25+
tooltipProps?: {
26+
// 截取文本长度
27+
number?: number;
28+
// 提示的文本
29+
content?: string;
30+
};
2431
children?: React.ReactNode;
2532
style?: StyleProp<TextStyle | Animated.AnimatedProps<TextStyle>>;
2633
};
2734

35+
const TooltipContainer = ({ content, children }: { content?: string; children?: any }) => {
36+
return content ? <Tooltip title={content}>{children}</Tooltip> : <React.Fragment>{children}</React.Fragment>;
37+
};
38+
2839
export default (props: RnTextProps) => {
2940
const {
3041
type,
@@ -38,9 +49,15 @@ export default (props: RnTextProps) => {
3849
label,
3950
highlightText,
4051
highlightTextStyle,
52+
tooltipProps = {
53+
number: 0,
54+
content: '',
55+
},
4156
...others
4257
} = props;
4358

59+
const { number, content } = tooltipProps;
60+
4461
const TextContainer: React.ClassType<any, any, any> = animated ? Animated.createAnimatedComponent(RNText) : RNText;
4562

4663
/**
@@ -82,9 +99,11 @@ export default (props: RnTextProps) => {
8299
];
83100

84101
return (
85-
<TextContainer {...others} style={textStyle}>
86-
{renderText(label || children)}
87-
</TextContainer>
102+
<TooltipContainer content={content}>
103+
<TextContainer {...others} style={textStyle}>
104+
{number ? sliceText(renderText(label || children), number) : renderText(label || children)}
105+
</TextContainer>
106+
</TooltipContainer>
88107
);
89108
};
90109

packages/core/src/utils/rn-text.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { TextStyle } from 'react-native';
2+
import _ from 'lodash';
3+
import { colorsPalette } from './colors';
4+
/**
5+
*
6+
* RnText 类型判断
7+
* @param type "header" | "title" | 'label' | 'subLabel'
8+
* @returns
9+
*/
10+
export function rnTextType(type?: 'header' | 'title' | 'label' | 'subLabel') {
11+
let textStyle: TextStyle = {
12+
color: colorsPalette.grey10,
13+
fontSize: 16,
14+
};
15+
switch (type) {
16+
case 'header':
17+
textStyle = {
18+
color: colorsPalette.grey10,
19+
fontSize: 17,
20+
fontWeight: '500',
21+
};
22+
break;
23+
case 'title':
24+
textStyle = {
25+
color: colorsPalette.grey10,
26+
fontSize: 15,
27+
fontWeight: '400',
28+
};
29+
break;
30+
case 'label':
31+
textStyle = {
32+
color: colorsPalette.grey10,
33+
fontSize: 14,
34+
fontWeight: '300',
35+
};
36+
break;
37+
case 'subLabel':
38+
textStyle = {
39+
color: colorsPalette.grey30,
40+
fontSize: 12,
41+
fontWeight: '300',
42+
};
43+
break;
44+
default:
45+
}
46+
return textStyle;
47+
}
48+
49+
/**
50+
* 处理高亮文本
51+
* @targetString 整体文本
52+
* @highlightText 高亮的文本
53+
* textParts:[]
54+
* */
55+
export const getTextPartsByHighlight = (targetString = '', highlightText = '') => {
56+
if (_.isEmpty(highlightText.trim())) {
57+
return [targetString];
58+
}
59+
60+
const textParts = [];
61+
let highlightIndex;
62+
63+
do {
64+
highlightIndex = targetString.toLowerCase().indexOf(highlightText.toLowerCase());
65+
if (highlightIndex !== -1) {
66+
if (highlightIndex > 0) {
67+
textParts.push(targetString.substring(0, highlightIndex));
68+
}
69+
textParts.push(targetString.substr(highlightIndex, highlightText.length));
70+
targetString = targetString.substr(highlightIndex + highlightText.length);
71+
} else {
72+
textParts.push(targetString);
73+
}
74+
} while (highlightIndex !== -1);
75+
return textParts;
76+
};
77+
78+
/**
79+
* 截取文本 + '...'
80+
* @param text 文本
81+
* @param length 截取的长度
82+
* @returns
83+
*/
84+
export function sliceText(text = '', length: number) {
85+
return text.slice(0, length) + '...';
86+
}

packages/core/src/utils/utils.ts

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import { TextStyle } from 'react-native';
21
import _ from 'lodash';
3-
import { colorsPalette } from './colors';
42

53
export function arrayTreeFilter<T>(
64
data: T[],
@@ -27,76 +25,3 @@ export function arrayTreeFilter<T>(
2725
} while (children.length > 0);
2826
return result;
2927
}
30-
/**
31-
*
32-
* RnText 类型判断
33-
* @param type "header" | "title" | 'label' | 'subLabel'
34-
* @returns
35-
*/
36-
export function rnTextType(type?: 'header' | 'title' | 'label' | 'subLabel') {
37-
let textStyle: TextStyle = {
38-
color: colorsPalette.grey10,
39-
fontSize: 16,
40-
};
41-
switch (type) {
42-
case 'header':
43-
textStyle = {
44-
color: colorsPalette.grey10,
45-
fontSize: 17,
46-
fontWeight: '500',
47-
};
48-
break;
49-
case 'title':
50-
textStyle = {
51-
color: colorsPalette.grey10,
52-
fontSize: 15,
53-
fontWeight: '400',
54-
};
55-
break;
56-
case 'label':
57-
textStyle = {
58-
color: colorsPalette.grey10,
59-
fontSize: 14,
60-
fontWeight: '300',
61-
};
62-
break;
63-
case 'subLabel':
64-
textStyle = {
65-
color: colorsPalette.grey30,
66-
fontSize: 12,
67-
fontWeight: '300',
68-
};
69-
break;
70-
default:
71-
}
72-
return textStyle;
73-
}
74-
75-
/**
76-
* 处理高亮文本
77-
* @targetString 整体文本
78-
* @highlightText 高亮的文本
79-
* textParts:[]
80-
* */
81-
export const getTextPartsByHighlight = (targetString = '', highlightText = '') => {
82-
if (_.isEmpty(highlightText.trim())) {
83-
return [targetString];
84-
}
85-
86-
const textParts = [];
87-
let highlightIndex;
88-
89-
do {
90-
highlightIndex = targetString.toLowerCase().indexOf(highlightText.toLowerCase());
91-
if (highlightIndex !== -1) {
92-
if (highlightIndex > 0) {
93-
textParts.push(targetString.substring(0, highlightIndex));
94-
}
95-
textParts.push(targetString.substr(highlightIndex, highlightText.length));
96-
targetString = targetString.substr(highlightIndex + highlightText.length);
97-
} else {
98-
textParts.push(targetString);
99-
}
100-
} while (highlightIndex !== -1);
101-
return textParts;
102-
};

0 commit comments

Comments
 (0)