-
Notifications
You must be signed in to change notification settings - Fork 3k
/
BaseAutoCompleteSuggestions.js
85 lines (78 loc) · 3.53 KB
/
BaseAutoCompleteSuggestions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import React, {useEffect} from 'react';
import {Pressable} from 'react-native';
import Animated, {Easing, FadeOutDown, useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated';
// We take FlatList from this package to properly handle the scrolling of AutoCompleteSuggestions in chats since one scroll is nested inside another
import {FlatList} from 'react-native-gesture-handler';
import styles from '../../styles/styles';
import * as StyleUtils from '../../styles/StyleUtils';
import CONST from '../../CONST';
import {propTypes} from './autoCompleteSuggestionsPropTypes';
/**
* @param {Number} numRows
* @param {Boolean} isSuggestionPickerLarge
* @returns {Number}
*/
const measureHeightOfSuggestionRows = (numRows, isSuggestionPickerLarge) => {
if (isSuggestionPickerLarge) {
return numRows * CONST.AUTO_COMPLETE_SUGGESTER.ITEM_HEIGHT;
}
if (numRows > 2) {
// On small screens, we display a scrollable window with a height of 2.5 items, indicating that there are more items available beyond what is currently visible
return CONST.AUTO_COMPLETE_SUGGESTER.SMALL_CONTAINER_HEIGHT_FACTOR * CONST.AUTO_COMPLETE_SUGGESTER.ITEM_HEIGHT;
}
return numRows * CONST.AUTO_COMPLETE_SUGGESTER.ITEM_HEIGHT;
};
const BaseAutoCompleteSuggestions = (props) => {
const rowHeight = useSharedValue(0);
/**
* Render a suggestion menu item component.
* @param {Object} params
* @param {Object} params.item
* @param {Number} params.index
* @returns {JSX.Element}
*/
const renderSuggestionMenuItem = ({item, index}) => (
<Pressable
style={({hovered}) => StyleUtils.getAutoCompleteSuggestionItemStyle(props.highlightedSuggestionIndex, CONST.AUTO_COMPLETE_SUGGESTER.ITEM_HEIGHT, hovered, index)}
onMouseDown={(e) => e.preventDefault()}
onPress={() => props.onSelect(index)}
onLongPress={() => {}}
>
{props.renderSuggestionMenuItem(item, index)}
</Pressable>
);
const innerHeight = CONST.AUTO_COMPLETE_SUGGESTER.ITEM_HEIGHT * props.suggestions.length;
const animatedStyles = useAnimatedStyle(() => StyleUtils.getAutoCompleteSuggestionContainerStyle(rowHeight.value, props.shouldIncludeReportRecipientLocalTimeHeight));
useEffect(() => {
rowHeight.value = withTiming(measureHeightOfSuggestionRows(props.suggestions.length, props.isSuggestionPickerLarge), {
duration: 100,
easing: Easing.inOut(Easing.ease),
});
}, [props.suggestions.length, props.isSuggestionPickerLarge, rowHeight]);
return (
<Animated.View
ref={props.forwardedRef}
style={[styles.autoCompleteSuggestionsContainer, animatedStyles]}
exiting={FadeOutDown.duration(100).easing(Easing.inOut(Easing.ease))}
>
<FlatList
keyboardShouldPersistTaps="handled"
data={props.suggestions}
renderItem={renderSuggestionMenuItem}
keyExtractor={props.keyExtractor}
removeClippedSubviews={false}
showsVerticalScrollIndicator={innerHeight > rowHeight}
style={{flex: 1}}
/>
</Animated.View>
);
};
BaseAutoCompleteSuggestions.propTypes = propTypes;
BaseAutoCompleteSuggestions.displayName = 'BaseAutoCompleteSuggestions';
export default React.forwardRef((props, ref) => (
<BaseAutoCompleteSuggestions
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
forwardedRef={ref}
/>
));