forked from brentvatne/react-native-svgkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Svg.js
81 lines (66 loc) · 2.06 KB
/
Svg.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
/**
* @providesModule Svg
* @flow
*/
'use strict';
var React = require('react-native');
var ReactChildren = require('ReactChildren');
var {
View,
PropTypes,
requireNativeComponent,
} = React;
var Svg = React.createClass({
propTypes: {
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
},
statics: {
PathSerializer: (path) => {
var { fill, stroke, strokeWidth, strokeMiterLimit, d, transform } = path.props;
return `<path fill="${fill}" stroke="${stroke}" stroke-width="${strokeWidth}" d="${d}" transform="${transform}"/>`
},
LineSerializer: (line) => {
var { x1, x2, y1, y2, style } = path.props;
return `<path fill="${fill}" stroke="${stroke}" stroke-width="${strokeWidth}" d="${d}" transform="${transform}"/>`
},
PolygonSerializer: (polygon) => {
var { fill, stroke, strokeWidth, points, transform } = path.props;
return `<polygon fill="${fill}" stroke="${stroke}" stroke-width="${strokeWidth}" points="${points}" transform="${transform}"`
}
},
getInitialState() {
return { data: "" }
},
serialize(el) {
return Svg[el.type.displayName + 'Serializer'](el);
},
stateFromChildren() {
var data = `<svg xmlns="http://www.w3.org/2000/svg" width="${this.props.width}px" height="${this.props.height}px">`;
ReactChildren.forEach(this.props.children, (child) => {
data = data + this.serialize(child);
});
data = data + "</svg>"
return data;
},
render() {
var nativeProps = {
style: this.props.style,
originalWidth: this.props.width,
originalHeight: this.props.height,
forceUpdate: this.props.forceUpdate,
};
if (this.props.source) {
nativeProps.src = this.props.source.uri;
} else if (this.props.data) {
nativeProps.data = this.props.data;
} else {
nativeProps.data = this.stateFromChildren();
}
return <RNSvg {...nativeProps} />
},
});
var RNSvg = requireNativeComponent('RNSvg', null);
Svg.Path = require('./Path');
Svg.Polygon = require('./Polygon');
module.exports = Svg;