mattermost-mobile/app/screens/edit_post/edit_post_input/index.tsx
Avinash Lingaloo 9e77c419b1
MM-41991 Gekidou Edit Post Screen (#6016)
* edit screen - in progress

* edit screen - in progress

* edit post screen - post input - in progress

* edit post screen - post input - in progress

* edit post screen - post input - in progress

* edit post screen - post input - in progress

* edit post screen - post error component - in progress

* edit post screen - post error component - in progress

* edit post screen -emitEditing - in progress

* edit post screen - in progress

* edit post screen - in progress

* edit post screen - in progress

* able to edit post

* edit post screen - in progress

* edit post screen - in progress

* edit post screen - in progress

* edit post screen - in progress

* updated errorLine

* corrections

* edit post screen - in progress

* edit post screen - in progress

* edit post screen - in progress

* properly closes modal on tablets

* starts with Save button set to false

* refactored onTextSelectionChange

* added useTheme to ErrorTextComponent

* passing canEdit and hasFilesAttached

* passing canEdit and hasFilesAttached

* fix API call

* change canEdit to canDelete

* nearly there

* displays alert

* maxPostSize

* autocomplete - fixing layout

* autocomplete - fixing layout

* autocomplete - work in progress

* autocomplete - work in progress

* clean up delete

* fixing autocomplete

* code fix

* added server error message

* update i18n

* removed comment

* code fix

* fix bug on empty post message

* post input top corrections

* post draft limit

* code corrections as per review

* removed theme from useEffect

* update edit_post - delete call

* refactor PostInputRef to EditPostInputRef

* autocomplete position fix and feedback addressed

* Navigation title & subtitle fonts / navigation button builder

* ux feedback

* delay focus of edit input by 20 frames

* properly dismiss the PostOptions screen

this comes from the fix for the BottomSheet screen

* using device info to check for physical keyboard

* autocomplete with keyboard closed

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2022-03-12 17:22:24 -03:00

93 lines
3 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {forwardRef, useCallback, useImperativeHandle, useMemo, useRef} from 'react';
import {useIntl} from 'react-intl';
import {KeyboardType, Platform, TextInput, useWindowDimensions, View} from 'react-native';
import {useTheme} from '@context/theme';
import {changeOpacity, getKeyboardAppearanceFromTheme, makeStyleSheetFromTheme} from '@utils/theme';
import {typography} from '@utils/typography';
const getStyleSheet = makeStyleSheetFromTheme((theme) => ({
input: {
color: theme.centerChannelColor,
padding: 15,
textAlignVertical: 'top',
...typography('Body', 200),
},
inputContainer: {
backgroundColor: theme.centerChannelBg,
marginTop: 2,
},
}));
const HEIGHT_DIFF = Platform.select({android: 40, default: 30});
export type EditPostInputRef = {
focus: () => void;
}
type PostInputProps = {
keyboardType: KeyboardType;
message: string;
hasError: boolean;
onTextSelectionChange: (curPos: number) => void;
onChangeText: (text: string) => void;
}
const EditPostInput = forwardRef<EditPostInputRef, PostInputProps>(({
keyboardType, message, onChangeText, onTextSelectionChange, hasError,
}: PostInputProps, ref) => {
const intl = useIntl();
const theme = useTheme();
const styles = getStyleSheet(theme);
const {height} = useWindowDimensions();
const textInputHeight = (height / 2) - HEIGHT_DIFF;
const inputRef = useRef<TextInput>(null);
const inputStyle = useMemo(() => {
return [styles.input, {height: textInputHeight}];
}, [textInputHeight, styles]);
const onSelectionChange = useCallback((event) => {
const curPos = event.nativeEvent.selection.end;
onTextSelectionChange(curPos);
}, [onTextSelectionChange]);
const containerStyle = useMemo(() => [
styles.inputContainer,
hasError && {marginTop: 0},
{height: textInputHeight},
], [styles, textInputHeight]);
useImperativeHandle(ref, () => ({
focus: () => inputRef.current?.focus(),
}), [inputRef.current]);
return (
<View style={containerStyle}>
<TextInput
ref={inputRef}
blurOnSubmit={false}
disableFullscreenUI={true}
keyboardAppearance={getKeyboardAppearanceFromTheme(theme)}
keyboardType={keyboardType}
multiline={true}
onChangeText={onChangeText}
onSelectionChange={onSelectionChange}
placeholder={intl.formatMessage({id: 'edit_post.editPost', defaultMessage: 'Edit the post...'})}
placeholderTextColor={changeOpacity(theme.centerChannelColor, 0.4)}
style={inputStyle}
testID='edit_post.message.input'
underlineColorAndroid='transparent'
value={message}
/>
</View>
);
});
EditPostInput.displayName = 'EditPostInput';
export default EditPostInput;