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

Commit 70eda2c

Browse files
committed
polish
1 parent 1b340c8 commit 70eda2c

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

Demo/SelectableText.js

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,15 @@ 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
*/
7073
export const SelectableText = ({
7174
onSelection, onHighlightPress, textValueProp, value, TextComponent,
7275
textComponentProps, ...props
7376
}) => {
77+
const usesTextComponent = !TextComponent;
7478
TextComponent = TextComponent || Text;
7579
textValueProp = textValueProp || 'children'; // default to `children` which will render `value` as a child of `TextComponent`
7680
const onSelectionNative = ({
@@ -97,33 +101,36 @@ export const SelectableText = ({
97101
: onHighlightPress
98102
: () => {}
99103

100-
// TODO: convert Text to TextComponent. User React Children API
101-
const textValue = (
102-
props.highlights && props.highlights.length > 0
103-
? mapHighlightsRanges(value, props.highlights).map(({ id, isHighlight, text }) => (
104-
<Text
105-
key={v4()}
106-
selectable
107-
style={
108-
isHighlight
109-
? {
110-
backgroundColor: props.highlightColor,
111-
}
112-
: {}
113-
}
114-
onPress={() => {
115-
if (isHighlight) {
116-
onHighlightPress && onHighlightPress(id)
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+
: {}
117119
}
118-
}}
119-
>
120-
{text}
121-
</Text>
122-
))
123-
: [value]
124-
);
125-
if (props.appendToChildren) {
126-
textValue.push(props.appendToChildren);
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+
}
127134
}
128135
return (
129136
<RNSelectableText
@@ -134,8 +141,7 @@ export const SelectableText = ({
134141
>
135142
<TextComponent
136143
key={v4()}
137-
textValueProp={textValue}
138-
{...textComponentProps}
144+
{...{[textValueProp]: textValue, ...textComponentProps}}
139145
/>
140146
</RNSelectableText>
141147
)

0 commit comments

Comments
 (0)