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 ce84d16
Showing 1 changed file with 50 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,49 @@ 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 (let 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) => (
<Text key={i} style={i % 2 === 0 ? undefined : styles.highlight}>
{part}
</Text>
));
}

public render() {
const exampleText = 'The quick brown fox jumped over the lazy dog.';
const parts = this.getTextParts(exampleText);
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 +836,9 @@ export class TextExample extends React.Component<
</Text>
</View>
</RNTesterBlock>
<RNTesterBlock title="Dynamic backgroundColor">
<TextHighlightDemo />
</RNTesterBlock>
</RNTesterPage>
);
}
Expand Down Expand Up @@ -825,6 +872,9 @@ export const styles = StyleSheet.create({
borderColor: 'black',
width: 400,
},
highlight: {
backgroundColor: 'yellow',
},
});

export const displayName = (_undefined?: string) => {};
Expand Down

0 comments on commit ce84d16

Please sign in to comment.