Skip to content
This repository was archived by the owner on Jun 24, 2021. It is now read-only.

Commit 1b32ac7

Browse files
authored
Merge pull request #53 from nsantacruz/master
Allow use of other Text components to render text
2 parents ef1a646 + 84a9bb8 commit 1b32ac7

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

Demo/SelectableText.js

+45-26
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,17 @@ const mapHighlightsRanges = (value, highlights) => {
6666
* highlights: array({ id, start, end })
6767
* highlightColor: string
6868
* onHighlightPress: string => void
69+
* textValueProp: string
70+
* TextComponent: ReactNode
71+
* textComponentProps: object
6972
*/
70-
export const SelectableText = ({ onSelection, onHighlightPress, value, children, ...props }) => {
73+
export const SelectableText = ({
74+
onSelection, onHighlightPress, textValueProp, value, TextComponent,
75+
textComponentProps, ...props
76+
}) => {
77+
const usesTextComponent = !TextComponent;
78+
TextComponent = TextComponent || Text;
79+
textValueProp = textValueProp || 'children'; // default to `children` which will render `value` as a child of `TextComponent`
7180
const onSelectionNative = ({
7281
nativeEvent: { content, eventType, selectionStart, selectionEnd },
7382
}) => {
@@ -92,38 +101,48 @@ export const SelectableText = ({ onSelection, onHighlightPress, value, children,
92101
: onHighlightPress
93102
: () => {}
94103

104+
// highlights feature is only supported if `TextComponent == Text`
105+
let textValue = value;
106+
if (usesTextComponent) {
107+
textValue = (
108+
props.highlights && props.highlights.length > 0
109+
? mapHighlightsRanges(value, props.highlights).map(({ id, isHighlight, text }) => (
110+
<Text
111+
key={v4()}
112+
selectable
113+
style={
114+
isHighlight
115+
? {
116+
backgroundColor: props.highlightColor,
117+
}
118+
: {}
119+
}
120+
onPress={() => {
121+
if (isHighlight) {
122+
onHighlightPress && onHighlightPress(id)
123+
}
124+
}}
125+
>
126+
{text}
127+
</Text>
128+
))
129+
: [value]
130+
);
131+
if (props.appendToChildren) {
132+
textValue.push(props.appendToChildren);
133+
}
134+
}
95135
return (
96136
<RNSelectableText
97137
{...props}
98138
onHighlightPress={onHighlightPressNative}
99139
selectable
100140
onSelection={onSelectionNative}
101141
>
102-
<Text selectable key={v4()}>
103-
{props.highlights && props.highlights.length > 0
104-
? mapHighlightsRanges(value, props.highlights).map(({ id, isHighlight, text }) => (
105-
<Text
106-
key={v4()}
107-
selectable
108-
style={
109-
isHighlight
110-
? {
111-
backgroundColor: props.highlightColor,
112-
}
113-
: {}
114-
}
115-
onPress={() => {
116-
if (isHighlight) {
117-
onHighlightPress && onHighlightPress(id)
118-
}
119-
}}
120-
>
121-
{text}
122-
</Text>
123-
))
124-
: value}
125-
{props.appendToChildren ? props.appendToChildren : null}
126-
</Text>
142+
<TextComponent
143+
key={v4()}
144+
{...{[textValueProp]: textValue, ...textComponentProps}}
145+
/>
127146
</RNSelectableText>
128147
)
129148
}

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,7 @@ import { SelectableText } from "@astrocoders/react-native-selectable-text";
8181
| **highlightColor** | highlight color |string | null |
8282
| **onHighlightPress** | called when the user taps the highlight |(id: string) => void | () => {} |
8383
| **appendToChildren** | element to be added in the last line of text | ReactNode | null |
84+
| **TextComponent** | Text component used to render `value` | ReactNode | <Text> |
85+
| **textValueProp** | text value prop for TextComponent. Should be used when passing TextComponent. Defaults to 'children' which works for <Text> | string | 'children' |
86+
| **textComponentProps** | additional props to pass to TextComponent | object | null |
87+

0 commit comments

Comments
 (0)