Skip to content

Commit

Permalink
Adds RNTester example with dynamic backgroundColor props
Browse files Browse the repository at this point in the history
  • Loading branch information
rozele committed Aug 11, 2021
1 parent b39cc25 commit 6067713
Showing 1 changed file with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import React from 'react';
import {
/*Image,*/ StyleSheet,
Text,
TextInput,
View,
TextStyle,
TouchableWithoutFeedback,
Expand Down Expand Up @@ -167,6 +168,51 @@ export class BackgroundColorDemo extends React.Component<{}> {
}
}

export class TextHighlightDemo extends React.Component<
{},
{search: string}
> {
constructor(props: any) {
super(props);
this.state = {search: ''};
}

private getTextParts(text: string) {
if (this.state.search === '') {
return [text];
}

let parts = text.split(this.state.search);
for (var i = parts.length - 2; i >= 0; --i) {
parts = [...parts.slice(0, i + 1), this.state.search, ...parts.slice(i + 1)];
}

return parts.map((part, i) => {
return i % 2 == 0
? part
: (
<Text key={`${this.state.search}${i}`} style={{backgroundColor: 'yellow'}}>{part}</Text>
);
});
}

public render() {
const text = "The quick brown fox jumped over the lazy dog.";
const parts = this.getTextParts(text);
return (
<View testID={'highlights'}>
<TextInput
placeholder="Enter search text"
value={this.state.search}
onChangeText={(text) => this.setState({search: text})} />
<Text>
{parts}
</Text>
</View>
)
}
}

export class TextExample extends React.Component<
{},
{toggle1: boolean; toggle2: boolean; toggle3: boolean}
Expand Down Expand Up @@ -792,6 +838,9 @@ export class TextExample extends React.Component<
</Text>
</View>
</RNTesterBlock>
<RNTesterBlock title="Dynamic backgroundColor">
<TextHighlightDemo />
</RNTesterBlock>
</RNTesterPage>
);
}
Expand Down

0 comments on commit 6067713

Please sign in to comment.