* copy directly from v1. will get working and then convert class to functional components
* screen showing up correctly. Need to convert lifestyle methods
* create channel button working
* save before refactor in prep for bringing in edit_channel functionality
* change function naming
* clean up lint
* clean up for PR review
* clean up for PR review
* remove hoardcoded styles
* add edit_channel screen
* add handlePatchChannel
* add custom useFormInput hook. use edit screen for both create and edit screens. edit or create screen mode determined by channel prop passed in as a channel or null
* rename edit_channel to create_or_edit_channel
* displayname, header, and purpose are now an object with value and onChange props, created from the useFormInput hook. Now only need to pass this new FormInput Prop to the edit_channel_info component and deconstruct there to get the onChange and value
* fix some lint errors
* fix some lint errors
* remove empty line
* pass intl into utils validate functions because they are not Hooks. add validation for displayName including translations.
* Move useFormInput hook to its own hooks file and import
* simplify
* remove editing prop. Was used to determine if the right button was enabled. It was always true for edit_channel screen and always false for create channel screen. The enableRightButton prop call back is was also used for the same reason.
* remove channeUrl editing references. This was not implemented on v1
* pass editing prop back into component and add back logic. When editing one field must change. when creating, just need to check that name is provided
* lint fixes
* fix typing issue for channel types
* scrolling ref should be fixed. Linting should pass now
* Linting should pass now
* require id field in partial Channel. fixes tsc
* remove everything related to renaming the channel URL. This has never been requred for mobile
* manage state with useReducer so that all actions/state in one location. This also removes the number of onXXX functions and reduces the number of functions in the component
* reorganize code. useEffects are at top. Move type and interfaces outside of function component
* Fix lint
* nit: invert if statement checking a negative
* use cneterChannelColor. in figma this is center channel text, but I verified theme color by comparing to SSO login text color
* Simple snapshot tests as a start
* Add more tests
* update snapshot
* add snapshot tests. Add tests for button enabling and disabling
* simplify test with destructuring.
* PR feedback. formatting changes. get user and teamid from one call
* remove FormInput hook and use value/setvalue convention for controlled components
* no need to setChannelDisplayName after creating/updating channel
* Just pass the setXXX function. Don't need to create as separate callback
* modify floatingTextInput component to allow placeholder text
* remove InteractionManager. PR nits
* mv EditChannelComponent into create_or_edit screen. Rename component from EditChannelInfo to ChannelInfoForm
* correct import path
* add IntlShape Type to function input. Wrap screen with withServerDatabase, not withIntl
* remove state setting function calls from inside the reducer. move close function outside of the component. remove setRightButton and rightButton and place rightbutton in initial appState
* move editing const after useX oneliners and before useCallback, useEffect, and useReducers
* rightButton
- useMemo to memoize an object with dependencies
- move out of the appState
emitCanSaveChannel
- wrap with useCallback
onCreateChannel
- wrap with useCallback
onUpdateChannel
- wrap with useCallback
useEffect Navigation
- use the callbacks as dependencies in stead of the depencies of those
callbacks.
* wrap all formatted message with useMemo()
wrap all onXXXChangeText with useCallback and add deps
move all oneliner derived constants directly after useState useMemo
* remove useMemo from formatted text
* switchToCHannel is still not working. failing at
const channel: ChannelModel = await member.channel.fetch();
* use prepareMyChannelsForTeam to update db tables for new channel
* add placeholder text color
* Attach open edit channel screen to `Set Header` button in channel intro view
port SectionItem from V1 and us to add a Switch for setting private/public channel
hook up the plus icon in the channel list header to create a channel (temporary fix to allow debugging)
add new queryChannelsInfoById and queryCurrentChannelInfo query functions
update text for create screen text inputs
* Fix styles and fix actions
* Add autocomplete, fix patch, and address design feedback
* Address feedback
* Add margin between icon and label on Make Private
* Address feedback
* Address feedback
* Address feedback and fix channel list not updating when the channel gets created
* Address feedback and directly add the channel to the default category
* Render at-mentions as Members if no channelId is set
* Display autocomplete on iOS
Co-authored-by: Jason Frerich <jason.frerich@mattermost.com>
Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
334 lines
11 KiB
TypeScript
334 lines
11 KiB
TypeScript
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
// Note: This file has been adapted from the library https://github.com/csath/react-native-reanimated-text-input
|
|
|
|
import {debounce} from 'lodash';
|
|
import React, {useState, useEffect, useRef, useImperativeHandle, forwardRef, useMemo, useCallback} from 'react';
|
|
import {GestureResponderEvent, LayoutChangeEvent, NativeSyntheticEvent, Platform, StyleProp, TargetedEvent, Text, TextInput, TextInputFocusEventData, TextInputProps, TextStyle, TouchableWithoutFeedback, View, ViewStyle} from 'react-native';
|
|
import Animated, {useCode, interpolateNode, EasingNode, Value, set, Clock} from 'react-native-reanimated';
|
|
|
|
import CompassIcon from '@components/compass_icon';
|
|
import {changeOpacity, makeStyleSheetFromTheme} from '@utils/theme';
|
|
|
|
import {timingAnimation} from './animation_utils';
|
|
|
|
const DEFAULT_INPUT_HEIGHT = 48;
|
|
const BORDER_DEFAULT_WIDTH = 1;
|
|
const BORDER_FOCUSED_WIDTH = 2;
|
|
|
|
const getTopStyle = (styles: any, animation: Value<0|1>, inputText?: string) => {
|
|
if (inputText) {
|
|
return getLabelPositions(styles.textInput, styles.label, styles.smallLabel)[1];
|
|
}
|
|
|
|
return interpolateNode(animation, {
|
|
inputRange: [0, 1],
|
|
outputRange: [...getLabelPositions(styles.textInput, styles.label, styles.smallLabel)],
|
|
});
|
|
};
|
|
|
|
const getFontSize = (styles: any, animation: Value<0|1>, inputText?: string) => {
|
|
if (inputText) {
|
|
return styles.smallLabel.fontSize;
|
|
}
|
|
|
|
return interpolateNode(animation, {
|
|
inputRange: [0, 1],
|
|
outputRange: [styles.textInput.fontSize, styles.smallLabel.fontSize],
|
|
});
|
|
};
|
|
|
|
const getStyleSheet = makeStyleSheetFromTheme((theme: Theme) => ({
|
|
container: {
|
|
height: DEFAULT_INPUT_HEIGHT + (2 * BORDER_DEFAULT_WIDTH),
|
|
width: '100%',
|
|
},
|
|
errorContainer: {
|
|
flexDirection: 'row',
|
|
},
|
|
errorIcon: {
|
|
color: theme.errorTextColor,
|
|
fontSize: 14,
|
|
marginRight: 7,
|
|
top: 5,
|
|
},
|
|
errorText: {
|
|
color: theme.errorTextColor,
|
|
fontFamily: 'OpenSans',
|
|
fontSize: 12,
|
|
lineHeight: 16,
|
|
paddingVertical: 5,
|
|
},
|
|
label: {
|
|
position: 'absolute',
|
|
color: changeOpacity(theme.centerChannelColor, 0.64),
|
|
left: 16,
|
|
fontFamily: 'OpenSans',
|
|
fontSize: 16,
|
|
zIndex: 10,
|
|
},
|
|
smallLabel: {
|
|
fontSize: 10,
|
|
},
|
|
textInput: {
|
|
fontFamily: 'OpenSans',
|
|
fontSize: 16,
|
|
paddingTop: 12,
|
|
paddingBottom: 12,
|
|
paddingHorizontal: 16,
|
|
color: theme.centerChannelColor,
|
|
borderColor: changeOpacity(theme.centerChannelColor, 0.16),
|
|
borderRadius: 4,
|
|
borderWidth: BORDER_DEFAULT_WIDTH,
|
|
backgroundColor: theme.centerChannelBg,
|
|
},
|
|
}));
|
|
|
|
const onExecution = (
|
|
e: NativeSyntheticEvent<TextInputFocusEventData>,
|
|
innerFunc?: () => void,
|
|
outerFunc?: ((event: NativeSyntheticEvent<TargetedEvent>) => void),
|
|
) => {
|
|
innerFunc?.();
|
|
outerFunc?.(e);
|
|
};
|
|
|
|
const getLabelPositions = (style: TextStyle, labelStyle: TextStyle, smallLabelStyle: TextStyle) => {
|
|
const top: number = style?.paddingTop as number || 0;
|
|
const bottom: number = style?.paddingBottom as number || 0;
|
|
|
|
const height: number = (style?.height as number || (top + bottom) || style?.padding as number) || 0;
|
|
const textInputFontSize = style?.fontSize || 13;
|
|
const labelFontSize = labelStyle?.fontSize || 16;
|
|
const smallLabelFontSize = smallLabelStyle?.fontSize || 10;
|
|
const fontSizeDiff = textInputFontSize - labelFontSize;
|
|
const unfocused = (height * 0.5) + (fontSizeDiff * (Platform.OS === 'android' ? 0.5 : 0.6));
|
|
const focused = -(labelFontSize + smallLabelFontSize) * 0.25;
|
|
return [unfocused, focused];
|
|
};
|
|
|
|
export type FloatingTextInputRef = {
|
|
blur: () => void;
|
|
focus: () => void;
|
|
isFocused: () => boolean;
|
|
}
|
|
|
|
type FloatingTextInputProps = TextInputProps & {
|
|
containerStyle?: ViewStyle;
|
|
textInputStyle?: TextStyle;
|
|
labelTextStyle?: TextStyle;
|
|
editable?: boolean;
|
|
error?: string;
|
|
errorIcon?: string;
|
|
isKeyboardInput?: boolean;
|
|
label: string;
|
|
multiline?: boolean;
|
|
onBlur?: (event: NativeSyntheticEvent<TargetedEvent>) => void;
|
|
onFocus?: (e: NativeSyntheticEvent<TargetedEvent>) => void;
|
|
onPress?: (e: GestureResponderEvent) => void;
|
|
onLayout?: (e: LayoutChangeEvent) => void;
|
|
placeholder?: string;
|
|
showErrorIcon?: boolean;
|
|
testID?: string;
|
|
theme: Theme;
|
|
value: string;
|
|
}
|
|
|
|
const FloatingTextInput = forwardRef<FloatingTextInputRef, FloatingTextInputProps>(({
|
|
error,
|
|
containerStyle,
|
|
isKeyboardInput = true,
|
|
editable = true,
|
|
errorIcon = 'alert-outline',
|
|
label = '',
|
|
onPress = undefined,
|
|
onFocus,
|
|
onBlur,
|
|
onLayout,
|
|
showErrorIcon = true,
|
|
placeholder,
|
|
multiline,
|
|
theme,
|
|
value = '',
|
|
textInputStyle,
|
|
labelTextStyle,
|
|
testID,
|
|
...props
|
|
}: FloatingTextInputProps, ref) => {
|
|
const [focusedLabel, setIsFocusLabel] = useState<boolean | undefined>();
|
|
const [focused, setIsFocused] = useState(Boolean(value) && editable);
|
|
const inputRef = useRef<TextInput>(null);
|
|
const [animation] = useState(new Value(focusedLabel ? 1 : 0));
|
|
const debouncedOnFocusTextInput = debounce(setIsFocusLabel, 500, {leading: true, trailing: false});
|
|
const styles = getStyleSheet(theme);
|
|
let from = 0;
|
|
let to = 0;
|
|
if (focusedLabel !== undefined) {
|
|
from = focusedLabel ? 0 : 1;
|
|
to = focusedLabel ? 1 : 0;
|
|
}
|
|
|
|
useImperativeHandle(ref, () => ({
|
|
blur: () => inputRef.current?.blur(),
|
|
focus: () => inputRef.current?.focus(),
|
|
isFocused: () => inputRef.current?.isFocused() || false,
|
|
}), [inputRef]);
|
|
|
|
useCode(
|
|
() => set(
|
|
animation,
|
|
timingAnimation({
|
|
animation,
|
|
duration: 150,
|
|
from,
|
|
to,
|
|
easing: EasingNode.linear,
|
|
clock: new Clock(),
|
|
}),
|
|
),
|
|
[focusedLabel],
|
|
);
|
|
|
|
useEffect(
|
|
() => {
|
|
if (!focusedLabel && value) {
|
|
debouncedOnFocusTextInput(true);
|
|
}
|
|
},
|
|
[value],
|
|
);
|
|
|
|
const onTextInputBlur = useCallback((e: NativeSyntheticEvent<TextInputFocusEventData>) => onExecution(e,
|
|
() => {
|
|
setIsFocusLabel(Boolean(value));
|
|
setIsFocused(false);
|
|
},
|
|
onBlur,
|
|
), [onBlur]);
|
|
|
|
const onTextInputFocus = useCallback((e: NativeSyntheticEvent<TextInputFocusEventData>) => onExecution(e,
|
|
() => {
|
|
setIsFocusLabel(true);
|
|
setIsFocused(true);
|
|
},
|
|
onFocus,
|
|
), [onFocus]);
|
|
|
|
const onAnimatedTextPress = useCallback(() => {
|
|
return focused ? null : inputRef?.current?.focus();
|
|
}, [focused]);
|
|
|
|
const shouldShowError = (!focused && error);
|
|
const onPressAction = !isKeyboardInput && editable && onPress ? onPress : undefined;
|
|
|
|
const combinedContainerStyle = useMemo(() => {
|
|
const res = [styles.container];
|
|
if (multiline) {
|
|
res.push({height: 100 + (2 * BORDER_DEFAULT_WIDTH)});
|
|
}
|
|
res.push(containerStyle);
|
|
return res;
|
|
}, [styles, containerStyle, multiline]);
|
|
|
|
const combinedTextInputStyle = useMemo(() => {
|
|
const res: StyleProp<TextStyle> = [styles.textInput];
|
|
res.push({
|
|
borderWidth: focusedLabel ? BORDER_FOCUSED_WIDTH : BORDER_DEFAULT_WIDTH,
|
|
height: DEFAULT_INPUT_HEIGHT + ((focusedLabel ? BORDER_FOCUSED_WIDTH : BORDER_DEFAULT_WIDTH) * 2),
|
|
});
|
|
|
|
if (focused) {
|
|
res.push({borderColor: theme.buttonBg});
|
|
} else if (shouldShowError) {
|
|
res.push({borderColor: theme.errorTextColor});
|
|
}
|
|
|
|
res.push(textInputStyle);
|
|
|
|
if (multiline) {
|
|
res.push({height: 100, textAlignVertical: 'top'});
|
|
}
|
|
|
|
return res;
|
|
}, [styles, theme, shouldShowError, focused, textInputStyle, focusedLabel, multiline]);
|
|
|
|
const textAnimatedTextStyle = useMemo(() => {
|
|
const res = [styles.label];
|
|
const inputText = value || placeholder;
|
|
res.push({
|
|
top: getTopStyle(styles, animation, inputText),
|
|
fontSize: getFontSize(styles, animation, inputText),
|
|
backgroundColor: (
|
|
focusedLabel || inputText ? theme.centerChannelBg : 'transparent'
|
|
),
|
|
paddingHorizontal: focusedLabel || inputText ? 4 : 0,
|
|
color: styles.label.color,
|
|
});
|
|
|
|
if (focused) {
|
|
res.push({color: theme.buttonBg});
|
|
}
|
|
|
|
res.push(labelTextStyle);
|
|
|
|
if (shouldShowError) {
|
|
res.push({color: theme.errorTextColor});
|
|
}
|
|
|
|
return res;
|
|
}, [theme, styles, focused, shouldShowError, labelTextStyle, placeholder, animation, focusedLabel]);
|
|
|
|
return (
|
|
<TouchableWithoutFeedback
|
|
onPress={onPressAction}
|
|
onLayout={onLayout}
|
|
>
|
|
<View style={combinedContainerStyle}>
|
|
<Animated.Text
|
|
onPress={onAnimatedTextPress}
|
|
style={textAnimatedTextStyle}
|
|
suppressHighlighting={true}
|
|
>
|
|
{label}
|
|
</Animated.Text>
|
|
<TextInput
|
|
{...props}
|
|
editable={isKeyboardInput && editable}
|
|
style={combinedTextInputStyle}
|
|
placeholder={placeholder}
|
|
placeholderTextColor={styles.label.color}
|
|
multiline={multiline}
|
|
value={value}
|
|
pointerEvents={isKeyboardInput ? 'auto' : 'none'}
|
|
onFocus={onTextInputFocus}
|
|
onBlur={onTextInputBlur}
|
|
ref={inputRef}
|
|
underlineColorAndroid='transparent'
|
|
testID={testID}
|
|
/>
|
|
{Boolean(error) && (
|
|
<View style={styles.errorContainer}>
|
|
{showErrorIcon && errorIcon &&
|
|
<CompassIcon
|
|
name={errorIcon}
|
|
style={styles.errorIcon}
|
|
/>
|
|
}
|
|
<Text
|
|
style={styles.errorText}
|
|
testID={`${testID}.error`}
|
|
>
|
|
{error}
|
|
</Text>
|
|
</View>
|
|
)}
|
|
</View>
|
|
</TouchableWithoutFeedback>
|
|
);
|
|
});
|
|
|
|
FloatingTextInput.displayName = 'FloatingTextInput';
|
|
|
|
export default FloatingTextInput;
|
|
|