-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add generic AutoCompleteSuggestions component #18024
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
82799bc
Add generic AutoCompleteSuggestions component
szebniok f798983
Fix lint errors
szebniok 5232332
Implement feedback
szebniok d24447c
Merge branch 'main' into rework-emoji-suggestions
szebniok e9c2420
Fix lint errors
szebniok 56bc6c4
Implement feedback
szebniok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/components/AutoCompleteSuggestions/BaseAutoCompleteSuggestions.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import React from 'react'; | ||
| import {View, Pressable} from 'react-native'; | ||
|
|
||
| // 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) => { | ||
| /** | ||
| * 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 rowHeight = measureHeightOfSuggestionRows( | ||
| props.suggestions.length, | ||
| props.isSuggestionPickerLarge, | ||
| ); | ||
|
|
||
| return ( | ||
| <View | ||
| ref={props.forwardedRef} | ||
| style={[ | ||
| styles.autoCompleteSuggestionsContainer, | ||
| StyleUtils.getAutoCompleteSuggestionContainerStyle( | ||
| rowHeight, | ||
| props.shouldIncludeReportRecipientLocalTimeHeight, | ||
| ), | ||
| ]} | ||
| > | ||
| <FlatList | ||
| keyboardShouldPersistTaps="handled" | ||
| data={props.suggestions} | ||
| renderItem={renderSuggestionMenuItem} | ||
| keyExtractor={props.keyExtractor} | ||
| removeClippedSubviews={false} | ||
| style={{height: rowHeight}} | ||
| /> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| BaseAutoCompleteSuggestions.propTypes = propTypes; | ||
| BaseAutoCompleteSuggestions.displayName = 'BaseAutoCompleteSuggestions'; | ||
|
|
||
| export default React.forwardRef((props, ref) => ( | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| <BaseAutoCompleteSuggestions {...props} forwardedRef={ref} /> | ||
| )); |
31 changes: 31 additions & 0 deletions
31
src/components/AutoCompleteSuggestions/autoCompleteSuggestionsPropTypes.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import PropTypes from 'prop-types'; | ||
|
|
||
| const propTypes = { | ||
| /** Array of suggestions */ | ||
| // eslint-disable-next-line react/forbid-prop-types | ||
| suggestions: PropTypes.arrayOf(PropTypes.object).isRequired, | ||
|
|
||
| /** Function used to render each suggestion, returned JSX will be enclosed inside a Pressable component */ | ||
| renderSuggestionMenuItem: PropTypes.func.isRequired, | ||
|
|
||
| /** Create unique keys for each suggestion item */ | ||
| keyExtractor: PropTypes.func.isRequired, | ||
|
|
||
| /** The index of the highlighted suggestion */ | ||
| highlightedSuggestionIndex: PropTypes.number.isRequired, | ||
|
|
||
| /** Fired when the user selects a suggestion */ | ||
| onSelect: PropTypes.func.isRequired, | ||
|
|
||
| /** Show that we can use large auto-complete suggestion picker. | ||
| * Depending on available space and whether the input is expanded, we can have a small or large mention suggester. | ||
| * When this value is false, the suggester will have a height of 2.5 items. When this value is true, the height can be up to 5 items. */ | ||
| isSuggestionPickerLarge: PropTypes.bool.isRequired, | ||
|
|
||
| /** Show that we should include ReportRecipientLocalTime view height */ | ||
| shouldIncludeReportRecipientLocalTimeHeight: PropTypes.bool.isRequired, | ||
| }; | ||
|
|
||
| const defaultProps = {}; | ||
|
|
||
| export {propTypes, defaultProps}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import React from 'react'; | ||
| import BaseAutoCompleteSuggestions from './BaseAutoCompleteSuggestions'; | ||
| import {propTypes} from './autoCompleteSuggestionsPropTypes'; | ||
|
|
||
| const AutoCompleteSuggestions = props => ( | ||
| // eslint-disable-next-line react/jsx-props-no-spreading | ||
| <BaseAutoCompleteSuggestions {...props} /> | ||
| ); | ||
|
|
||
| AutoCompleteSuggestions.propTypes = propTypes; | ||
| AutoCompleteSuggestions.displayName = 'AutoCompleteSuggestions'; | ||
|
|
||
| export default AutoCompleteSuggestions; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import React from 'react'; | ||
| import {View} from 'react-native'; | ||
| import PropTypes from 'prop-types'; | ||
| import _ from 'underscore'; | ||
| import styles from '../styles/styles'; | ||
| import * as StyleUtils from '../styles/StyleUtils'; | ||
| import * as EmojiUtils from '../libs/EmojiUtils'; | ||
| import Text from './Text'; | ||
| import getStyledTextArray from '../libs/GetStyledTextArray'; | ||
| import AutoCompleteSuggestions from | ||
| './AutoCompleteSuggestions'; | ||
|
|
||
| const propTypes = { | ||
| /** The index of the highlighted emoji */ | ||
| highlightedEmojiIndex: PropTypes.number, | ||
|
|
||
| /** Array of suggested emoji */ | ||
| emojis: PropTypes.arrayOf(PropTypes.shape({ | ||
| /** The emoji code */ | ||
| code: PropTypes.string.isRequired, | ||
|
|
||
| /** The name of the emoji */ | ||
| name: PropTypes.string.isRequired, | ||
|
|
||
| /** Array of different skin tone variants. | ||
| * If provided, it will be indexed with props.preferredSkinToneIndex */ | ||
| types: PropTypes.arrayOf(PropTypes.string.isRequired), | ||
| })).isRequired, | ||
|
|
||
| /** Fired when the user selects an emoji */ | ||
| onSelect: PropTypes.func.isRequired, | ||
|
|
||
| /** Emoji prefix that follows the colon */ | ||
| prefix: PropTypes.string.isRequired, | ||
|
|
||
| /** Show that we can use large emoji picker. Depending on available space | ||
| * and whether the input is expanded, we can have a small or large emoji | ||
| * suggester. When this value is false, the suggester will have a height of | ||
| * 2.5 items. When this value is true, the height can be up to 5 items. */ | ||
| isEmojiPickerLarge: PropTypes.bool.isRequired, | ||
|
|
||
| /** Show that we should include ReportRecipientLocalTime view height */ | ||
| shouldIncludeReportRecipientLocalTimeHeight: PropTypes.bool.isRequired, | ||
|
|
||
| /** Stores user's preferred skin tone */ | ||
| preferredSkinToneIndex: PropTypes.number.isRequired, | ||
| }; | ||
|
|
||
| const defaultProps = {highlightedEmojiIndex: 0}; | ||
|
|
||
| /** | ||
| * Create unique keys for each emoji item | ||
| * @param {Object} item | ||
| * @param {Number} index | ||
| * @returns {String} | ||
| */ | ||
| const keyExtractor = (item, index) => `${item.name}+${index}}`; | ||
|
|
||
| const EmojiSuggestions = (props) => { | ||
| /** | ||
| * Render an emoji suggestion menu item component. | ||
| * @param {Object} item | ||
| * @returns {JSX.Element} | ||
| */ | ||
| const renderSuggestionMenuItem = (item) => { | ||
| const styledTextArray = getStyledTextArray(item.name, props.prefix); | ||
|
|
||
| return ( | ||
| <View style={styles.autoCompleteSuggestionContainer}> | ||
| <Text style={styles.emojiSuggestionsEmoji}>{EmojiUtils.getEmojiCodeWithSkinColor(item, props.preferredSkinToneIndex)}</Text> | ||
| <Text style={styles.emojiSuggestionsText}> | ||
| : | ||
| {_.map(styledTextArray, ({text, isColored}, i) => ( | ||
| <Text key={`${text}+${i}`} style={StyleUtils.getColoredBackgroundStyle(isColored)}> | ||
| {text} | ||
| </Text> | ||
| ))} | ||
| : | ||
| </Text> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| return ( | ||
| <AutoCompleteSuggestions | ||
| suggestions={props.emojis} | ||
| renderSuggestionMenuItem={renderSuggestionMenuItem} | ||
| keyExtractor={keyExtractor} | ||
| highlightedSuggestionIndex={props.highlightedEmojiIndex} | ||
| onSelect={props.onSelect} | ||
| isSuggestionPickerLarge={props.isEmojiPickerLarge} | ||
| shouldIncludeReportRecipientLocalTimeHeight={props.shouldIncludeReportRecipientLocalTimeHeight} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| EmojiSuggestions.propTypes = propTypes; | ||
| EmojiSuggestions.defaultProps = defaultProps; | ||
| EmojiSuggestions.displayName = 'EmojiSuggestions'; | ||
|
|
||
| export default EmojiSuggestions; | ||
112 changes: 0 additions & 112 deletions
112
src/components/EmojiSuggestions/BaseEmojiSuggestions.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This existed before but not sure why index is used for key. Is there any possibility that texts are duplicated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that is likely scenario, as the
item.nameis splitted into at most into 3 parts, anditem.namecontains the:sign at the beginning and at the end.I could consider setting the
keytotext, but I could see an argument being made that the current solution is more future-proof.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like index is safer in general as a key.