Skip to content

Commit 2f08b39

Browse files
committed
feat: Add view props to svg component
1 parent b488e0a commit 2f08b39

9 files changed

Lines changed: 38 additions & 13 deletions

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@types/mkdirp": "^0.5.2",
3939
"@types/node": "^12.7.2",
4040
"@types/react": "^16.9.2",
41+
"@types/react-native": "^0.60.8",
4142
"@types/xml2js": "^0.4.4",
4243
"react": "^16.9.0",
4344
"react-native": "^0.60.5",

src/libs/generateComponent.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ export const generateComponent = (data: XmlData, config: Config) => {
4040
svgComponents.add('Svg');
4141
}
4242

43+
if (config.use_typescript) {
44+
svgComponents.add('GProps');
45+
}
46+
4347
mkdirp.sync(saveDir);
4448
glob.sync(path.join(saveDir, '*')).forEach((file) => fs.unlinkSync(file));
4549

@@ -54,6 +58,10 @@ export const generateComponent = (data: XmlData, config: Config) => {
5458

5559
names.push(iconIdAfterTrim);
5660

61+
if (config.use_typescript) {
62+
currentSvgComponents.add('GProps');
63+
}
64+
5765
for (const domName of Object.keys(item)) {
5866
switch (domName) {
5967
case 'path':
@@ -77,7 +85,7 @@ export const generateComponent = (data: XmlData, config: Config) => {
7785
}
7886

7987
imports.push(componentName);
80-
cases += `${whitespace(6)}return <${componentName} size={size} color={color} />;\n`;
88+
cases += `${whitespace(6)}return <${componentName} size={size} color={color} {...rest} />;\n`;
8189

8290
singleFile = getTemplate('SingleIcon' + extension);
8391
singleFile = replaceSize(singleFile, config.default_icon_size);
@@ -128,7 +136,7 @@ export const generateComponent = (data: XmlData, config: Config) => {
128136
};
129137

130138
const generateCase = (data: XmlData['svg']['symbol'][number], baseIdent: number) => {
131-
let template = `\n${whitespace(baseIdent)}<Svg viewBox="${data.$.viewBox}" width={size} height={size}>\n`;
139+
let template = `\n${whitespace(baseIdent)}<Svg viewBox="${data.$.viewBox}" width={size} height={size} {...rest}>\n`;
132140

133141
for (const domName of Object.keys(data)) {
134142
let realDomName = SVG_MAP[domName];

src/templates/Icon.d.ts.template

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { FunctionComponent } from 'react';
2+
// Don't forget to install package: @types/react-native
3+
import { ViewProps } from 'react-native';
4+
import { GProps } from 'react-native-svg';
25

3-
interface Props {
6+
interface Props extends GProps, ViewProps {
7+
name: '#names#';
48
size?: number;
59
color?: string | string[];
6-
name: '#names#';
710
}
811

912
declare const Icon: FunctionComponent<Props>;

src/templates/Icon.jsx.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import React from 'react';
66
#imports#
77

88
#comments#
9-
const Icon = ({ color, name, size }) => {
9+
const Icon = ({ color, name, size, ...rest }) => {
1010
switch (name) {
1111
#cases#
1212
}

src/templates/Icon.tsx.template

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
/* eslint-disable */
33

44
import React, { FunctionComponent } from 'react';
5+
import {ViewProps} from 'react-native';
56
#svgComponents#
67
#imports#
78

89
export type IconNames = '#names#';
910

10-
interface Props {
11+
interface Props extends GProps, ViewProps {
12+
name: IconNames;
1113
size?: number;
1214
color?: string | string[];
13-
name: IconNames;
1415
}
1516

1617
#comments#
17-
const Icon: FunctionComponent<Props> = ({ color, name, size }) => {
18+
const Icon: FunctionComponent<Props> = ({ color, name, size, ...rest }) => {
1819
switch (name) {
1920
#cases#
2021
}

src/templates/SingleIcon.d.ts.template

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { FunctionComponent } from 'react';
2+
// Don't forget to install package: @types/react-native
3+
import { ViewProps } from 'react-native';
4+
import { GProps } from 'react-native-svg';
25

3-
interface Props {
6+
interface Props extends GProps, ViewProps {
47
size?: number;
58
color?: string | string[];
69
}

src/templates/SingleIcon.jsx.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import React from 'react';
55
#svgComponents#
66

77
#comments#
8-
const #componentName# = ({ size, color }) => {
8+
const #componentName# = ({ size, color, ...rest }) => {
99
return (#iconContent# );
1010
};
1111

src/templates/SingleIcon.tsx.template

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
/* eslint-disable */
33

44
import React, { FunctionComponent } from 'react';
5+
import { ViewProps } from 'react-native';
56
#svgComponents#
67

7-
interface Props {
8+
interface Props extends GProps, ViewProps {
89
size?: number;
910
color?: string | string[];
1011
}
1112

1213
#comments#
13-
const #componentName#: FunctionComponent<Props> = ({ size, color }) => {
14+
const #componentName#: FunctionComponent<Props> = ({ size, color, ...rest }) => {
1415
return (#iconContent# );
1516
};
1617

yarn.lock

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,15 @@
871871
resolved "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.1.tgz#f1a11e7babb0c3cad68100be381d1e064c68f1f6"
872872
integrity sha512-CFzn9idOEpHrgdw8JsoTkaDDyRWk1jrzIV8djzcgpq0y9tG4B4lFT+Nxh52DVpDXV+n4+NPNv7M1Dj5uMp6XFg==
873873

874-
"@types/react@^16.9.2":
874+
"@types/react-native@^0.60.8":
875+
version "0.60.8"
876+
resolved "https://registry.npmjs.org/@types/react-native/-/react-native-0.60.8.tgz#71f340b4b97ed0b1161f942354e0eebc9fbe5b00"
877+
integrity sha512-bk13deCDm/DiNFdrF8Y5je++G9NPaCT/VxkSBY1Rcxu3rWZQIQal9uIH0Qc0Vi9sRX9us32EPevYylbscH3wEg==
878+
dependencies:
879+
"@types/prop-types" "*"
880+
"@types/react" "*"
881+
882+
"@types/react@*", "@types/react@^16.9.2":
875883
version "16.9.2"
876884
resolved "https://registry.npmjs.org/@types/react/-/react-16.9.2.tgz#6d1765431a1ad1877979013906731aae373de268"
877885
integrity sha512-jYP2LWwlh+FTqGd9v7ynUKZzjj98T8x7Yclz479QdRhHfuW9yQ+0jjnD31eXSXutmBpppj5PYNLYLRfnZJvcfg==

0 commit comments

Comments
 (0)