Skip to content

Commit ddfae22

Browse files
committed
fix(babel-preset): expandProps + icon option
Closes #277
1 parent d01d33f commit ddfae22

2 files changed

Lines changed: 46 additions & 30 deletions

File tree

packages/babel-preset/src/index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const plugin = (api, opts) => {
6363

6464
const plugins = [
6565
[transformSvgComponent, opts],
66+
...(opts.icon && opts.dimensions ? [svgEmDimensions] : []),
6667
[
6768
removeJSXAttribute,
6869
{ elements: ['svg', 'Svg'], attributes: toRemoveAttributes },
@@ -81,10 +82,6 @@ const plugin = (api, opts) => {
8182
])
8283
}
8384

84-
if (opts.icon && opts.dimensions) {
85-
plugins.push(svgEmDimensions)
86-
}
87-
8885
if (opts.titleProp) {
8986
plugins.push(svgDynamicTitle)
9087
}

packages/babel-preset/src/index.test.js

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ describe('preset', () => {
2424
},
2525
}),
2626
).toMatchInlineSnapshot(`
27-
"import React from \\"react\\";
28-
29-
const SvgComponent = () => <svg foo=\\"bar\\" x={y} />;
30-
31-
export default SvgComponent;"
32-
`)
27+
"import React from \\"react\\";
28+
29+
const SvgComponent = () => <svg foo=\\"bar\\" x={y} />;
30+
31+
export default SvgComponent;"
32+
`)
3333
})
3434

3535
it('should handle native expo option', () => {
@@ -41,13 +41,13 @@ export default SvgComponent;"
4141
},
4242
}),
4343
).toMatchInlineSnapshot(`
44-
"import React from \\"react\\";
45-
import { Svg } from \\"expo\\";
46-
47-
const SvgComponent = () => <Svg><Svg.G><Svg.Path d=\\"M0 0h48v1H0z\\" /></Svg.G></Svg>;
48-
49-
export default SvgComponent;"
50-
`)
44+
"import React from \\"react\\";
45+
import { Svg } from \\"expo\\";
46+
47+
const SvgComponent = () => <Svg><Svg.G><Svg.Path d=\\"M0 0h48v1H0z\\" /></Svg.G></Svg>;
48+
49+
export default SvgComponent;"
50+
`)
5151
})
5252

5353
it('should handle titleProp', () => {
@@ -59,14 +59,14 @@ export default SvgComponent;"
5959
},
6060
}),
6161
).toMatchInlineSnapshot(`
62-
"import React from \\"react\\";
63-
64-
const SvgComponent = ({
65-
title
66-
}) => <svg><title>{title}</title></svg>;
67-
68-
export default SvgComponent;"
69-
`)
62+
"import React from \\"react\\";
63+
64+
const SvgComponent = ({
65+
title
66+
}) => <svg><title>{title}</title></svg>;
67+
68+
export default SvgComponent;"
69+
`)
7070
})
7171

7272
it('should handle replaceAttrValues', () => {
@@ -81,11 +81,30 @@ export default SvgComponent;"
8181
},
8282
}),
8383
).toMatchInlineSnapshot(`
84-
"import React from \\"react\\";
85-
86-
const SvgComponent = () => <svg a=\\"black\\" b={props.white} />;
84+
"import React from \\"react\\";
85+
86+
const SvgComponent = () => <svg a=\\"black\\" b={props.white} />;
87+
88+
export default SvgComponent;"
89+
`)
90+
})
8791

88-
export default SvgComponent;"
89-
`)
92+
it('should handle expandProps & icon & dimensions', () => {
93+
expect(
94+
testPreset('<svg a="#000" b="#fff" />', {
95+
expandProps: 'end',
96+
icon: true,
97+
dimensions: true,
98+
state: {
99+
componentName: 'SvgComponent',
100+
},
101+
}),
102+
).toMatchInlineSnapshot(`
103+
"import React from \\"react\\";
104+
105+
const SvgComponent = props => <svg a=\\"#000\\" b=\\"#fff\\" width=\\"1em\\" height=\\"1em\\" {...props} />;
106+
107+
export default SvgComponent;"
108+
`)
90109
})
91110
})

0 commit comments

Comments
 (0)