Skip to content

Commit ef3257c

Browse files
committed
fix: relax input types
1 parent f471892 commit ef3257c

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed

src/index.d.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export interface CircleProps extends CommonPathProps {
219219
export const Circle: React.ComponentClass<CircleProps>;
220220

221221
export interface ClipPathProps {
222-
id: string;
222+
id?: string;
223223
}
224224
export const ClipPath: React.ComponentClass<ClipPathProps>;
225225

@@ -249,7 +249,7 @@ export interface ImageProps
249249
width?: NumberProp;
250250
height?: NumberProp;
251251
xlinkHref?: ReactNative.ImageProps['source'];
252-
href: ReactNative.ImageProps['source'];
252+
href?: ReactNative.ImageProps['source'];
253253
preserveAspectRatio?: string;
254254
opacity?: NumberProp;
255255
clipPath?: string;
@@ -271,18 +271,18 @@ export interface LinearGradientProps {
271271
y1?: NumberProp;
272272
y2?: NumberProp;
273273
gradientUnits?: Units;
274-
id: string;
274+
id?: string;
275275
}
276276
export const LinearGradient: React.ComponentClass<LinearGradientProps>;
277277

278278
export interface PathProps extends CommonPathProps {
279-
d: string;
279+
d?: string;
280280
opacity?: NumberProp;
281281
}
282282
export const Path: React.ComponentClass<PathProps>;
283283

284284
export interface PatternProps {
285-
id: string;
285+
id?: string;
286286
x?: NumberProp;
287287
y?: NumberProp;
288288
width?: NumberProp;
@@ -297,13 +297,13 @@ export const Pattern: React.ComponentClass<PatternProps>;
297297

298298
export interface PolygonProps extends CommonPathProps {
299299
opacity?: NumberProp;
300-
points: string | ReadonlyArray<NumberProp>;
300+
points?: string | ReadonlyArray<NumberProp>;
301301
}
302302
export const Polygon: React.ComponentClass<PolygonProps>;
303303

304304
export interface PolylineProps extends CommonPathProps {
305305
opacity?: NumberProp;
306-
points: string | ReadonlyArray<NumberProp>;
306+
points?: string | ReadonlyArray<NumberProp>;
307307
}
308308
export const Polyline: React.ComponentClass<PolylineProps>;
309309

@@ -316,7 +316,7 @@ export interface RadialGradientProps {
316316
cy?: NumberProp;
317317
r?: NumberProp;
318318
gradientUnits?: Units;
319-
id: string;
319+
id?: string;
320320
}
321321
export const RadialGradient: React.ComponentClass<RadialGradientProps>;
322322

@@ -352,7 +352,7 @@ export const Svg: React.ComponentClass<SvgProps>;
352352
export default Svg;
353353

354354
export interface SymbolProps {
355-
id: string;
355+
id?: string;
356356
viewBox?: string;
357357
preserveAspectRatio?: string;
358358
opacity?: NumberProp;
@@ -384,17 +384,17 @@ export const Text: React.ComponentClass<TextProps>;
384384

385385
export interface TextPathProps extends TextSpecificProps {
386386
xlinkHref?: string;
387-
href: string;
387+
href?: string;
388388
startOffset?: NumberProp;
389389
method?: TextPathMethod;
390390
spacing?: TextPathSpacing;
391-
midLine: TextPathMidLine;
391+
midLine?: TextPathMidLine;
392392
}
393393
export const TextPath: React.ComponentClass<TextPathProps>;
394394

395395
export interface UseProps extends CommonPathProps {
396396
xlinkHref?: string;
397-
href: string;
397+
href?: string;
398398
width?: NumberProp;
399399
height?: NumberProp;
400400
x?: NumberProp;
@@ -413,7 +413,7 @@ export type TMaskUnits =
413413
| EMaskUnits.OBJECT_BOUNDING_BOX;
414414

415415
export interface MaskProps extends CommonPathProps {
416-
id: string;
416+
id?: string;
417417
x?: NumberProp;
418418
y?: NumberProp;
419419
width?: NumberProp;

src/xml.tsx

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,25 @@ function missingTag() {
5858
return null;
5959
}
6060

61-
interface AST {
61+
export interface AST {
6262
tag: string;
6363
children: (AST | string)[] | (ReactElement | string)[];
6464
props: {};
6565
Tag: ComponentType;
6666
}
6767

68-
export function SvgAst({ ast, override }: { ast: AST; override?: Object }) {
68+
export type UriProps = { uri: string | null; override?: Object };
69+
export type UriState = { xml: string | null };
70+
71+
export type XmlProps = { xml: string | null; override?: Object };
72+
export type XmlState = { ast: AST | null };
73+
74+
export type AstProps = { ast: AST | null; override?: Object };
75+
76+
export function SvgAst({ ast, override }: AstProps) {
77+
if (!ast) {
78+
return null;
79+
}
6980
const { props, children } = ast;
7081
return (
7182
<Svg {...props} {...override}>
@@ -74,10 +85,12 @@ export function SvgAst({ ast, override }: { ast: AST; override?: Object }) {
7485
);
7586
}
7687

77-
export function SvgXml(props: { xml: string; override?: Object }) {
88+
export function SvgXml(props: XmlProps) {
7889
const { xml, override } = props;
79-
const ast = useMemo(() => xml && parse(xml), [xml]);
80-
return (ast && <SvgAst ast={ast} override={override || props} />) || null;
90+
const ast = useMemo<AST | null>(() => (xml !== null ? parse(xml) : null), [
91+
xml,
92+
]);
93+
return <SvgAst ast={ast} override={override || props} />;
8194
}
8295

8396
async function fetchText(uri: string) {
@@ -87,21 +100,23 @@ async function fetchText(uri: string) {
87100

88101
const err = console.error.bind(console);
89102

90-
export function SvgUri(props: { uri: string; override?: Object }) {
103+
export function SvgUri(props: UriProps) {
91104
const { uri } = props;
92-
const [xml, setXml] = useState();
105+
const [xml, setXml] = useState<string | null>(null);
93106
useEffect(() => {
94-
fetchText(uri)
95-
.then(setXml)
96-
.catch(err);
107+
uri
108+
? fetchText(uri)
109+
.then(setXml)
110+
.catch(err)
111+
: setXml(null);
97112
}, [uri]);
98-
return (xml && <SvgXml xml={xml} override={props} />) || null;
113+
return <SvgXml xml={xml} override={props} />;
99114
}
100115

101116
// Extending Component is required for Animated support.
102117

103-
export class SvgFromXml extends Component<{ xml: string; override?: Object }> {
104-
state: { ast?: AST } = {};
118+
export class SvgFromXml extends Component<XmlProps, XmlState> {
119+
state = { ast: null };
105120
componentDidMount() {
106121
this.parse(this.props.xml);
107122
}
@@ -111,9 +126,9 @@ export class SvgFromXml extends Component<{ xml: string; override?: Object }> {
111126
this.parse(xml);
112127
}
113128
}
114-
parse(xml: string) {
129+
parse(xml: string | null) {
115130
try {
116-
this.setState({ ast: parse(xml) });
131+
this.setState({ ast: xml ? parse(xml) : null });
117132
} catch (e) {
118133
console.error(e);
119134
}
@@ -123,24 +138,24 @@ export class SvgFromXml extends Component<{ xml: string; override?: Object }> {
123138
props,
124139
state: { ast },
125140
} = this;
126-
return ast ? <SvgAst ast={ast} override={props.override || props} /> : null;
141+
return <SvgAst ast={ast} override={props.override || props} />;
127142
}
128143
}
129144

130-
export class SvgFromUri extends Component<{ uri: string; override?: Object }> {
131-
state: { xml?: string } = {};
145+
export class SvgFromUri extends Component<UriProps, UriState> {
146+
state = { xml: null };
132147
componentDidMount() {
133148
this.fetch(this.props.uri);
134149
}
135-
componentDidUpdate(prevProps: { uri: string }) {
150+
componentDidUpdate(prevProps: { uri: string | null }) {
136151
const { uri } = this.props;
137152
if (uri !== prevProps.uri) {
138153
this.fetch(uri);
139154
}
140155
}
141-
async fetch(uri: string) {
156+
async fetch(uri: string | null) {
142157
try {
143-
this.setState({ xml: await fetchText(uri) });
158+
this.setState({ xml: uri ? await fetchText(uri) : null });
144159
} catch (e) {
145160
console.error(e);
146161
}
@@ -150,7 +165,7 @@ export class SvgFromUri extends Component<{ uri: string; override?: Object }> {
150165
props,
151166
state: { xml },
152167
} = this;
153-
return xml ? <SvgFromXml xml={xml} override={props} /> : null;
168+
return <SvgFromXml xml={xml} override={props} />;
154169
}
155170
}
156171

0 commit comments

Comments
 (0)