Skip to content

Commit 5d592fd

Browse files
committed
Option to return a stateful component 🤞
1 parent efab0d4 commit 5d592fd

File tree

3 files changed

+2694
-4
lines changed

3 files changed

+2694
-4
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ npm install --save-dev babel-plugin-inline-react-svg
4444

4545
- *`ignorePattern`* - A pattern that imports will be tested against to selectively ignore imports.
4646
- *`caseSensitive`* - A boolean value that if true will require file paths to match with case-sensitivity. Useful to ensure consistent behavior if working on both a case-sensitive operating system like Linux and a case-insensitive one like OS X or Windows.
47+
- *`statefulComponent`* - If true return a stateful component that supports a svgRef property
4748
- *`svgo`* - svgo options (`false` to disable). Example:
4849
```json
4950
{

src/index.js

+48-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,30 @@ const buildSvgWithDefaults = template(`
1919
SVG_NAME.defaultProps = SVG_DEFAULT_PROPS_CODE;
2020
`);
2121

22+
const buildSvgSF = template(`
23+
class SVG_NAME extends React.Component {
24+
render (){
25+
const props = Object.assign({}, this.props);
26+
const refcb = this.props.svgRef ? this.props.svgRef : function(){}
27+
delete props.svgRef
28+
return SVG_CODE
29+
}
30+
}
31+
`);
32+
33+
const buildSvgWithDefaultsSF = template(`
34+
class SVG_NAME extends React.Component {
35+
render (){
36+
const props = Object.assign({}, this.props);
37+
const refcb = this.props.svgRef ? this.props.svgRef : function(){}
38+
delete props.svgRef
39+
return SVG_CODE
40+
}
41+
}
42+
SVG_NAME.defaultProps = SVG_DEFAULT_PROPS_CODE;
43+
`);
44+
45+
2246
let ignoreRegex;
2347

2448
export default ({ types: t }) => ({
@@ -38,7 +62,7 @@ export default ({ types: t }) => ({
3862
},
3963
ImportDeclaration(path, state) {
4064
const importPath = path.node.source.value;
41-
const { ignorePattern, caseSensitive } = state.opts;
65+
const { ignorePattern, caseSensitive, statefulComponent } = state.opts;
4266
const { file } = state;
4367
if (ignorePattern) {
4468
// Only set the ignoreRegex once:
@@ -96,15 +120,35 @@ export default ({ types: t }) => ({
96120
}
97121
});
98122

99-
svgCode.openingElement.attributes = keepProps;
123+
svgCode.openingElement.attributes = keepProps
124+
125+
if (statefulComponent){
126+
svgCode.openingElement.attributes.push(
127+
t.jSXAttribute(
128+
t.jSXIdentifier('ref'),
129+
t.jSXExpressionContainer(
130+
t.arrowFunctionExpression(
131+
[t.identifier('el')],
132+
t.callExpression(
133+
t.identifier('refcb'),
134+
[t.identifier('el')]
135+
)
136+
)
137+
)
138+
)
139+
);
140+
}
141+
100142
opts.SVG_DEFAULT_PROPS_CODE = t.objectExpression(defaultProps);
101143
}
102144

103145
if (opts.SVG_DEFAULT_PROPS_CODE) {
104-
const svgReplacement = buildSvgWithDefaults(opts);
146+
const svgReplacement = (
147+
statefulComponent ? buildSvgWithDefaultsSF(opts) : buildSvgWithDefaults(opts)
148+
);
105149
path.replaceWithMultiple(svgReplacement);
106150
} else {
107-
const svgReplacement = buildSvg(opts);
151+
const svgReplacement = statefulComponent ? buildSvgSF(opts) : buildSvg(opts);
108152
path.replaceWith(svgReplacement);
109153
}
110154
file.get('ensureReact')();

0 commit comments

Comments
 (0)