Skip to content

Commit

Permalink
Remove unnecessary parts
Browse files Browse the repository at this point in the history
+ handling of props is taken care by {...this.props}. {this.props.x ?
this.props.x : "default"} becomes unnecessary.
+ Correct README regarding this
+ Add to README - class to className change under internals
  • Loading branch information
boopathi committed Apr 17, 2016
1 parent c5c7e66 commit a162ecf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 43 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,24 @@ The props passed to the output component is passed on to the root SVG node and t
is transformed to

```html
<svg width={this.props.width ? this.props.width : '50'} {...this.props}>
<svg width="50" {...this.props}>
...
</svg>
```

#### 4. export React.Component
#### 4. class to className

```html
<svg class="hello"/>
```

is transformed to

```html
<svg className="hello"/>
```

#### 5. export React.Component

The loader should now export the svg component. And this is done by wrapping it in a Component Class.

Expand Down
45 changes: 4 additions & 41 deletions src/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,50 +52,18 @@ export default function (babel) {
}
};

// Flow props from Output component to root SVG
// converts
// <svg width="50">
// to
// <svg width={this.props.width ? this.props.width : "50"}>
const attrVisitor = {
JSXAttribute(path) {
if (!t.isJSXIdentifier(path.node.name)) return;
// don't handle style attr. It needs to be an object
if (path.node.name.name === 'style') return;

// else
const valueExpression = t.memberExpression(
t.memberExpression(
t.thisExpression(),
t.identifier('props')
),
t.identifier(hyphenToCamel(path.node.name.name))
);

path.node.value = t.jSXExpressionContainer(
t.conditionalExpression(
valueExpression,
valueExpression,
t.stringLiteral(path.node.value.value)
)
);

}
};

// converts
// <svg>
// to
// <svg {...this.props}>
// after passing through attributes visitors
const svgVisitor = {
JSXOpeningElement(path) {
if (t.isJSXIdentifier(path.node.name) && path.node.name.name.toLowerCase() === 'svg') {
path.traverse(classNameVisitor);
path.traverse(camelizeVisitor);
path.traverse(attrVisitor);
path.traverse(styleAttrVisitor);
path.traverse(classNameVisitor);
path.traverse(camelizeVisitor);
path.traverse(styleAttrVisitor);

if (path.node.name.name.toLowerCase() === 'svg') {
// add spread props
path.node.attributes.push(
t.jSXSpreadAttribute(
Expand All @@ -105,11 +73,6 @@ export default function (babel) {
)
)
);
} else {
// don't ignore style attr transformations for other nodes
path.traverse(classNameVisitor);
path.traverse(camelizeVisitor);
path.traverse(styleAttrVisitor);
}
}
};
Expand Down

0 comments on commit a162ecf

Please sign in to comment.