Skip to content

Commit 9647e76

Browse files
committed
Changed method signature of onLinkPress to pass an object with url and other data attributes of the link node.
1 parent c808625 commit 9647e76

4 files changed

Lines changed: 182 additions & 174 deletions

File tree

HTMLView.js

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import React, {PureComponent} from 'react';
1+
import React, { PureComponent } from 'react';
22
import PropTypes from 'prop-types';
33
import htmlToElement from './htmlToElement';
4-
import {Linking, Platform, StyleSheet, View, ViewPropTypes} from 'react-native';
5-
6-
const boldStyle = {fontWeight: '500'};
7-
const italicStyle = {fontStyle: 'italic'};
8-
const underlineStyle = {textDecorationLine: 'underline'};
9-
const codeStyle = {fontFamily: Platform.OS === 'ios' ? 'Menlo' : 'monospace'};
4+
import {
5+
Linking,
6+
Platform,
7+
StyleSheet,
8+
View,
9+
ViewPropTypes
10+
} from 'react-native';
11+
12+
const boldStyle = { fontWeight: '500' };
13+
const italicStyle = { fontStyle: 'italic' };
14+
const underlineStyle = { textDecorationLine: 'underline' };
15+
const codeStyle = { fontFamily: Platform.OS === 'ios' ? 'Menlo' : 'monospace' };
1016

1117
const baseStyles = StyleSheet.create({
1218
b: boldStyle,
@@ -18,14 +24,14 @@ const baseStyles = StyleSheet.create({
1824
code: codeStyle,
1925
a: {
2026
fontWeight: '500',
21-
color: '#007AFF',
27+
color: '#007AFF'
2228
},
23-
h1: {fontWeight: '500', fontSize: 36},
24-
h2: {fontWeight: '500', fontSize: 30},
25-
h3: {fontWeight: '500', fontSize: 24},
26-
h4: {fontWeight: '500', fontSize: 18},
27-
h5: {fontWeight: '500', fontSize: 14},
28-
h6: {fontWeight: '500', fontSize: 12},
29+
h1: { fontWeight: '500', fontSize: 36 },
30+
h2: { fontWeight: '500', fontSize: 30 },
31+
h3: { fontWeight: '500', fontSize: 24 },
32+
h4: { fontWeight: '500', fontSize: 18 },
33+
h5: { fontWeight: '500', fontSize: 14 },
34+
h6: { fontWeight: '500', fontSize: 12 }
2935
});
3036

3137
const htmlToElementOptKeys = [
@@ -35,14 +41,14 @@ const htmlToElementOptKeys = [
3541
'TextComponent',
3642
'textComponentProps',
3743
'NodeComponent',
38-
'nodeComponentProps',
44+
'nodeComponentProps'
3945
];
4046

4147
class HtmlView extends PureComponent {
4248
constructor() {
4349
super();
4450
this.state = {
45-
element: null,
51+
element: null
4652
};
4753
}
4854

@@ -52,8 +58,16 @@ class HtmlView extends PureComponent {
5258
}
5359

5460
componentWillReceiveProps(nextProps) {
55-
if (this.props.value !== nextProps.value || this.props.stylesheet !== nextProps.stylesheet || this.props.textComponentProps !== nextProps.textComponentProps) {
56-
this.startHtmlRender(nextProps.value, nextProps.stylesheet, nextProps.textComponentProps);
61+
if (
62+
this.props.value !== nextProps.value ||
63+
this.props.stylesheet !== nextProps.stylesheet ||
64+
this.props.textComponentProps !== nextProps.textComponentProps
65+
) {
66+
this.startHtmlRender(
67+
nextProps.value,
68+
nextProps.stylesheet,
69+
nextProps.textComponentProps
70+
);
5771
}
5872
}
5973

@@ -68,19 +82,19 @@ class HtmlView extends PureComponent {
6882
onLinkLongPress,
6983
stylesheet,
7084
renderNode,
71-
onError,
85+
onError
7286
} = this.props;
7387

7488
if (!value) {
75-
this.setState({element: null});
89+
this.setState({ element: null });
7690
}
7791

7892
const opts = {
7993
addLineBreaks,
8094
linkHandler: onLinkPress,
8195
linkLongPressHandler: onLinkLongPress,
82-
styles: {...baseStyles, ...stylesheet, ...style},
83-
customRenderer: renderNode,
96+
styles: { ...baseStyles, ...stylesheet, ...style },
97+
customRenderer: renderNode
8498
};
8599

86100
htmlToElementOptKeys.forEach(key => {
@@ -99,30 +113,22 @@ class HtmlView extends PureComponent {
99113
}
100114

101115
if (this.mounted) {
102-
this.setState({element});
116+
this.setState({ element });
103117
}
104118
});
105119
}
106120

107121
render() {
108-
const {RootComponent, style} = this.props;
109-
const {element} = this.state;
122+
const { RootComponent, style } = this.props;
123+
const { element } = this.state;
110124
if (element) {
111125
return (
112-
<RootComponent
113-
{...this.props.rootComponentProps}
114-
style={style}
115-
>
126+
<RootComponent {...this.props.rootComponentProps} style={style}>
116127
{element}
117128
</RootComponent>
118129
);
119130
}
120-
return (
121-
<RootComponent
122-
{...this.props.rootComponentProps}
123-
style={style}
124-
/>
125-
);
131+
return <RootComponent {...this.props.rootComponentProps} style={style} />;
126132
}
127133
}
128134

@@ -143,15 +149,15 @@ HtmlView.propTypes = {
143149
stylesheet: PropTypes.object,
144150
TextComponent: PropTypes.func,
145151
textComponentProps: PropTypes.object,
146-
value: PropTypes.string,
152+
value: PropTypes.string
147153
};
148154

149155
HtmlView.defaultProps = {
150156
addLineBreaks: true,
151-
onLinkPress: url => Linking.openURL(url),
157+
onLinkPress: ({ url }) => Linking.openURL(url),
152158
onLinkLongPress: null,
153159
onError: console.error.bind(console),
154-
RootComponent: View,
160+
RootComponent: View
155161
};
156162

157163
export default HtmlView;

0 commit comments

Comments
 (0)