Skip to content

Commit 12a9300

Browse files
albinotonninaljharb
authored andcommitted
Option to return a stateful component 🤞
1 parent 7dc34fa commit 12a9300

File tree

3 files changed

+2694
-4
lines changed

3 files changed

+2694
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
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

Lines changed: 48 additions & 4 deletions
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:
@@ -94,15 +118,35 @@ export default ({ types: t }) => ({
94118
}
95119
});
96120

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

101143
if (opts.SVG_DEFAULT_PROPS_CODE) {
102-
const svgReplacement = buildSvgWithDefaults(opts);
144+
const svgReplacement = (
145+
statefulComponent ? buildSvgWithDefaultsSF(opts) : buildSvgWithDefaults(opts)
146+
);
103147
path.replaceWithMultiple(svgReplacement);
104148
} else {
105-
const svgReplacement = buildSvg(opts);
149+
const svgReplacement = statefulComponent ? buildSvgSF(opts) : buildSvg(opts);
106150
path.replaceWith(svgReplacement);
107151
}
108152
file.get('ensureReact')();

0 commit comments

Comments
 (0)