Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migration from class to functional component #23911

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 77 additions & 71 deletions src/components/GrowlNotification/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, {Component} from 'react';
import {View, Animated} from 'react-native';
import React, {forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState} from 'react';
import {Directions, FlingGestureHandler, State} from 'react-native-gesture-handler';
import {View, Animated} from 'react-native';
import colors from '../../styles/colors';
import Text from '../Text';
import Icon from '../Icon';
Expand Down Expand Up @@ -30,23 +30,11 @@ const INACTIVE_POSITION_Y = -255;

const PressableWithoutFeedback = Pressables.PressableWithoutFeedback;

class GrowlNotification extends Component {
constructor(props) {
super(props);

this.state = {
bodyText: '',
type: 'success',
translateY: new Animated.Value(INACTIVE_POSITION_Y),
};

this.show = this.show.bind(this);
this.fling = this.fling.bind(this);
}

componentDidMount() {
Growl.setIsReady();
}
function GrowlNotification(_, ref) {
const translateY = useRef(new Animated.Value(INACTIVE_POSITION_Y)).current;
const [bodyText, setBodyText] = useState('');
const [type, setType] = useState('success');
const [duration, setDuration] = useState();

/**
* Show the growl notification
Expand All @@ -55,65 +43,83 @@ class GrowlNotification extends Component {
* @param {String} type
* @param {Number} duration
*/
show(bodyText, type, duration) {
this.setState(
{
bodyText,
type,
},
() => {
this.fling(0);
setTimeout(() => {
this.fling(INACTIVE_POSITION_Y);
}, duration);
},
);
}
const show = useCallback((text, growlType, growlDuration) => {
setBodyText(text);
mountiny marked this conversation as resolved.
Show resolved Hide resolved
setType(growlType);
setDuration(growlDuration);
}, []);

/**
* Animate growl notification
*
* @param {Number} val
*/
fling(val = INACTIVE_POSITION_Y) {
Animated.spring(this.state.translateY, {
toValue: val,
duration: 80,
useNativeDriver: true,
}).start();
}
const fling = useCallback(
(val = INACTIVE_POSITION_Y) => {
Animated.spring(translateY, {
toValue: val,
duration: 80,
useNativeDriver: true,
}).start();
},
[translateY],
);

render() {
return (
<FlingGestureHandler
direction={Directions.UP}
onHandlerStateChange={({nativeEvent}) => {
if (nativeEvent.state !== State.ACTIVE) {
return;
}
useImperativeHandle(
ref,
() => ({
show,
}),
[show],
);

this.fling(INACTIVE_POSITION_Y);
}}
>
<View style={styles.growlNotificationWrapper}>
<GrowlNotificationContainer translateY={this.state.translateY}>
<PressableWithoutFeedback
accessibilityLabel={this.state.bodyText}
onPress={() => this.fling(INACTIVE_POSITION_Y)}
>
<View style={styles.growlNotificationBox}>
<Icon
src={types[this.state.type].icon}
fill={types[this.state.type].iconColor}
/>
<Text style={styles.growlNotificationText}>{this.state.bodyText}</Text>
</View>
</PressableWithoutFeedback>
</GrowlNotificationContainer>
</View>
</FlingGestureHandler>
);
}
useEffect(() => {
Growl.setIsReady();
}, []);

useEffect(() => {
if (!duration) {
return;
}

fling(0);
setTimeout(() => {
fling();
setDuration(undefined);
}, duration);
}, [duration, fling]);

return (
<FlingGestureHandler
direction={Directions.UP}
onHandlerStateChange={({nativeEvent}) => {
if (nativeEvent.state !== State.ACTIVE) {
return;
}

fling();
}}
>
<View style={styles.growlNotificationWrapper}>
<GrowlNotificationContainer translateY={translateY}>
<PressableWithoutFeedback
accessibilityLabel={bodyText}
onPress={() => fling()}
>
mountiny marked this conversation as resolved.
Show resolved Hide resolved
<View style={styles.growlNotificationBox}>
<Icon
src={types[type].icon}
fill={types[type].iconColor}
/>
<Text style={styles.growlNotificationText}>{bodyText}</Text>
</View>
</PressableWithoutFeedback>
</GrowlNotificationContainer>
</View>
</FlingGestureHandler>
);
}

export default GrowlNotification;
GrowlNotification.displayName = 'GrowlNotification';

export default forwardRef(GrowlNotification);
Loading