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

Commit 1b340c8

Browse files
committed
basic refactor to customize text component
1 parent ef1a646 commit 1b340c8

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

Demo/SelectableText.js

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,12 @@ const mapHighlightsRanges = (value, highlights) => {
6767
* highlightColor: string
6868
* onHighlightPress: string => void
6969
*/
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`
7176
const onSelectionNative = ({
7277
nativeEvent: { content, eventType, selectionStart, selectionEnd },
7378
}) => {
@@ -92,38 +97,46 @@ export const SelectableText = ({ onSelection, onHighlightPress, value, children,
9297
: onHighlightPress
9398
: () => {}
9499

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+
}
95128
return (
96129
<RNSelectableText
97130
{...props}
98131
onHighlightPress={onHighlightPressNative}
99132
selectable
100133
onSelection={onSelectionNative}
101134
>
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+
/>
127140
</RNSelectableText>
128141
)
129142
}

0 commit comments

Comments
 (0)