This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
HTMLElement.js
115 lines (102 loc) · 3.48 KB
/
HTMLElement.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import React from 'react'
import { Text, View } from 'react-native'
import shallowCompare from 'react-addons-shallow-compare'
import HTMLStyles from './HTMLStyles'
class HTMLElement extends React.Component {
/* ****************************************************************************/
// Class
/* ****************************************************************************/
static propTypes = {
tagName: React.PropTypes.string.isRequired,
renderers: React.PropTypes.object.isRequired,
groupInfo: React.PropTypes.object,
parentTagName: React.PropTypes.string,
htmlStyles: React.PropTypes.object,
htmlAttibs: React.PropTypes.object,
onLinkPress: React.PropTypes.func,
children: React.PropTypes.node,
parentIsText: React.PropTypes.bool.isRequired
}
/* ****************************************************************************/
// Data Lifecycle
/* ****************************************************************************/
shouldComponentUpdate (nextProps, nextState) {
return shallowCompare(this, nextProps, nextState)
}
/* ****************************************************************************/
// UI Events
/* ****************************************************************************/
handleLinkPressed (evt) {
if (this.props.onLinkPress) {
this.props.onLinkPress(evt, this.props.onLinkPressArg)
}
}
/* ****************************************************************************/
// Rendering
/* ****************************************************************************/
/**
* Generates the prefix nodes
* @return prefix nodes if applicable
*/
prefixNode () {
if (this.props.tagName === 'li') {
if (this.props.parentTagName === 'ol') {
return <Text>{`\n${this.props.groupInfo.index + 1}). `}</Text>
} else {
return <Text>{"\n• "}</Text>
}
} else {
return undefined
}
}
/**
* @return the class for this node
*/
elementClass () {
if (HTMLStyles.blockElements.has(this.props.tagName)) {
if (this.props.parentIsText) {
console.warn([
'You are trying to nest a non-text HTML element inside a text element.',
'The following nodes can only be rendered within themselves and not within text nodes:'
].concat(Array.from(HTMLStyles.blockElements)).join('\n'))
return Text
} else {
return View
}
} else {
return Text
}
}
render () {
const { htmlStyles, tagName, htmlAttribs, renderers, children, ...passProps } = this.props
if (renderers[tagName]) {
const copyProps = [
'htmlStyles',
'groupInfo',
'parentTagName',
'onLinkPress',
'parentIsText'
].reduce((acc, k) => {
acc[k] = this.props[k]
return acc
}, {})
return renderers[tagName](htmlAttribs, children, copyProps)
} else {
const RNElem = this.elementClass()
const styleset = RNElem === Text ? HTMLStyles.STYLESETS.TEXT : HTMLStyles.STYLESETS.VIEW
const style = []
.concat(
HTMLStyles.defaultStyles[tagName],
htmlStyles ? htmlStyles[tagName] : undefined,
htmlAttribs.style ? HTMLStyles.cssStringToRNStyle(htmlAttribs.style, styleset) : undefined
).filter((s) => s !== undefined)
return (
<RNElem {...passProps} style={style}>
{this.prefixNode()}
{this.props.children}
</RNElem>
)
}
}
}
module.exports = HTMLElement