Skip to content
This repository was archived by the owner on Dec 20, 2025. It is now read-only.

Commit 7ce15f8

Browse files
committed
Refactor code
1 parent 4805008 commit 7ce15f8

File tree

4 files changed

+47
-46
lines changed

4 files changed

+47
-46
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const IconBar = () => (
3838
| name | `IconName` | - | Name of the icon |
3939
| android | `IconName` | - | Name of the icon for Android devices |
4040
| ios | `IconName` | - | Name of the icon for iOS devices |
41-
| active | `?boolean` | false | Render filled icons, **iOS only** |
4241
| color | `?string` | - | Color of the icon |
4342
| size | `?number` | 30 | Size of the icon, namely `fontSize` |
4443
| ... | - | - | Other props of `Text` component |

glyph/index.js

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,12 @@ import glyphs from './map.json'
44
* Get glyph
55
*
66
* @param {Array.<?string>} iconNames
7-
* @param {string} currentOS
8-
* @param {boolean} [active]
7+
* @param {string} prefix
98
* @return {string}
109
*/
11-
export default function getGlyph (iconNames, currentOS, active) {
10+
export default function (iconNames, prefix) {
1211
const code = iconNames.reduce((prev, name) => {
13-
if (prev) return prev
14-
15-
if (name in glyphs) {
16-
return glyphs[name]
17-
}
18-
19-
const xs = []
20-
if (currentOS === 'ios') {
21-
xs.push('ios', name)
22-
if (!active) {
23-
xs.push('outline')
24-
}
25-
} else {
26-
xs.push('md', name)
27-
}
28-
29-
return glyphs[xs.join('-')]
12+
return prev || glyphs[name in glyphs ? name : `${prefix}-${name}`]
3013
}, undefined)
3114

3215
return code ? String.fromCharCode(code) : ''

index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export interface Icon {
44
ios?: string
55
size?: number
66
color?: string
7-
active?: boolean
87
children?: any
98
style?: any
109
}

index.js

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,73 @@
11
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'
44

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 {
614
constructor (props) {
715
super(props)
816

9-
this.setRef = function (node) {
10-
this._ref = node
11-
}.bind(this)
17+
this._setRef = ref => {
18+
this._text = ref
19+
}
1220
}
1321

1422
setNativeProps (props) {
15-
if (this._ref) {
16-
this._ref.setNativeProps(props)
23+
if (this._text) {
24+
this._text.setNativeProps(props)
1725
}
1826
}
1927

2028
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
2839

29-
const textStyle = {
30-
fontFamily: 'Ionicons',
31-
fontWeight: 'normal',
32-
fontStyle: 'normal',
40+
const fontStyle = {
3341
fontSize: size,
3442
color
3543
}
3644

37-
props.style = [textStyle, props.style]
38-
props.ref = this.setRef
45+
const glyph = getGlyph(android, ios, name)
3946

4047
return (
41-
<Text {...props}>
48+
<Text
49+
{...textProps}
50+
style={[styles.default, fontStyle, style]}
51+
ref={this._setRef}
52+
>
4253
{glyph}
43-
{props.children}
54+
{children}
4455
</Text>
4556
)
4657
}
4758
}
4859

60+
export default Icon
61+
62+
const styles = StyleSheet.create({
63+
default: {
64+
fontFamily: 'Ionicons',
65+
fontWeight: 'normal',
66+
fontStyle: 'normal'
67+
}
68+
})
69+
4970
Icon.defaultProps = {
50-
active: false,
5171
allowFontScaling: false,
5272
size: 30
5373
}

0 commit comments

Comments
 (0)