Skip to content

Commit

Permalink
fix: remove tsignore in LocalSvg and provide correct types (#1816)
Browse files Browse the repository at this point in the history
PR adding proper typings for asset prop of LocalSvg based on Image.resolveAssetSource() typings. Based on #1593 by @pratyushok.
  • Loading branch information
WoLewicki committed Jul 26, 2022
1 parent af68c93 commit c87f823
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/LocalSvg.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
import React, { useState, useEffect, Component } from 'react';
import { NativeModules, Platform } from 'react-native';
// @ts-ignore
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
import {
NativeModules,
Platform,
Image,
ImageSourcePropType,
} from 'react-native';

import { fetchText } from './xml';
import { SvgCss, SvgWithCss } from './css';
import { SvgProps } from './elements/Svg';

const { getRawResource } = NativeModules.RNSVGRenderableManager || {};

export function getUriFromSource(source?: string | number) {
const resolvedAssetSource = resolveAssetSource(source);
export function getUriFromSource(source: ImageSourcePropType) {
const resolvedAssetSource = Image.resolveAssetSource(source);
return resolvedAssetSource.uri;
}

export function loadLocalRawResourceDefault(source?: string | number) {
export function loadLocalRawResourceDefault(source: ImageSourcePropType) {
const uri = getUriFromSource(source);
return fetchText(uri);
}

export function isUriAnAndroidResourceIdentifier(uri?: string | number) {
export function isUriAnAndroidResourceIdentifier(uri?: string) {
return typeof uri === 'string' && uri.indexOf('/') <= -1;
}

export async function loadAndroidRawResource(uri?: string | number) {
export async function loadAndroidRawResource(uri: string) {
try {
return await getRawResource(uri);
} catch (e) {
Expand All @@ -35,7 +38,7 @@ export async function loadAndroidRawResource(uri?: string | number) {
}
}

export function loadLocalRawResourceAndroid(source?: string | number) {
export function loadLocalRawResourceAndroid(source: ImageSourcePropType) {
const uri = getUriFromSource(source);
if (isUriAnAndroidResourceIdentifier(uri)) {
return loadAndroidRawResource(uri);
Expand All @@ -50,7 +53,7 @@ export const loadLocalRawResource =
: loadLocalRawResourceAndroid;

export type LocalProps = SvgProps & {
asset?: string | number;
asset: ImageSourcePropType;
override?: Object;
};
export type LocalState = { xml: string | null };
Expand All @@ -69,13 +72,13 @@ export class WithLocalSvg extends Component<LocalProps, LocalState> {
componentDidMount() {
this.load(this.props.asset);
}
componentDidUpdate(prevProps: { asset?: string | number }) {
componentDidUpdate(prevProps: { asset: ImageSourcePropType }) {
const { asset } = this.props;
if (asset !== prevProps.asset) {
this.load(asset);
}
}
async load(asset?: string | number) {
async load(asset: ImageSourcePropType) {
try {
this.setState({ xml: asset ? await loadLocalRawResource(asset) : null });
} catch (e) {
Expand Down

0 comments on commit c87f823

Please sign in to comment.