Skip to content
Open
Show file tree
Hide file tree
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
42 changes: 37 additions & 5 deletions src/components/Dropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
View,
ViewStyle,
StatusBar,
Animated,
} from 'react-native';
import { useDetectDevice } from '../../toolkits';
import { useDeviceOrientation } from '../../useDeviceOrientation';
Expand Down Expand Up @@ -98,6 +99,7 @@ const DropdownComponent: <T>(
closeModalWhenSelectedItem = true,
excludeItems = [],
excludeSearchItems = [],
animationDuration = 300,
} = props;

const ref = useRef<View>(null);
Expand All @@ -108,6 +110,7 @@ const DropdownComponent: <T>(
const [position, setPosition] = useState<any>();
const [keyboardHeight, setKeyboardHeight] = useState<number>(0);
const [searchText, setSearchText] = useState('');
const [modalAnimatedHeight] = useState(new Animated.Value(0));

const { width: W, height: H } = Dimensions.get('window');
const styleContainerVertical: ViewStyle = useMemo(() => {
Expand Down Expand Up @@ -164,6 +167,11 @@ const DropdownComponent: <T>(
if (!disable) {
_measure();
setVisible(true);
Animated.timing(modalAnimatedHeight, {
toValue: 1,
duration: animationDuration,
useNativeDriver: false,
}).start();
if (onFocus) {
onFocus();
}
Expand All @@ -177,12 +185,18 @@ const DropdownComponent: <T>(

const eventClose = useCallback(() => {
if (!disable) {
setVisible(false);
Animated.timing(modalAnimatedHeight, {
toValue: 0,
duration: animationDuration,
useNativeDriver: false,
}).start(() => {
setVisible(false);
});
if (onBlur) {
onBlur();
}
}
}, [disable, onBlur]);
}, [disable, modalAnimatedHeight, animationDuration, onBlur]);

const font = useCallback(() => {
if (fontFamily) {
Expand Down Expand Up @@ -320,18 +334,30 @@ const DropdownComponent: <T>(
}

_measure();
setVisible(visibleStatus);

if (data) {
const filterData = excludeData(data);
setListData(filterData);
}

if (visibleStatus) {
setVisible(true);
Animated.timing(modalAnimatedHeight, {
toValue: 1,
duration: animationDuration,
useNativeDriver: false,
}).start();
if (onFocus) {
onFocus();
}
} else {
Animated.timing(modalAnimatedHeight, {
toValue: 0,
duration: animationDuration,
useNativeDriver: false,
}).start(() => {
setVisible(false);
});
if (onBlur) {
onBlur();
}
Expand All @@ -347,6 +373,7 @@ const DropdownComponent: <T>(
disable,
keyboardHeight,
visible,
modalAnimatedHeight,
_measure,
data,
searchText,
Expand Down Expand Up @@ -701,18 +728,22 @@ const DropdownComponent: <T>(
isFull && styles.fullScreen,
])}
>
<View
<Animated.View
style={StyleSheet.flatten([
styles.container,
isFull ? styleHorizontal : styleVertical,
{
width,
height: modalAnimatedHeight.interpolate({
inputRange: [0, 1],
outputRange: [0, maxHeight],
}),
},
containerStyle,
])}
>
{_renderList(isTopPosition)}
</View>
</Animated.View>
</View>
</View>
</TouchableWithoutFeedback>
Expand All @@ -727,6 +758,7 @@ const DropdownComponent: <T>(
search,
position,
keyboardHeight,
modalAnimatedHeight,
maxHeight,
minHeight,
dropdownPosition,
Expand Down
1 change: 1 addition & 0 deletions src/components/Dropdown/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface DropdownProps<T> {
closeModalWhenSelectedItem?: boolean;
excludeItems?: T[];
excludeSearchItems?: T[];
animationDuration?: number;
onChange: (item: T) => void;
renderLeftIcon?: (visible?: boolean) => JSX.Element | null | undefined;
renderRightIcon?: (visible?: boolean) => JSX.Element | null | undefined;
Expand Down