Add setNativeProps method for most of elements

This commit is contained in:
Horcrux
2016-07-18 18:34:54 +08:00
parent 2516a778ba
commit efa7363b6f
13 changed files with 74 additions and 3 deletions

View File

@@ -7,7 +7,8 @@ import Svg, {
Circle, Circle,
Line, Line,
Rect, Rect,
Text Text,
Use
} from 'react-native-svg'; } from 'react-native-svg';
class GExample extends Component{ class GExample extends Component{
@@ -63,6 +64,7 @@ class GTransform extends Component{
<G <G
rotate="50" rotate="50"
origin="100, 50" origin="100, 50"
id="group"
> >
<Line <Line
x1="60" x1="60"
@@ -90,6 +92,7 @@ class GTransform extends Component{
> >
Text grouped with shapes</Text> Text grouped with shapes</Text>
</G> </G>
<Use href="#group" x="5" rotate="0" />
</Svg>; </Svg>;
} }
} }

View File

@@ -569,6 +569,7 @@ npm install
2. more Text features support 2. more Text features support
3. Pattern element 3. Pattern element
4. implement Animated elements 4. implement Animated elements
5. refactor defs element (cannot use id prop for shape elements)
#### Thanks: #### Thanks:

View File

@@ -20,9 +20,14 @@ class Circle extends Shape {
svgId: numberProp svgId: numberProp
}; };
setNativeProps = (...args) => {
this.root.setNativeProps(...args);
};
render() { render() {
let props = mergeContext(this.props, this.context); let props = mergeContext(this.props, this.context);
return <RNSVGCircle return <RNSVGCircle
ref={ele => this.root = ele}
{...this.extractProps(props)} {...this.extractProps(props)}
cx={props.cx.toString()} cx={props.cx.toString()}
cy={props.cy.toString()} cy={props.cy.toString()}

View File

@@ -20,9 +20,14 @@ class Ellipse extends Shape{
svgId: numberProp svgId: numberProp
}; };
setNativeProps = (...args) => {
this.root.setNativeProps(...args);
};
render() { render() {
let props = mergeContext(this.props, this.context); let props = mergeContext(this.props, this.context);
return <RNSVGEllipse return <RNSVGEllipse
ref={ele => this.root = ele}
{...this.extractProps(props)} {...this.extractProps(props)}
cx={props.cx.toString()} cx={props.cx.toString()}
cy={props.cy.toString()} cy={props.cy.toString()}

View File

@@ -33,6 +33,14 @@ class G extends Component{
return _.defaults({}, this.context, context); return _.defaults({}, this.context, context);
}; };
setNativeProps = (...args) => {
this.getNativeElement().setNativeProps(...args);
};
getNativeElement = () => {
return this.refs.root || this.root;
};
render() { render() {
if (this.props.id) { if (this.props.id) {
return <Defs.Item return <Defs.Item
@@ -42,12 +50,14 @@ class G extends Component{
> >
<G <G
{...this.props} {...this.props}
ref={ele => this.root = ele.refs.root}
id={null} id={null}
/> />
</Defs.Item>; </Defs.Item>;
} else { } else {
return <RNSVGGroup return <RNSVGGroup
{...extractProps(this.props, {transform: true})} {...extractProps(this.props, {transform: true})}
ref="root"
asClipPath={this.props.asClipPath} asClipPath={this.props.asClipPath}
> >
{this.props.children} {this.props.children}

View File

@@ -19,10 +19,14 @@ class Image extends Shape {
//preserveAspectRatio: PropTypes.string //preserveAspectRatio: PropTypes.string
}; };
setNativeProps = (...args) => {
this.root.setNativeProps(...args);
};
render() { render() {
let {props} = this; let {props} = this;
return <RNSVGImage return <RNSVGImage
ref={ele => this.root = ele}
{...this.extractProps(props, {transform: true, responder: true})} {...this.extractProps(props, {transform: true, responder: true})}
x={props.x.toString()} x={props.x.toString()}
y={props.y.toString()} y={props.y.toString()}

View File

@@ -20,9 +20,14 @@ class Line extends Shape {
svgId: numberProp svgId: numberProp
}; };
setNativeProps = (...args) => {
this.root.setNativeProps(...args);
};
render() { render() {
let props = mergeContext(this.props, this.context); let props = mergeContext(this.props, this.context);
return <RNSVGLine return <RNSVGLine
ref={ele => this.root = ele}
{...this.extractProps(props)} {...this.extractProps(props)}
x1={props.x1.toString()} x1={props.x1.toString()}
y1={props.y1.toString()} y1={props.y1.toString()}

View File

@@ -30,6 +30,14 @@ class Path extends Shape {
} }
}; };
setNativeProps = (...args) => {
this.getNativeElement().setNativeProps(...args);
};
getNativeElement = () => {
return this.refs.root || this.root;
};
render() { render() {
let props = mergeContext(this.props, this.context); let props = mergeContext(this.props, this.context);
@@ -39,13 +47,18 @@ class Path extends Shape {
svgId={props.svgId} svgId={props.svgId}
visible={true} visible={true}
> >
<Path {...props} id={null} /> <Path
ref={ele => this.root = ele.refs.root}
{...props}
id={null}
/>
</Defs.Item>; </Defs.Item>;
} }
let d = new SerializablePath(props.d).toJSON(); let d = new SerializablePath(props.d).toJSON();
return ( return (
<RNSVGPath <RNSVGPath
ref="root"
{...this.extractProps(props)} {...this.extractProps(props)}
d={d} d={d}
/> />

View File

@@ -9,8 +9,13 @@ class Polygon extends Component{
points: PropTypes.oneOfType([PropTypes.string, PropTypes.array]) points: PropTypes.oneOfType([PropTypes.string, PropTypes.array])
}; };
setNativeProps = (...args) => {
this.root.getNativeElement().setNativeProps(...args);
};
render() { render() {
return <Path return <Path
ref={ele => this.root = ele}
{...this.props} {...this.props}
d={`M${this.props.points.trim().replace(/\s+/g, 'L')}z`} d={`M${this.props.points.trim().replace(/\s+/g, 'L')}z`}
/>; />;

View File

@@ -9,8 +9,13 @@ class Polyline extends Component{
points: PropTypes.oneOfType([PropTypes.string, PropTypes.array]) points: PropTypes.oneOfType([PropTypes.string, PropTypes.array])
}; };
setNativeProps = (...args) => {
this.root.getNativeElement().setNativeProps(...args);
};
render() { render() {
return <Path return <Path
ref={ele => this.root = ele}
{...this.props} {...this.props}
d={`M${this.props.points.trim().replace(/\s+/g, 'L')}`} d={`M${this.props.points.trim().replace(/\s+/g, 'L')}`}
/>; />;

View File

@@ -21,10 +21,15 @@ class Rect extends Shape {
svgId: numberProp svgId: numberProp
}; };
setNativeProps = (...args) => {
this.root.setNativeProps(...args);
};
render() { render() {
let props = mergeContext(this.props, this.context); let props = mergeContext(this.props, this.context);
return <RNSVGRect return <RNSVGRect
ref={ele => this.root = ele}
{...this.extractProps({ {...this.extractProps({
...props, ...props,
x: null, x: null,

View File

@@ -25,6 +25,14 @@ class Text extends Shape {
svgId: numberProp svgId: numberProp
}; };
setNativeProps = (...args) => {
this.getNativeElement().setNativeProps(...args);
};
getNativeElement = () => {
return this.refs.root || this.root;
};
render() { render() {
let props = mergeContext(this.props, this.context); let props = mergeContext(this.props, this.context);
@@ -39,6 +47,7 @@ class Text extends Shape {
if (this.props.id) { if (this.props.id) {
return <Defs.Item return <Defs.Item
ref={ele => this.root = ele.refs.root}
id={this.props.id} id={this.props.id}
svgId={this.props.svgId} svgId={this.props.svgId}
visible={true} visible={true}
@@ -50,6 +59,7 @@ class Text extends Shape {
return ( return (
<RNSVGText <RNSVGText
ref="root"
{...extractProps({...props, x, y})} {...extractProps({...props, x, y})}
{...extractText(props)} {...extractText(props)}
/> />

View File

@@ -1,5 +1,5 @@
{ {
"version": "3.1.0", "version": "3.1.1",
"name": "react-native-svg", "name": "react-native-svg",
"description": "react native svg library", "description": "react native svg library",
"repository": { "repository": {