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

Suggestion box for edit input #44720

Merged
merged 37 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
82b38df
Merge branch 'main' of https://github.com/Expensify/App
perunt Jun 21, 2024
79713ed
Merge branch 'main' of https://github.com/Expensify/App
perunt Jun 26, 2024
3d2339a
Merge branch 'main' of https://github.com/Expensify/App
perunt Jun 26, 2024
6a4d1f2
update const
perunt Jun 28, 2024
8987fd1
fix positioning
perunt Jun 28, 2024
cbb8243
types
perunt Jun 28, 2024
6f9dd07
Merge branch 'main' of https://github.com/Expensify/App into perunt/s…
perunt Jul 1, 2024
5fc92bf
update SUGGESTION_BOX_MAX_SAFE_DISTANCE
perunt Jul 1, 2024
9026d4b
add runAfterInteractions
perunt Jul 1, 2024
b050267
update ReportActionItemMessageEdit
perunt Jul 1, 2024
d43e776
Merge branch 'main' of https://github.com/Expensify/App into perunt/s…
perunt Jul 2, 2024
410fefc
AutoCompleteSuggestions update
perunt Jul 2, 2024
376bbb1
change SUGGESTION_BOX_MAX_SAFE_DISTANCE
perunt Jul 2, 2024
af3beca
add getBottomSuggestionPadding
perunt Jul 2, 2024
fa0be2f
calculateEmojiSuggestion
perunt Jul 2, 2024
35448f3
checkIfSuggestionVisible
perunt Jul 2, 2024
b8a42a8
Merge branch 'main' of https://github.com/Expensify/App into perunt/s…
perunt Jul 2, 2024
be606bc
fix wrong positioning when focus input
perunt Jul 3, 2024
eec0d6f
fix shortcuts and focus issue
perunt Jul 3, 2024
e59287c
Merge branch 'main' of https://github.com/Expensify/App into perunt/s…
perunt Jul 3, 2024
2ed0d9a
checkIfSuggestionVisible
perunt Jul 3, 2024
9e8d8e0
lint
perunt Jul 3, 2024
d04dec3
fix 'enter' from keyboard on suggestion
perunt Jul 3, 2024
354f77d
type
perunt Jul 3, 2024
b72bf52
type
perunt Jul 3, 2024
507d8ac
clean
perunt Jul 5, 2024
ba105c2
remove const
perunt Jul 5, 2024
1c390e5
fix selection
perunt Jul 9, 2024
e2d662b
remove eslint-disable
perunt Jul 9, 2024
bc2fd14
run tag on UI
perunt Jul 9, 2024
2a73da2
fix react-compiler/react-compiler
perunt Jul 9, 2024
9bcb5db
Merge branch 'main' of https://github.com/Expensify/App into perunt/s…
perunt Jul 9, 2024
8a66410
Merge branch 'main' of https://github.com/Expensify/App into perunt/s…
perunt Jul 9, 2024
eba8f47
Merge branch 'main' of https://github.com/Expensify/App into perunt/s…
perunt Jul 9, 2024
f7d8319
eslint-disable-next-line react-compiler/react-compiler,
perunt Jul 9, 2024
049d487
one more react-compiler/react-compiler
perunt Jul 9, 2024
c213eec
naming
perunt Jul 11, 2024
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
2 changes: 1 addition & 1 deletion src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ const CONST = {
MAX_AMOUNT_OF_SUGGESTIONS: 20,
MAX_AMOUNT_OF_VISIBLE_SUGGESTIONS_IN_CONTAINER: 5,
HERE_TEXT: '@here',
SUGGESTION_BOX_MAX_SAFE_DISTANCE: 38,
SUGGESTION_BOX_MAX_SAFE_DISTANCE: 10,
BIG_SCREEN_SUGGESTION_WIDTH: 300,
},
COMPOSER_MAX_HEIGHT: 125,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function getBottomSuggestionPadding(): number {
return 0;
return 6;
}

