Skip to content

Commit 7b05a57

Browse files
authored
Merge branch 'master' into master
2 parents e63e820 + 19f443a commit 7b05a57

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ react-native link react-native-svg # not react-native-svg-uri !!!
2727
| `source` | `ImageSource` | | Same kind of `source` prop that `<Image />` component has
2828
| `svgXmlData` | `String` | | You can pass the SVG as String directly
2929
| `fill` | `Color` | | Overrides all fill attributes of the svg file
30+
| `fillAll` | `Boolean` | Adds the fill color to the entire svg object
3031

3132
## Known Bugs
3233

index.d.ts

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ interface SvgUriProps {
3434
* Fill color for the svg object
3535
*/
3636
fill?: string
37+
38+
/**
39+
* Invoked when load completes successfully.
40+
*/
41+
onLoad?: Function
42+
43+
/**
44+
* Fill the entire svg element with same color
45+
*/
46+
fillAll?: boolean
3747
}
3848

3949
export default class SvgUri extends Component<SvgUriProps, {}> { }

index.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,22 @@ class SvgUri extends Component{
125125
this.isComponentMounted = false
126126
}
127127

128-
async fetchSVGData(uri){
129-
let responseXML = null;
128+
async fetchSVGData(uri) {
129+
let responseXML = null, error = null;
130130
try {
131131
const response = await fetch(uri);
132132
responseXML = await response.text();
133133
} catch(e) {
134+
error = e;
134135
console.error("ERROR SVG", e);
135136
} finally {
136137
if (this.isComponentMounted) {
137-
this.setState({svgXmlData:responseXML});
138+
this.setState({ svgXmlData: responseXML }, () => {
139+
const { onLoad } = this.props;
140+
if (onLoad && !error) {
141+
onLoad();
142+
}
143+
});
138144
}
139145
}
140146

@@ -144,8 +150,8 @@ class SvgUri extends Component{
144150
// Remove empty strings from children array
145151
trimElementChilden(children) {
146152
for (child of children) {
147-
if (typeof child === 'string') {
148-
if (child.trim().length === 0)
153+
if (typeof child === 'string') {
154+
if (child.trim().length === 0)
149155
children.splice(children.indexOf(child), 1);
150156
}
151157
}
@@ -217,6 +223,11 @@ class SvgUri extends Component{
217223

218224
obtainComponentAtts({attributes}, enabledAttributes) {
219225
const styleAtts = {};
226+
227+
if (this.state.fill && this.props.fillAll) {
228+
styleAtts.fill = this.state.fill;
229+
}
230+
220231
Array.from(attributes).forEach(({nodeName, nodeValue}) => {
221232
Object.assign(styleAtts, utils.transformStyle({
222233
nodeName,
@@ -241,7 +252,7 @@ class SvgUri extends Component{
241252
inspectNode(node){
242253
// Only process accepted elements
243254
if (!ACCEPTED_SVG_ELEMENTS.includes(node.nodeName)) {
244-
return null;
255+
return (<View />);
245256
}
246257

247258
// Process the xml node
@@ -275,7 +286,7 @@ class SvgUri extends Component{
275286
const inputSVG = this.state.svgXmlData.substring(
276287
this.state.svgXmlData.indexOf("<svg "),
277288
(this.state.svgXmlData.indexOf("</svg>") + 6)
278-
);
289+
).replace(/<!-(.*?)->/g, '');
279290

280291
const doc = new xmldom.DOMParser().parseFromString(inputSVG);
281292

@@ -300,6 +311,8 @@ SvgUri.propTypes = {
300311
svgXmlData: PropTypes.string,
301312
source: PropTypes.any,
302313
fill: PropTypes.string,
314+
onLoad: PropTypes.func,
315+
fillAll: PropTypes.bool
303316
}
304317

305318
module.exports = SvgUri;

0 commit comments

Comments
 (0)