1 |
|
2 | import isEqual from 'lodash/isEqual';
|
3 | import pick from 'lodash/pick';
|
4 | import React, { Component } from 'react';
|
5 | import PropTypes from 'prop-types';
|
6 | import { TabBarIOS } from './react-native';
|
7 |
|
8 | export default function createTabBarItemIOSComponent(
|
9 | IconNamePropType,
|
10 | getImageSource
|
11 | ) {
|
12 | return class TabBarItemIOS extends Component {
|
13 | static propTypes = {
|
14 | iconName: IconNamePropType.isRequired,
|
15 | selectedIconName: IconNamePropType,
|
16 | iconSize: PropTypes.number,
|
17 | iconColor: PropTypes.string,
|
18 | selectedIconColor: PropTypes.string,
|
19 | };
|
20 |
|
21 | static defaultProps = {
|
22 | iconSize: 30,
|
23 | };
|
24 |
|
25 | componentWillMount() {
|
26 | this.updateIconSources(this.props);
|
27 | }
|
28 |
|
29 | componentWillReceiveProps(nextProps) {
|
30 | const keys = Object.keys(TabBarItemIOS.propTypes);
|
31 | if (!isEqual(pick(nextProps, keys), pick(this.props, keys))) {
|
32 | this.updateIconSources(nextProps);
|
33 | }
|
34 | }
|
35 |
|
36 | updateIconSources(props) {
|
37 | if (props.iconName) {
|
38 | getImageSource(
|
39 | props.iconName,
|
40 | props.iconSize,
|
41 | props.iconColor
|
42 | ).then(icon => this.setState({ icon }));
|
43 | }
|
44 | if (props.selectedIconName || props.selectedIconColor) {
|
45 | const selectedIconName = props.selectedIconName || props.iconName;
|
46 | const selectedIconColor = props.selectedIconColor || props.iconColor;
|
47 | getImageSource(
|
48 | selectedIconName,
|
49 | props.iconSize,
|
50 | selectedIconColor
|
51 | ).then(selectedIcon => this.setState({ selectedIcon }));
|
52 | }
|
53 | }
|
54 |
|
55 | render() {
|
56 | return <TabBarIOS.Item {...this.props} {...this.state} />;
|
57 | }
|
58 | };
|
59 | }
|
60 |
|
\ | No newline at end of file |