@@ -66,11 +66,15 @@ const mapHighlightsRanges = (value, highlights) => {
66
66
* highlights: array({ id, start, end })
67
67
* highlightColor: string
68
68
* onHighlightPress: string => void
69
+ * textValueProp: string
70
+ * TextComponent: ReactNode
71
+ * textComponentProps: object
69
72
*/
70
73
export const SelectableText = ( {
71
74
onSelection, onHighlightPress, textValueProp, value, TextComponent,
72
75
textComponentProps, ...props
73
76
} ) => {
77
+ const usesTextComponent = ! TextComponent ;
74
78
TextComponent = TextComponent || Text ;
75
79
textValueProp = textValueProp || 'children' ; // default to `children` which will render `value` as a child of `TextComponent`
76
80
const onSelectionNative = ( {
@@ -97,33 +101,36 @@ export const SelectableText = ({
97
101
: onHighlightPress
98
102
: ( ) => { }
99
103
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
+ : { }
117
119
}
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
+ }
127
134
}
128
135
return (
129
136
< RNSelectableText
@@ -134,8 +141,7 @@ export const SelectableText = ({
134
141
>
135
142
< TextComponent
136
143
key = { v4 ( ) }
137
- textValueProp = { textValue }
138
- { ...textComponentProps }
144
+ { ...{ [ textValueProp ] : textValue , ...textComponentProps } }
139
145
/>
140
146
</ RNSelectableText >
141
147
)
0 commit comments