forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Expensify#35226 from dukenv0307/fix/34442
Implement suggestion for edit composer
- Loading branch information
Showing
18 changed files
with
340 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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
98 changes: 98 additions & 0 deletions
98
...me/report/ReportActionCompose/ComposerWithSuggestionsEdit/ComposerWithSuggestionsEdit.tsx
This file contains 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,98 @@ | ||
import type {Dispatch, ForwardedRef, RefObject, SetStateAction} from 'react'; | ||
import React, {useState} from 'react'; | ||
import type {MeasureInWindowOnSuccessCallback, TextInput} from 'react-native'; | ||
import Composer from '@components/Composer'; | ||
import type {ComposerProps} from '@components/Composer/types'; | ||
import type {SuggestionsRef} from '@pages/home/report/ReportActionCompose/ReportActionCompose'; | ||
import Suggestions from '@pages/home/report/ReportActionCompose/Suggestions'; | ||
|
||
type Selection = { | ||
start: number; | ||
end: number; | ||
}; | ||
|
||
type ComposerWithSuggestionsEditProps = ComposerProps & { | ||
setValue: Dispatch<SetStateAction<string>>; | ||
setSelection: Dispatch<SetStateAction<Selection>>; | ||
resetKeyboardInput: () => void; | ||
isComposerFocused: boolean; | ||
suggestionsRef: RefObject<SuggestionsRef>; | ||
updateDraft: (newValue: string) => void; | ||
measureParentContainer: (callback: MeasureInWindowOnSuccessCallback) => void; | ||
value: string; | ||
selection: Selection; | ||
isGroupPolicyReport: boolean; | ||
}; | ||
|
||
function ComposerWithSuggestionsEdit( | ||
{ | ||
value, | ||
maxLines = -1, | ||
onKeyPress = () => {}, | ||
style, | ||
onSelectionChange = () => {}, | ||
selection = { | ||
start: 0, | ||
end: 0, | ||
}, | ||
onBlur = () => {}, | ||
onFocus = () => {}, | ||
onChangeText = () => {}, | ||
setValue = () => {}, | ||
setSelection = () => {}, | ||
resetKeyboardInput = () => {}, | ||
isComposerFocused, | ||
suggestionsRef, | ||
updateDraft, | ||
measureParentContainer, | ||
id = undefined, | ||
isGroupPolicyReport, | ||
}: ComposerWithSuggestionsEditProps, | ||
ref: ForwardedRef<TextInput>, | ||
) { | ||
const [composerHeight, setComposerHeight] = useState(0); | ||
|
||
return ( | ||
<> | ||
<Composer | ||
multiline | ||
ref={ref} | ||
id={id} | ||
onChangeText={onChangeText} // Debounced saveDraftComment | ||
onKeyPress={onKeyPress} | ||
value={value} | ||
maxLines={maxLines} // This is the same that slack has | ||
style={style} | ||
onFocus={onFocus} | ||
onBlur={onBlur} | ||
selection={selection} | ||
onSelectionChange={onSelectionChange} | ||
onLayout={(e) => { | ||
const composerLayoutHeight = e.nativeEvent.layout.height; | ||
if (composerHeight === composerLayoutHeight) { | ||
return; | ||
} | ||
setComposerHeight(composerLayoutHeight); | ||
}} | ||
/> | ||
|
||
<Suggestions | ||
ref={suggestionsRef} | ||
isComposerFullSize={false} | ||
isComposerFocused={isComposerFocused} | ||
updateComment={updateDraft} | ||
composerHeight={composerHeight} | ||
measureParentContainer={measureParentContainer} | ||
isAutoSuggestionPickerLarge | ||
value={value} | ||
setValue={setValue} | ||
selection={selection} | ||
setSelection={setSelection} | ||
resetKeyboardInput={resetKeyboardInput} | ||
isGroupPolicyReport={isGroupPolicyReport} | ||
/> | ||
</> | ||
); | ||
} | ||
|
||
export default React.forwardRef(ComposerWithSuggestionsEdit); |
56 changes: 56 additions & 0 deletions
56
src/pages/home/report/ReportActionCompose/ComposerWithSuggestionsEdit/SuggestionsContext.tsx
This file contains 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,56 @@ | ||
import type {MutableRefObject, ReactNode} from 'react'; | ||
import React, {createContext, useCallback, useContext, useMemo, useRef, useState} from 'react'; | ||
import type {SuggestionsRef} from '@pages/home/report/ReportActionCompose/ReportActionCompose'; | ||
|
||
type SuggestionsContextProviderProps = { | ||
children?: ReactNode; | ||
}; | ||
|
||
type SuggestionsContextProps = { | ||
activeID: string | null; | ||
currentActiveSuggestionsRef: MutableRefObject<SuggestionsRef | null>; | ||
updateCurrentActiveSuggestionsRef: (ref: SuggestionsRef | null, id: string) => void; | ||
clearActiveSuggestionsRef: () => void; | ||
isActiveSuggestions: (id: string) => boolean; | ||
}; | ||
|
||
const SuggestionsContext = createContext<SuggestionsContextProps>({ | ||
activeID: null, | ||
currentActiveSuggestionsRef: {current: null}, | ||
updateCurrentActiveSuggestionsRef: () => {}, | ||
clearActiveSuggestionsRef: () => {}, | ||
isActiveSuggestions: () => false, | ||
}); | ||
|
||
function SuggestionsContextProvider({children}: SuggestionsContextProviderProps) { | ||
const currentActiveSuggestionsRef = useRef<SuggestionsRef | null>(null); | ||
const [activeID, setActiveID] = useState<string | null>(null); | ||
|
||
const updateCurrentActiveSuggestionsRef = useCallback((ref: SuggestionsRef | null, id: string) => { | ||
currentActiveSuggestionsRef.current = ref; | ||
setActiveID(id); | ||
}, []); | ||
|
||
const clearActiveSuggestionsRef = useCallback(() => { | ||
currentActiveSuggestionsRef.current = null; | ||
setActiveID(null); | ||
}, []); | ||
|
||
const isActiveSuggestions = useCallback((id: string) => id === activeID, [activeID]); | ||
|
||
const contextValue = useMemo( | ||
() => ({activeID, currentActiveSuggestionsRef, updateCurrentActiveSuggestionsRef, clearActiveSuggestionsRef, isActiveSuggestions}), | ||
[activeID, currentActiveSuggestionsRef, updateCurrentActiveSuggestionsRef, clearActiveSuggestionsRef, isActiveSuggestions], | ||
); | ||
|
||
return <SuggestionsContext.Provider value={contextValue}>{children}</SuggestionsContext.Provider>; | ||
} | ||
|
||
function useSuggestionsContext() { | ||
const context = useContext(SuggestionsContext); | ||
return context; | ||
} | ||
|
||
SuggestionsContextProvider.displayName = 'SuggestionsContextProvider'; | ||
|
||
export {SuggestionsContextProvider, useSuggestionsContext}; |
This file contains 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 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
Oops, something went wrong.