Skip to content

Commit

Permalink
animate opacity
Browse files Browse the repository at this point in the history
  • Loading branch information
wan committed Aug 28, 2023
1 parent 59505a4 commit 2984d03
Showing 1 changed file with 50 additions and 19 deletions.
69 changes: 50 additions & 19 deletions src/SelectDropdown.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {forwardRef, useImperativeHandle} from 'react';
import React, {useRef, useState, forwardRef, useImperativeHandle} from 'react';
import {View, Text, TouchableOpacity, FlatList} from 'react-native';
import styles from './styles';
import {isExist} from './helpers/isExist';
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

0 comments on commit 2984d03

Please sign in to comment.