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

animate opacity #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
71 changes: 51 additions & 20 deletions src/SelectDropdown.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, {forwardRef, useImperativeHandle} from 'react';
import {View, Text, TouchableOpacity, FlatList} from 'react-native';
import React, {useRef, useEffect, useState, forwardRef, useImperativeHandle} from 'react';
import {Animated, View, Text, TouchableOpacity, FlatList} from 'react-native';
import styles from './styles';
import {isExist} from './helpers/isExist';
import {mergeStyles} from './helpers/mergeStyles';
Expand Down Expand Up @@ -59,6 +59,7 @@ const SelectDropdown = (
) => {
const disabledInternalSearch = !!onChangeSearchInputText;
/* ******************* hooks ******************* */
const fadeAnim = useRef(new Animated.Value(0)).current;
const {dropdownButtonRef, dropDownFlatlistRef} = useRefs();
const {
dataArr, //
Expand Down Expand Up @@ -92,6 +93,34 @@ const SelectDropdown = (
selectItem(index);
},
}));

const [initClose, setInitClose] = useState(false);

const fadeInOut = value => {
Animated.timing(fadeAnim, {
toValue: value,
duration: 300,
useNativeDriver: true,
}).start(() => {
if (initClose) {
setIsVisible(false);
setInitClose(false);
}
});
};

useEffect(() => {
if (isVisible) {
fadeInOut(1);
}
}, [isVisible]);

useEffect(() => {
if (initClose) {
fadeInOut(0);
}
}, [initClose]);

/* ******************* Methods ******************* */
const openDropdown = () => {
dropdownButtonRef.current.measure((fx, fy, w, h, px, py) => {
Expand All @@ -101,7 +130,7 @@ const SelectDropdown = (
});
};
const closeDropdown = () => {
setIsVisible(false);
setInitClose(true);
setSearchTxt('');
onBlur && onBlur();
};
Expand Down Expand Up @@ -172,23 +201,25 @@ const SelectDropdown = (
return (
isVisible && (
<DropdownModal statusBarTranslucent={statusBarTranslucent} visible={isVisible} onRequestClose={onRequestClose}>
<DropdownOverlay onPress={closeDropdown} backgroundColor={dropdownOverlayColor} />
<DropdownWindow layoutStyle={dropdownWindowStyle}>
<FlatList
data={dataArr}
keyExtractor={(item, index) => index.toString()}
ref={dropDownFlatlistRef}
renderItem={renderFlatlistItem}
getItemLayout={getItemLayout}
onLayout={onLayout}
ListHeaderComponent={renderSearchView()}
stickyHeaderIndices={search && [0]}
keyboardShouldPersistTaps="always"
onEndReached={() => onScrollEndReached && onScrollEndReached()}
onEndReachedThreshold={0.5}
showsVerticalScrollIndicator={showsVerticalScrollIndicator}
/>
</DropdownWindow>
<Animated.View style={{opacity: fadeAnim}}>
<DropdownOverlay onPress={closeDropdown} backgroundColor={dropdownOverlayColor} />
<DropdownWindow layoutStyle={dropdownWindowStyle}>
<FlatList
data={dataArr}
keyExtractor={(item, index) => index.toString()}
ref={dropDownFlatlistRef}
renderItem={renderFlatlistItem}
getItemLayout={getItemLayout}
onLayout={onLayout}
ListHeaderComponent={renderSearchView()}
stickyHeaderIndices={search && [0]}
keyboardShouldPersistTaps="always"
onEndReached={() => onScrollEndReached && onScrollEndReached()}
onEndReachedThreshold={0.5}
showsVerticalScrollIndicator={showsVerticalScrollIndicator}
/>
</DropdownWindow>
</Animated.View>
</DropdownModal>
)
);
Expand Down