export default getBottomSuggestionPadding;
51 changes: 30 additions & 21 deletions src/components/AutoCompleteSuggestions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ const measureHeightOfSuggestionRows = (numRows: number, canBeBig: boolean): numb
}
return numRows * CONST.AUTO_COMPLETE_SUGGESTER.SUGGESTION_ROW_HEIGHT;
};
function isSuggestionRenderedAbove(isEnoughSpaceAboveForBig: boolean, isEnoughSpaceAboveForSmall: boolean): boolean {
return isEnoughSpaceAboveForBig || isEnoughSpaceAboveForSmall;
function isSuggestionMenuRenderedAbove(isEnoughSpaceAboveForBigMenu: boolean, isEnoughSpaceAboveForSmallMenu: boolean): boolean {
return isEnoughSpaceAboveForBigMenu || isEnoughSpaceAboveForSmallMenu;
}

type IsEnoughSpaceToRenderMenuAboveCursor = Pick<MeasureParentContainerAndCursor, 'y' | 'cursorCoordinates' | 'scrollValue'> & {
contentHeight: number;
topInset: number;
};
function isEnoughSpaceToRenderMenuAboveCursor({y, cursorCoordinates, scrollValue, contentHeight, topInset}: IsEnoughSpaceToRenderMenuAboveCursor): boolean {
return y + (cursorCoordinates.y - scrollValue) > contentHeight + topInset + CONST.AUTO_COMPLETE_SUGGESTER.SUGGESTION_BOX_MAX_SAFE_DISTANCE;
}

/**
Expand All @@ -35,7 +43,7 @@ function isSuggestionRenderedAbove(isEnoughSpaceAboveForBig: boolean, isEnoughSp
function AutoCompleteSuggestions<TSuggestion>({measureParentContainerAndReportCursor = () => {}, ...props}: AutoCompleteSuggestionsProps<TSuggestion>) {
const containerRef = React.useRef<HTMLDivElement>(null);
const isInitialRender = React.useRef<boolean>(true);
const isSuggestionAboveRef = React.useRef<boolean>(false);
const isSuggestionMenuAboveRef = React.useRef<boolean>(false);
const leftValue = React.useRef<number>(0);
const prevLeftValue = React.useRef<number>(0);
const {windowHeight, windowWidth, isSmallScreenWidth} = useWindowDimensions();
Expand All @@ -44,11 +52,12 @@ function AutoCompleteSuggestions<TSuggestion>({measureParentContainerAndReportCu
width: 0,
left: 0,
bottom: 0,
cursorCoordinates: {x: 0, y: 0},
});
const StyleUtils = useStyleUtils();
const insets = useSafeAreaInsets();
const {keyboardHeight} = useKeyboardState();
const {paddingBottom: bottomInset} = StyleUtils.getSafeAreaPadding(insets ?? undefined);
const {paddingBottom: bottomInset, paddingTop: topInset} = StyleUtils.getSafeAreaPadding(insets ?? undefined);

useEffect(() => {
const container = containerRef.current;
Expand All @@ -73,51 +82,51 @@ function AutoCompleteSuggestions<TSuggestion>({measureParentContainerAndReportCu

measureParentContainerAndReportCursor(({x, y, width, scrollValue, cursorCoordinates}: MeasureParentContainerAndCursor) => {
const xCoordinatesOfCursor = x + cursorCoordinates.x;
const leftValueForBigScreen =
const bigScreenLeftOffset =
xCoordinatesOfCursor + CONST.AUTO_COMPLETE_SUGGESTER.BIG_SCREEN_SUGGESTION_WIDTH > windowWidth
? windowWidth - CONST.AUTO_COMPLETE_SUGGESTER.BIG_SCREEN_SUGGESTION_WIDTH
: xCoordinatesOfCursor;

let bottomValue = windowHeight - y - cursorCoordinates.y + scrollValue - (keyboardHeight || bottomInset);
const widthValue = isSmallScreenWidth ? width : CONST.AUTO_COMPLETE_SUGGESTER.BIG_SCREEN_SUGGESTION_WIDTH;

const contentMaxHeight = measureHeightOfSuggestionRows(suggestionsLength, true);
const contentMinHeight = measureHeightOfSuggestionRows(suggestionsLength, false);
const isEnoughSpaceAboveForBig = windowHeight - bottomValue - contentMaxHeight > CONST.AUTO_COMPLETE_SUGGESTER.SUGGESTION_BOX_MAX_SAFE_DISTANCE;
const isEnoughSpaceAboveForSmall = windowHeight - bottomValue - contentMinHeight > CONST.AUTO_COMPLETE_SUGGESTER.SUGGESTION_BOX_MAX_SAFE_DISTANCE;
let bottomValue = windowHeight - (cursorCoordinates.y - scrollValue + y) - keyboardHeight;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it intentional to remove || bottomInset?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, due to the coordinate grid starting from the top

const widthValue = isSmallScreenWidth ? width : CONST.AUTO_COMPLETE_SUGGESTER.BIG_SCREEN_SUGGESTION_WIDTH;

const isEnoughSpaceToRenderMenuAboveForBig = isEnoughSpaceToRenderMenuAboveCursor({y, cursorCoordinates, scrollValue, contentHeight: contentMaxHeight, topInset});
const isEnoughSpaceToRenderMenuAboveForSmall = isEnoughSpaceToRenderMenuAboveCursor({y, cursorCoordinates, scrollValue, contentHeight: contentMinHeight, topInset});

const newLeftValue = isSmallScreenWidth ? x : leftValueForBigScreen;
const newLeftOffset = isSmallScreenWidth ? x : bigScreenLeftOffset;
// If the suggested word is longer than 150 (approximately half the width of the suggestion popup), then adjust a new position of popup
const isAdjustmentNeeded = Math.abs(prevLeftValue.current - leftValueForBigScreen) > 150;
const isAdjustmentNeeded = Math.abs(prevLeftValue.current - bigScreenLeftOffset) > 150;
if (isInitialRender.current || isAdjustmentNeeded) {
isSuggestionAboveRef.current = isSuggestionRenderedAbove(isEnoughSpaceAboveForBig, isEnoughSpaceAboveForSmall);
leftValue.current = newLeftValue;
isSuggestionMenuAboveRef.current = isSuggestionMenuRenderedAbove(isEnoughSpaceToRenderMenuAboveForBig, isEnoughSpaceToRenderMenuAboveForSmall);
leftValue.current = newLeftOffset;
isInitialRender.current = false;
prevLeftValue.current = newLeftValue;
prevLeftValue.current = newLeftOffset;
}

let measuredHeight = 0;
if (isSuggestionAboveRef.current && isEnoughSpaceAboveForBig) {
if (isSuggestionMenuAboveRef.current && isEnoughSpaceToRenderMenuAboveForBig) {
// calculation for big suggestion box above the cursor
measuredHeight = measureHeightOfSuggestionRows(suggestionsLength, true);
} else if (isSuggestionAboveRef.current && isEnoughSpaceAboveForSmall) {
} else if (isSuggestionMenuAboveRef.current && isEnoughSpaceToRenderMenuAboveForSmall) {
// calculation for small suggestion box above the cursor
measuredHeight = measureHeightOfSuggestionRows(suggestionsLength, false);
} else {
// calculation for big suggestion box below the cursor
measuredHeight = measureHeightOfSuggestionRows(suggestionsLength, true);
bottomValue = windowHeight - y - cursorCoordinates.y + scrollValue - measuredHeight - CONST.AUTO_COMPLETE_SUGGESTER.SUGGESTION_ROW_HEIGHT;
bottomValue = windowHeight - y - cursorCoordinates.y + scrollValue - measuredHeight - CONST.AUTO_COMPLETE_SUGGESTER.SUGGESTION_ROW_HEIGHT - keyboardHeight;
}
setSuggestionHeight(measuredHeight);
setContainerState({
left: leftValue.current,
bottom: bottomValue,
width: widthValue,
cursorCoordinates,
});
});
}, [measureParentContainerAndReportCursor, windowHeight, windowWidth, keyboardHeight, isSmallScreenWidth, suggestionsLength, bottomInset]);
}, [measureParentContainerAndReportCursor, windowHeight, windowWidth, keyboardHeight, isSmallScreenWidth, suggestionsLength, bottomInset, topInset]);

if (containerState.width === 0 && containerState.left === 0 && containerState.bottom === 0) {
if ((containerState.width === 0 && containerState.left === 0 && containerState.bottom === 0) || (containerState.cursorCoordinates.x === 0 && containerState.cursorCoordinates.y === 0)) {
return null;
}
return (
Expand Down
2 changes: 2 additions & 0 deletions src/libs/focusComposerWithDelay/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type {TextInput} from 'react-native';
type Selection = {
start: number;
end: number;
positionX?: number;
positionY?: number;
};

type FocusComposerWithDelay = (shouldDelay?: boolean, forcedSelectionRange?: Selection) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,8 @@ function ComposerWithSuggestions(
}, []);

useEffect(() => {
// We use the tag to store the native ID of the text input. Later, we use it in onSelectionChange to pick up the proper text input data.

tag.value = findNodeHandle(textInputRef.current) ?? -1;
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, []);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type SuggestionsRef = {
updateShouldShowSuggestionMenuToFalse: (shouldShowSuggestionMenu?: boolean) => void;
setShouldBlockSuggestionCalc: (shouldBlock: boolean) => void;
getSuggestions: () => Mention[] | Emoji[];
getIsSuggestionsMenuVisible: () => boolean;
};

type ReportActionComposeOnyxProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function SuggestionEmoji(
*/
const calculateEmojiSuggestion = useCallback(
(selectionStart?: number, selectionEnd?: number) => {
if (selectionStart !== selectionEnd || !selectionEnd || shouldBlockCalc.current || !value) {
if (selectionStart !== selectionEnd || !selectionEnd || shouldBlockCalc.current || !value || (selectionStart === 0 && selectionEnd === 0)) {
shouldBlockCalc.current = false;
resetSuggestions();
return;
Expand Down Expand Up @@ -181,6 +181,7 @@ function SuggestionEmoji(
if (!isComposerFocused) {
return;
}

calculateEmojiSuggestion(selection.start, selection.end);
}, [selection, calculateEmojiSuggestion, isComposerFocused]);

Expand All @@ -193,6 +194,8 @@ function SuggestionEmoji(

const getSuggestions = useCallback(() => suggestionValues.suggestedEmojis, [suggestionValues]);

const getIsSuggestionsMenuVisible = useCallback(() => isEmojiSuggestionsMenuVisible, [isEmojiSuggestionsMenuVisible]);

useImperativeHandle(
ref,
() => ({
Expand All @@ -201,8 +204,9 @@ function SuggestionEmoji(
setShouldBlockSuggestionCalc,
updateShouldShowSuggestionMenuToFalse,
getSuggestions,
getIsSuggestionsMenuVisible,
}),
[resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse, getSuggestions],
[resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse, getSuggestions, getIsSuggestionsMenuVisible],
);

if (!isEmojiSuggestionsMenuVisible) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ function SuggestionMention(
);

const getSuggestions = useCallback(() => suggestionValues.suggestedMentions, [suggestionValues]);
const getIsSuggestionsMenuVisible = useCallback(() => isMentionSuggestionsMenuVisible, [isMentionSuggestionsMenuVisible]);

useImperativeHandle(
ref,
Expand All @@ -417,8 +418,9 @@ function SuggestionMention(
setShouldBlockSuggestionCalc,
updateShouldShowSuggestionMenuToFalse,
getSuggestions,
getIsSuggestionsMenuVisible,
}),
[resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse, getSuggestions],
[resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse, getSuggestions, getIsSuggestionsMenuVisible],
);

if (!isMentionSuggestionsMenuVisible) {
Expand Down
8 changes: 7 additions & 1 deletion src/pages/home/report/ReportActionCompose/Suggestions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ function Suggestions(
suggestionEmojiRef.current?.setShouldBlockSuggestionCalc(shouldBlock);
suggestionMentionRef.current?.setShouldBlockSuggestionCalc(shouldBlock);
}, []);
const getIsSuggestionsMenuVisible = useCallback((): boolean => {
const isEmojiVisible = suggestionEmojiRef.current?.getIsSuggestionsMenuVisible() ?? false;
const isSuggestionVisible = suggestionMentionRef.current?.getIsSuggestionsMenuVisible() ?? false;
return isEmojiVisible || isSuggestionVisible;
}, []);

useImperativeHandle(
ref,
Expand All @@ -134,8 +139,9 @@ function Suggestions(
updateShouldShowSuggestionMenuToFalse,
setShouldBlockSuggestionCalc,
getSuggestions,
getIsSuggestionsMenuVisible,
}),
[onSelectionChange, resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse, getSuggestions],
[onSelectionChange, resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse, getSuggestions, getIsSuggestionsMenuVisible],
);

useEffect(() => {
Expand Down
Loading
Loading