forked from expo/vector-icons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createIconSet.js
61 lines (51 loc) · 1.46 KB
/
createIconSet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React from 'react';
import { Text } from 'react-native';
import { Font } from 'expo';
import createIconSet from './vendor/react-native-vector-icons/lib/create-icon-set';
import createIconButtonComponent from './vendor/react-native-vector-icons/lib/icon-button';
export default function(glyphMap, fontName, expoAssetId) {
const expoFontName = Font.style(fontName, {
ignoreWarning: true,
}).fontFamily;
const font = { [fontName]: expoAssetId };
const RNVIconComponent = createIconSet(glyphMap, expoFontName);
class Icon extends React.Component {
static propTypes = RNVIconComponent.propTypes;
static defaultProps = RNVIconComponent.defaultProps;
state = {
fontIsLoaded: Font.isLoaded(fontName),
};
async componentWillMount() {
this._mounted = true;
if (!this.state.fontIsLoaded) {
await Font.loadAsync(font);
this._mounted && this.setState({ fontIsLoaded: true });
}
}
componentWillUnmount() {
this._mounted = false;
}
setNativeProps(props) {
if (this._icon) {
this._icon.setNativeProps(props);
}
}
render() {
if (!this.state.fontIsLoaded) {
return <Text />;
}
return (
<RNVIconComponent
ref={view => {
this._icon = view;
}}
{...this.props}
/>
);
}
}
Icon.Button = createIconButtonComponent(Icon);
Icon.glyphMap = glyphMap;
Icon.font = font;
return Icon;
}