Browse Source

Change icon based on focused state

Arnaud Vergnet 4 years ago
parent
commit
3a570bd993
1 changed files with 15 additions and 3 deletions
  1. 15
    3
      src/components/Tabbar/TabHomeIcon.js

+ 15
- 3
src/components/Tabbar/TabHomeIcon.js View File

@@ -1,7 +1,7 @@
1 1
 // @flow
2 2
 
3 3
 import * as React from 'react';
4
-import {View} from "react-native";
4
+import {Image, View} from "react-native";
5 5
 import {FAB, withTheme} from 'react-native-paper';
6 6
 import * as Animatable from "react-native-animatable";
7 7
 
@@ -20,7 +20,7 @@ const AnimatedFAB = Animatable.createAnimatableComponent(FAB);
20 20
 class TabHomeIcon extends React.Component<Props> {
21 21
 
22 22
     focusedIcon = require('../../../assets/tab-icon.png');
23
-    unFocusedIcon = require('../../../assets/tab-icon-outline.png'); // has a weird rotating on icon change
23
+    unFocusedIcon = require('../../../assets/tab-icon-outline.png');
24 24
 
25 25
     constructor(props) {
26 26
         super(props);
@@ -47,12 +47,24 @@ class TabHomeIcon extends React.Component<Props> {
47 47
         });
48 48
     }
49 49
 
50
+    iconRender = ({size, color}) =>
51
+        this.props.focused
52
+            ? <Image
53
+                source={this.focusedIcon}
54
+                style={{width: size, height: size, tintColor: color}}
55
+            />
56
+            : <Image
57
+                source={this.unFocusedIcon}
58
+                style={{width: size, height: size, tintColor: color}}
59
+            />;
60
+
50 61
     shouldComponentUpdate(nextProps: Props): boolean {
51 62
         return (nextProps.focused !== this.props.focused);
52 63
     }
53 64
 
54 65
     render(): React$Node {
55 66
         const props = this.props;
67
+        console.log(props.focused)
56 68
         return (
57 69
             <View style={{
58 70
                 flex: 1,
@@ -65,7 +77,7 @@ class TabHomeIcon extends React.Component<Props> {
65 77
                     useNativeDriver
66 78
                     onPress={props.onPress}
67 79
                     onLongPress={props.onLongPress}
68
-                    icon={this.focusedIcon}
80
+                    icon={this.iconRender}
69 81
                     style={{
70 82
                         marginLeft: 'auto',
71 83
                         marginRight: 'auto'

Loading…
Cancel
Save