Skip to content

Commit

Permalink
Merge pull request #16 from fram-x/release/0.2.0
Browse files Browse the repository at this point in the history
Release/0.2.0
  • Loading branch information
bjornegil authored Oct 31, 2018
2 parents 7d6e067 + ec8ff94 commit ec507bf
Show file tree
Hide file tree
Showing 7 changed files with 843 additions and 23 deletions.
2 changes: 1 addition & 1 deletion App.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class App extends Component<Props> {
return (
<View style={styles.container}>
<StyledText
text="Welcome to <b><u>React Native</u> <demo><i>Styled</i> Text</demo></b> demo!"
text="Welcome to <b>React <u>Native</u> <demo><i>Styled</i> Text</demo></b> demo!"
style={styles.welcome}
textStyles={textStyles}
/>
Expand Down
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Renders as
<img src="https://github.com/fram-x/react-native-styled-text/raw/develop/docs/happyStyling.png" width="200">

### Using custom styles
For richer styling, you set the `textStyles` property of `StyledText` to an object (`StyleSheet`) containing your custom text styles and apply the styles in the `text` property.
For richer styling, you set the `textStyles` property of `StyledText` to an object (e.g. `StyleSheet`) containing your custom text styles and apply these styles in the `text` property.

Example:

Expand Down Expand Up @@ -100,15 +100,37 @@ Renders as

<img src="https://github.com/fram-x/react-native-styled-text/raw/develop/docs/welcome.png" width="280">

## How it works

Internally, the `render` function of `StyledText` parses the value of the `text` property and returns a nested structure of React Native [`Text`](https://facebook.github.io/react-native/docs/text) components.

From the example above:

```javascript
<StyledText
text="Ha<i>pp</i>y <b>Styling</b>!"
style={styles.header}
/>
```
would be transformed to:

```javascript
<Text
style={styles.header}
>Ha<Text style={defaultStyles.i}>pp</Text>y <Text style={defaultStyles.b}>Styling</Text>!
</Text>
```

So `StyledText` just provides a more compact, readable and flexible coding of nested `Text` components.


## API
`StyledText` exposes the following properties:
In addition to the React Native `Text` properties, `StyledText` supports the following properties:

| Name | Description |
| ---- | ----------- |
| text | String with style tags for mixed styling of the text. Each style tag must match one of the styles provided in textStyles or one of the default styles, see below. |
| style | Base style for the component, typically including layout properties. (Optional) |
| textStyles | Object (`StyleSheet`) containing definition of the styles used in the provided text. (Optional) |
| textStyles | Object (e.g. `StyleSheet`) containing definition of the styles used in the provided text. (Optional) |

The following default styles are defined:

Expand Down
19 changes: 5 additions & 14 deletions lib/StyledText/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
import * as React from 'react';
import { StyleSheet } from 'react-native';
import { StyleSheet, TextProps } from 'react-native';

import { renderStyledText } from './renderer';

type Props = {
type Props = TextProps & {
text: String,
style: StyleSheet.Style,
textStyles: StyleSheet.StyleSheet,
}

class StyledText extends React.Component<Props> {
shouldComponentUpdate = (nextProps) => {
return (
this.props.text !== nextProps.text ||
this.props.style !== nextProps.style ||
this.props.textStyles !== nextProps.textStyles
);
}

class StyledText extends React.PureComponent<Props> {
render() {
const { text, style, textStyles } = this.props;
return renderStyledText(text, style, textStyles);
const { text, textStyles, ...textProps } = this.props;
return renderStyledText(text, textStyles, textProps);
}
}

Expand Down
4 changes: 2 additions & 2 deletions lib/StyledText/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const renderMixedText = (mixedText: Mixed, textStyles: Object) => mixedText.map(
)
));

export const renderStyledText = (text, style, textStyles) => {
export const renderStyledText = (text, textStyles, textProps) => {
const mixedText = parse(text);
const styles = textStyles || {}
verifyTextStyles(mixedText, styles, defaultStyles);
Expand All @@ -38,7 +38,7 @@ export const renderStyledText = (text, style, textStyles) => {

return React.createElement(
Text,
{ style: style },
textProps,
...textElements,
);
};
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-styled-text",
"version": "0.1.4",
"version": "0.2.0",
"description": "A React Native component for easy rendering of styled text.",
"main": "./index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-styled-text-ex",
"version": "0.1.4",
"version": "0.2.0",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
Expand Down
Loading

0 comments on commit ec507bf

Please sign in to comment.