Merge pull request #1266 from uqmessias/feat/svg-uri-on-error

feat(svgUri): add onError prop to SvgUri/Xml/Ast
This commit is contained in:
Mikael Sand
2020-04-10 01:37:35 +03:00
committed by GitHub
2 changed files with 28 additions and 16 deletions
+7 -6
View File
@@ -466,29 +466,30 @@ export type JsxAST = {
children: (React$Node | string)[], children: (React$Node | string)[],
... ...
} & AST; } & AST;
export type AdditionalProps = {
onError?: (error: Error) => void,
override?: SvgProps,
} & SvgProps;
export type UriProps = { export type UriProps = {
uri: string | null, uri: string | null,
override?: SvgProps,
... ...
} & SvgProps; } & AdditionalProps;
export type UriState = { export type UriState = {
xml: string | null, xml: string | null,
... ...
}; };
export type XmlProps = { export type XmlProps = {
xml: string | null, xml: string | null,
override?: SvgProps,
... ...
} & SvgProps; } & AdditionalProps;
export type XmlState = { export type XmlState = {
ast: JsxAST | null, ast: JsxAST | null,
... ...
}; };
export type AstProps = { export type AstProps = {
ast: JsxAST | null, ast: JsxAST | null,
override?: SvgProps,
... ...
} & SvgProps; } & AdditionalProps;
export type Middleware = (ast: XmlAST) => XmlAST; export type Middleware = (ast: XmlAST) => XmlAST;
declare export function parse( declare export function parse(
source: string, source: string,
+21 -10
View File
@@ -81,13 +81,18 @@ export interface JsxAST extends AST {
children: (JSX.Element | string)[]; children: (JSX.Element | string)[];
} }
export type UriProps = { uri: string | null; override?: Object }; export type AdditionalProps = {
onError?: (error: Error) => void;
override?: Object;
};
export type UriProps = { uri: string | null } & AdditionalProps;
export type UriState = { xml: string | null }; export type UriState = { xml: string | null };
export type XmlProps = { xml: string | null; override?: Object }; export type XmlProps = { xml: string | null } & AdditionalProps;
export type XmlState = { ast: JsxAST | null }; export type XmlState = { ast: JsxAST | null };
export type AstProps = { ast: JsxAST | null; override?: Object }; export type AstProps = { ast: JsxAST | null } & AdditionalProps;
export function SvgAst({ ast, override }: AstProps) { export function SvgAst({ ast, override }: AstProps) {
if (!ast) { if (!ast) {
@@ -101,12 +106,20 @@ export function SvgAst({ ast, override }: AstProps) {
); );
} }
export const err = console.error.bind(console);
export function SvgXml(props: XmlProps) { export function SvgXml(props: XmlProps) {
const { xml, override } = props; const { onError = err, xml, override } = props;
const ast = useMemo<JsxAST | null>(() => (xml !== null ? parse(xml) : null), [ const ast = useMemo<JsxAST | null>(() => (xml !== null ? parse(xml) : null), [
xml, xml,
]); ]);
return <SvgAst ast={ast} override={override || props} />;
try {
return <SvgAst ast={ast} override={override || props} />;
} catch (error) {
onError(error);
return null;
}
} }
export async function fetchText(uri: string) { export async function fetchText(uri: string) {
@@ -114,18 +127,16 @@ export async function fetchText(uri: string) {
return await response.text(); return await response.text();
} }
export const err = console.error.bind(console);
export function SvgUri(props: UriProps) { export function SvgUri(props: UriProps) {
const { uri } = props; const { onError = err, uri } = props;
const [xml, setXml] = useState<string | null>(null); const [xml, setXml] = useState<string | null>(null);
useEffect(() => { useEffect(() => {
uri uri
? fetchText(uri) ? fetchText(uri)
.then(setXml) .then(setXml)
.catch(err) .catch(onError)
: setXml(null); : setXml(null);
}, [uri]); }, [onError, uri]);
return <SvgXml xml={xml} override={props} />; return <SvgXml xml={xml} override={props} />;
} }