fix(mobile): svg circle support (#2874)
This commit is contained in:
@@ -13,7 +13,7 @@ interface SvgNode {
|
||||
children?: SvgNode[]
|
||||
}
|
||||
|
||||
const generatePathElement = (node: SvgNode): string => {
|
||||
const generateElement = (node: SvgNode): string => {
|
||||
const props = Object.entries(node.properties)
|
||||
.map(([key, value]) => {
|
||||
const camelKey = key.replaceAll(/-([a-z])/g, (_match, p1: string) => p1.toUpperCase())
|
||||
@@ -33,7 +33,9 @@ const generatePathElement = (node: SvgNode): string => {
|
||||
})
|
||||
.join(" ")
|
||||
|
||||
return ` <Path ${props} />`
|
||||
const tagName = `${node.tagName.charAt(0).toUpperCase()}${node.tagName.slice(1)}`
|
||||
|
||||
return ` <${tagName} ${props} />`
|
||||
}
|
||||
|
||||
const isPureBackgroundPath = (node: any) => {
|
||||
@@ -45,6 +47,8 @@ const isPureBackgroundPath = (node: any) => {
|
||||
)
|
||||
}
|
||||
|
||||
const supportedTags = new Set(["path", "circle"])
|
||||
|
||||
const convertSvgToRN = (svgContent: string, componentName: string) => {
|
||||
const ast = parse(svgContent)
|
||||
const svgNode = ast.children[0] as SvgNode
|
||||
@@ -52,13 +56,13 @@ const convertSvgToRN = (svgContent: string, componentName: string) => {
|
||||
|
||||
const pathElements =
|
||||
svgNode.children
|
||||
?.filter((child) => child.tagName === "path")
|
||||
?.filter((child) => supportedTags.has(child.tagName))
|
||||
.filter((child) => !isPureBackgroundPath(child))
|
||||
.map((node) => generatePathElement(node))
|
||||
.map((node) => generateElement(node))
|
||||
.join("\n") ?? ""
|
||||
|
||||
return `import * as React from "react"
|
||||
import Svg, { Path } from "react-native-svg"
|
||||
import Svg, { Circle, Path } from "react-native-svg"
|
||||
|
||||
interface ${componentName}Props {
|
||||
width?: number
|
||||
|
||||
Reference in New Issue
Block a user