Skip to content

Commit 39a94c0

Browse files
authored
FloatingButton - Remove UNSAFE method (#916)
* Turning component to a fully controlled component by removing state management. * change variable name * Fix for hiding button with 'withoutAnimation' flag
1 parent 89d7d7d commit 39a94c0

File tree

2 files changed

+18
-44
lines changed

2 files changed

+18
-44
lines changed

demo/src/screens/componentScreens/FloatingButtonScreen.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default class FloatingButtonScreen extends Component {
1313
super(props);
1414

1515
this.state = {
16-
showButton: false
16+
showButton: true
1717
};
1818
}
1919

@@ -107,6 +107,7 @@ export default class FloatingButtonScreen extends Component {
107107
}}
108108
// bottomMargin={80}
109109
// hideBackgroundOverlay
110+
// withoutAnimation
110111
/>
111112
</View>
112113
);

src/components/floatingButton/index.tsx

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ export interface FloatingButtonProps {
3939
hideBackgroundOverlay?: boolean;
4040
}
4141

42-
interface State {
43-
shouldAnimateHide?: boolean;
44-
isVisible?: boolean;
45-
animating?: boolean;
46-
}
47-
4842
const SHOW_ANIMATION_DELAY = 350;
4943
const SHOW_ANIMATION_DURATION = 180;
5044
const HIDE_ANIMATION_DURATION = 150;
@@ -57,44 +51,21 @@ const gradientImage = () => require('./gradient.png');
5751
* @extends: Button
5852
* @extendsLink: https://github.com/wix/react-native-ui-lib/blob/master/src/components/button/index.js
5953
*/
60-
class FloatingButton extends PureComponent<FloatingButtonProps, State> {
54+
class FloatingButton extends PureComponent<FloatingButtonProps> {
6155
static displayName = 'FloatingButton';
6256

57+
initialVisibility?: boolean;
58+
firstLoad: boolean;
59+
6360
constructor(props: FloatingButtonProps) {
6461
super(props);
6562

66-
this.state = {
67-
shouldAnimateHide: false,
68-
isVisible: props.visible,
69-
animating: false
70-
};
63+
this.initialVisibility = props.visible;
64+
this.firstLoad = true;
7165
}
7266

73-
UNSAFE_componentWillReceiveProps(nextProps: FloatingButtonProps) {
74-
const {withoutAnimation} = this.props;
75-
const propsVisible = this.props.visible;
76-
const nextVisible = nextProps.visible;
77-
78-
if (!withoutAnimation) {
79-
const shouldStartAnimation =
80-
!this.state.isVisible && nextVisible && !this.state.animating;
81-
if (shouldStartAnimation) {
82-
this.setState({animating: true});
83-
84-
if (nextProps.duration) {
85-
setTimeout(() => {
86-
this.setState({isVisible: false, shouldAnimateHide: true});
87-
}, nextProps.duration);
88-
}
89-
}
90-
}
91-
92-
this.setState({
93-
shouldAnimateHide: withoutAnimation
94-
? false
95-
: !nextVisible && propsVisible,
96-
isVisible: nextVisible
97-
});
67+
componentDidMount() {
68+
this.firstLoad = false;
9869
}
9970

10071
onAnimationEnd = () => {
@@ -143,22 +114,24 @@ class FloatingButton extends PureComponent<FloatingButtonProps, State> {
143114
}
144115

145116
render() {
146-
const {withoutAnimation, secondaryButton} = this.props;
147-
const {isVisible, shouldAnimateHide} = this.state;
117+
const {withoutAnimation, secondaryButton, visible} = this.props;
148118
const Container = !withoutAnimation ? AnimatableView : View;
149119

150-
// NOTE: Don't show if it should not be visible and it was already animated
151-
if (!isVisible && !shouldAnimateHide) {
120+
// NOTE: On first load, don't show if it should not be visible
121+
if (this.firstLoad === true && !this.initialVisibility) {
122+
return false;
123+
}
124+
if (!visible && withoutAnimation) {
152125
return false;
153126
}
154127

155128
return (
156129
<Container
157130
pointerEvents="box-none"
158131
style={[styles.animatedContainer, Constants.isAndroid && {zIndex: 99}]}
159-
animation={!isVisible ? 'fadeOutDown' : 'fadeInUp'}
132+
animation={!visible ? 'fadeOutDown' : 'fadeInUp'}
160133
duration={SHOW_ANIMATION_DURATION}
161-
delay={!isVisible ? HIDE_ANIMATION_DURATION : SHOW_ANIMATION_DELAY}
134+
delay={!visible ? HIDE_ANIMATION_DURATION : SHOW_ANIMATION_DELAY}
162135
easing={'ease-out'}
163136
onAnimationEnd={this.onAnimationEnd}
164137
>

0 commit comments

Comments
 (0)