chore: refactor props and styles extraction

This commit is contained in:
Mikael Sand
2019-12-09 21:26:14 +02:00
parent 353eddc43c
commit c0ffc65609
11 changed files with 31 additions and 41 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { requireNativeComponent } from 'react-native'; import { requireNativeComponent } from 'react-native';
import extractProps, { propsAndStyles } from '../lib/extract/extractProps'; import { extract } from '../lib/extract/extractProps';
import { NumberProp } from '../lib/extract/types'; import { NumberProp } from '../lib/extract/types';
import Shape from './Shape'; import Shape from './Shape';
@@ -23,7 +23,7 @@ export default class Circle extends Shape<{
return ( return (
<RNSVGCircle <RNSVGCircle
ref={this.refMethod} ref={this.refMethod}
{...extractProps(propsAndStyles(props), this)} {...extract(this, props)}
cx={cx} cx={cx}
cy={cy} cy={cy}
r={r} r={r}
+2 -5
View File
@@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { requireNativeComponent } from 'react-native'; import { requireNativeComponent } from 'react-native';
import extractProps, { propsAndStyles } from '../lib/extract/extractProps'; import { extract } from '../lib/extract/extractProps';
import Shape from './Shape'; import Shape from './Shape';
export default class ClipPath extends Shape<{}> { export default class ClipPath extends Shape<{}> {
@@ -9,10 +9,7 @@ export default class ClipPath extends Shape<{}> {
render() { render() {
const { props } = this; const { props } = this;
return ( return (
<RNSVGClipPath <RNSVGClipPath ref={this.refMethod} {...extract(this, props)}>
ref={this.refMethod}
{...extractProps(propsAndStyles(props), this)}
>
{props.children} {props.children}
</RNSVGClipPath> </RNSVGClipPath>
); );
+2 -2
View File
@@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { requireNativeComponent } from 'react-native'; import { requireNativeComponent } from 'react-native';
import extractProps, { propsAndStyles } from '../lib/extract/extractProps'; import { extract } from '../lib/extract/extractProps';
import { NumberProp } from '../lib/extract/types'; import { NumberProp } from '../lib/extract/types';
import Shape from './Shape'; import Shape from './Shape';
@@ -25,7 +25,7 @@ export default class Ellipse extends Shape<{
return ( return (
<RNSVGEllipse <RNSVGEllipse
ref={this.refMethod} ref={this.refMethod}
{...extractProps(propsAndStyles(props), this)} {...extract(this, props)}
cx={cx} cx={cx}
cy={cy} cy={cy}
rx={rx} rx={rx}
+2 -2
View File
@@ -5,7 +5,7 @@ import {
requireNativeComponent, requireNativeComponent,
} from 'react-native'; } from 'react-native';
import { meetOrSliceTypes, alignEnum } from '../lib/extract/extractViewBox'; import { meetOrSliceTypes, alignEnum } from '../lib/extract/extractViewBox';
import extractProps, { propsAndStyles } from '../lib/extract/extractProps'; import { withoutXY } from '../lib/extract/extractProps';
import { NumberProp } from '../lib/extract/types'; import { NumberProp } from '../lib/extract/types';
import Shape from './Shape'; import Shape from './Shape';
@@ -50,7 +50,7 @@ export default class SvgImage extends Shape<{
return ( return (
<RNSVGImage <RNSVGImage
ref={this.refMethod} ref={this.refMethod}
{...extractProps({ ...propsAndStyles(props), x: null, y: null }, this)} {...withoutXY(this, props)}
x={x} x={x}
y={y} y={y}
width={width} width={width}
+2 -2
View File
@@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { requireNativeComponent } from 'react-native'; import { requireNativeComponent } from 'react-native';
import extractProps, { propsAndStyles } from '../lib/extract/extractProps'; import { extract } from '../lib/extract/extractProps';
import { NumberProp } from '../lib/extract/types'; import { NumberProp } from '../lib/extract/types';
import Shape from './Shape'; import Shape from './Shape';
@@ -25,7 +25,7 @@ export default class Line extends Shape<{
return ( return (
<RNSVGLine <RNSVGLine
ref={this.refMethod} ref={this.refMethod}
{...extractProps(propsAndStyles(props), this)} {...extract(this, props)}
x1={x1} x1={x1}
y1={y1} y1={y1}
x2={x2} x2={x2}
+2 -2
View File
@@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import { requireNativeComponent } from 'react-native'; import { requireNativeComponent } from 'react-native';
import extractTransform from '../lib/extract/extractTransform'; import extractTransform from '../lib/extract/extractTransform';
import extractProps, { propsAndStyles } from '../lib/extract/extractProps'; import { withoutXY } from '../lib/extract/extractProps';
import { NumberProp, TransformProps } from '../lib/extract/types'; import { NumberProp, TransformProps } from '../lib/extract/types';
import units from '../lib/units'; import units from '../lib/units';
import Shape from './Shape'; import Shape from './Shape';
@@ -41,7 +41,7 @@ export default class Mask extends Shape<{
return ( return (
<RNSVGMask <RNSVGMask
ref={this.refMethod} ref={this.refMethod}
{...extractProps({ ...propsAndStyles(props), x: null, y: null }, this)} {...withoutXY(this, props)}
x={x} x={x}
y={y} y={y}
width={width} width={width}
+2 -6
View File
@@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { requireNativeComponent } from 'react-native'; import { requireNativeComponent } from 'react-native';
import extractProps, { propsAndStyles } from '../lib/extract/extractProps'; import { extract } from '../lib/extract/extractProps';
import Shape from './Shape'; import Shape from './Shape';
export default class Path extends Shape<{ export default class Path extends Shape<{
@@ -11,11 +11,7 @@ export default class Path extends Shape<{
render() { render() {
const { props } = this; const { props } = this;
return ( return (
<RNSVGPath <RNSVGPath ref={this.refMethod} {...extract(this, props)} d={props.d} />
ref={this.refMethod}
{...extractProps(propsAndStyles(props), this)}
d={props.d}
/>
); );
} }
} }
+2 -9
View File
@@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { requireNativeComponent } from 'react-native'; import { requireNativeComponent } from 'react-native';
import extractProps, { propsAndStyles } from '../lib/extract/extractProps'; import { withoutXY } from '../lib/extract/extractProps';
import { NumberProp } from '../lib/extract/types'; import { NumberProp } from '../lib/extract/types';
import Shape from './Shape'; import Shape from './Shape';
@@ -27,14 +27,7 @@ export default class Rect extends Shape<{
return ( return (
<RNSVGRect <RNSVGRect
ref={this.refMethod} ref={this.refMethod}
{...extractProps( {...withoutXY(this, props)}
{
...propsAndStyles(props),
x: null,
y: null,
},
this,
)}
x={x} x={x}
y={y} y={y}
width={width} width={width}
+2 -9
View File
@@ -1,7 +1,7 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { requireNativeComponent } from 'react-native'; import { requireNativeComponent } from 'react-native';
import extractTransform from '../lib/extract/extractTransform'; import extractTransform from '../lib/extract/extractTransform';
import extractProps, { propsAndStyles } from '../lib/extract/extractProps'; import { withoutXY } from '../lib/extract/extractProps';
import { NumberProp, TransformProps } from '../lib/extract/types'; import { NumberProp, TransformProps } from '../lib/extract/types';
import extractText from '../lib/extract/extractText'; import extractText from '../lib/extract/extractText';
import { idPattern, pickNotNil } from '../lib/util'; import { idPattern, pickNotNil } from '../lib/util';
@@ -51,14 +51,7 @@ export default class TextPath extends Shape<{
const matched = href && href.match(idPattern); const matched = href && href.match(idPattern);
const match = matched && matched[1]; const match = matched && matched[1];
if (match) { if (match) {
const props = extractProps( const props = withoutXY(this, prop);
{
...propsAndStyles(prop),
x: null,
y: null,
},
this,
);
Object.assign( Object.assign(
props, props,
extractText( extractText(
+2 -2
View File
@@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { requireNativeComponent } from 'react-native'; import { requireNativeComponent } from 'react-native';
import extractProps, { propsAndStyles } from '../lib/extract/extractProps'; import { withoutXY } from '../lib/extract/extractProps';
import { NumberProp } from '../lib/extract/types'; import { NumberProp } from '../lib/extract/types';
import { idPattern } from '../lib/util'; import { idPattern } from '../lib/util';
import Shape from './Shape'; import Shape from './Shape';
@@ -48,7 +48,7 @@ export default class Use extends Shape<{
return ( return (
<RNSVGUse <RNSVGUse
ref={this.refMethod} ref={this.refMethod}
{...extractProps({ ...propsAndStyles(props), x: null, y: null }, this)} {...withoutXY(this, props)}
href={match} href={match}
x={x} x={x}
y={y} y={y}
+11
View File
@@ -149,3 +149,14 @@ export default function extractProps(
return extracted; return extracted;
} }
export function extract(instance: Object, props: Object & { style?: [] | {} }) {
return extractProps(propsAndStyles(props), instance);
}
export function withoutXY(
instance: Object,
props: Object & { style?: [] | {} },
) {
return extractProps({ ...propsAndStyles(props), x: null, y: null }, instance);
}