@@ -67,7 +67,12 @@ const mapHighlightsRanges = (value, highlights) => {
67
67
* highlightColor: string
68
68
* onHighlightPress: string => void
69
69
*/
70
- export const SelectableText = ( { onSelection, onHighlightPress, value, children, ...props } ) => {
70
+ export const SelectableText = ( {
71
+ onSelection, onHighlightPress, textValueProp, value, TextComponent,
72
+ textComponentProps, ...props
73
+ } ) => {
74
+ TextComponent = TextComponent || Text ;
75
+ textValueProp = textValueProp || 'children' ; // default to `children` which will render `value` as a child of `TextComponent`
71
76
const onSelectionNative = ( {
72
77
nativeEvent : { content, eventType, selectionStart, selectionEnd } ,
73
78
} ) => {
@@ -92,38 +97,46 @@ export const SelectableText = ({ onSelection, onHighlightPress, value, children,
92
97
: onHighlightPress
93
98
: ( ) => { }
94
99
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 )
117
+ }
118
+ } }
119
+ >
120
+ { text }
121
+ </ Text >
122
+ ) )
123
+ : [ value ]
124
+ ) ;
125
+ if ( props . appendToChildren ) {
126
+ textValue . push ( props . appendToChildren ) ;
127
+ }
95
128
return (
96
129
< RNSelectableText
97
130
{ ...props }
98
131
onHighlightPress = { onHighlightPressNative }
99
132
selectable
100
133
onSelection = { onSelectionNative }
101
134
>
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 >
135
+ < TextComponent
136
+ key = { v4 ( ) }
137
+ textValueProp = { textValue }
138
+ { ...textComponentProps }
139
+ />
127
140
</ RNSelectableText >
128
141
)
129
142
}
0 commit comments