|
1 | 1 | import * as React from 'react' |
2 | | -import { Platform, Text } from 'react-native' |
3 | | -import getGlyph from './glyph' |
| 2 | +import { StyleSheet, Text, Platform } from 'react-native' |
| 3 | +import tryGlyph from './glyph' |
4 | 4 |
|
5 | | -export default class Icon extends React.PureComponent { |
| 5 | +/** |
| 6 | + * (string, string, string) => string |
| 7 | + */ |
| 8 | +const getGlyph = Platform.select({ |
| 9 | + ios: (_android, ios, name) => tryGlyph([ios, name], 'ios'), |
| 10 | + default: (android, _ios, name) => tryGlyph([android, name], 'md') |
| 11 | +}) |
| 12 | + |
| 13 | +class Icon extends React.PureComponent { |
6 | 14 | constructor (props) { |
7 | 15 | super(props) |
8 | 16 |
|
9 | | - this.setRef = function (node) { |
10 | | - this._ref = node |
11 | | - }.bind(this) |
| 17 | + this._setRef = ref => { |
| 18 | + this._text = ref |
| 19 | + } |
12 | 20 | } |
13 | 21 |
|
14 | 22 | setNativeProps (props) { |
15 | | - if (this._ref) { |
16 | | - this._ref.setNativeProps(props) |
| 23 | + if (this._text) { |
| 24 | + this._text.setNativeProps(props) |
17 | 25 | } |
18 | 26 | } |
19 | 27 |
|
20 | 28 | render () { |
21 | | - const { name, android, ios, active, size, color, ...props } = this.props |
22 | | - |
23 | | - const glyph = getGlyph( |
24 | | - [Platform.select({ android, ios }), name], |
25 | | - Platform.OS, |
26 | | - active |
27 | | - ) |
| 29 | + const { |
| 30 | + name, |
| 31 | + android, |
| 32 | + ios, |
| 33 | + size, |
| 34 | + color, |
| 35 | + style, |
| 36 | + children, |
| 37 | + ...textProps |
| 38 | + } = this.props |
28 | 39 |
|
29 | | - const textStyle = { |
30 | | - fontFamily: 'Ionicons', |
31 | | - fontWeight: 'normal', |
32 | | - fontStyle: 'normal', |
| 40 | + const fontStyle = { |
33 | 41 | fontSize: size, |
34 | 42 | color |
35 | 43 | } |
36 | 44 |
|
37 | | - props.style = [textStyle, props.style] |
38 | | - props.ref = this.setRef |
| 45 | + const glyph = getGlyph(android, ios, name) |
39 | 46 |
|
40 | 47 | return ( |
41 | | - <Text {...props}> |
| 48 | + <Text |
| 49 | + {...textProps} |
| 50 | + style={[styles.default, fontStyle, style]} |
| 51 | + ref={this._setRef} |
| 52 | + > |
42 | 53 | {glyph} |
43 | | - {props.children} |
| 54 | + {children} |
44 | 55 | </Text> |
45 | 56 | ) |
46 | 57 | } |
47 | 58 | } |
48 | 59 |
|
| 60 | +export default Icon |
| 61 | + |
| 62 | +const styles = StyleSheet.create({ |
| 63 | + default: { |
| 64 | + fontFamily: 'Ionicons', |
| 65 | + fontWeight: 'normal', |
| 66 | + fontStyle: 'normal' |
| 67 | + } |
| 68 | +}) |
| 69 | + |
49 | 70 | Icon.defaultProps = { |
50 | | - active: false, |
51 | 71 | allowFontScaling: false, |
52 | 72 | size: 30 |
53 | 73 | } |
0 commit comments