@sendbird/uikit-react-native
Version:
Sendbird UIKit for React Native: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.
200 lines (197 loc) • 7.41 kB
JavaScript
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import React, { useEffect, useMemo, useState } from 'react';
import { KeyboardAvoidingView, Platform, StyleSheet, View } from 'react-native';
import { createStyleSheet, useUIKitTheme } from '@sendbird/uikit-react-native-foundation';
import { Logger, replace, useIIFE, useSafeAreaPadding } from '@sendbird/uikit-utils';
import { useSendbirdChat } from '../../hooks/useContext';
import useMentionTextInput from '../../hooks/useMentionTextInput';
import AttachmentsButton from './AttachmentsButton';
import EditInput from './EditInput';
import { MessageToReplyPreview } from './MessageToReplyPreview';
import SendInput from './SendInput';
import VoiceMessageInput from './VoiceMessageInput';
const AUTO_FOCUS = Platform.select({
ios: false,
android: true,
default: false
});
const KEYBOARD_AVOID_VIEW_BEHAVIOR = Platform.select({
ios: 'padding',
default: undefined
});
// FIXME(iOS): Dynamic style does not work properly when typing the CJK. (https://github.com/facebook/react-native/issues/26107)
// To workaround temporarily, change the key for re-mount the component.
// -> This will affect to keyboard blur when add/remove first mentioned user.
// const GET_INPUT_KEY = (shouldReset: boolean) => {
// return Platform.OS === 'ios' && shouldReset ? 'uikit-input-clear' : 'uikit-input';
// };
// TODO: Refactor 'Edit' mode to clearly
const ChannelInput = props => {
const {
channel,
keyboardAvoidOffset,
messageToEdit,
setMessageToEdit
} = props;
const safeArea = useSafeAreaPadding(['top', 'left', 'right', 'bottom']);
const {
colors,
typography
} = useUIKitTheme();
const {
sbOptions,
mentionManager
} = useSendbirdChat();
const {
selection,
onSelectionChange,
textInputRef,
text,
onChangeText,
mentionedUsers
} = useMentionTextInput({
messageToEdit
});
const inputMode = useIIFE(() => {
if (messageToEdit && !messageToEdit.isFileMessage()) return 'edit';else return 'send';
});
const mentionAvailable = sbOptions.uikit.groupChannel.channel.enableMention && channel.isGroupChannel() && !channel.isBroadcast;
const inputKeyToRemount = 'input'; //GET_INPUT_KEY(mentionAvailable ? mentionedUsers.length === 0 : false);
const [inputHeight, setInputHeight] = useState(styles.inputDefault.height);
const fontStyle = useMemo(() => {
if (!typography.body3.fontSize) return typography.body3;
// NOTE: iOS does not support textAlignVertical, so we should adjust lineHeight to center the text in multiline TextInput.
return {
...typography.body3,
lineHeight: typography.body3.fontSize * 1.275,
textAlignVertical: 'center'
};
}, [typography.body3.fontSize]);
const textInputStyle = StyleSheet.flatten([styles.input, fontStyle, props.style]);
useTypingTrigger(text, channel);
useTextClearOnDisabled(onChangeText, props.inputDisabled);
useAutoFocusOnEditMode(textInputRef, messageToEdit);
const onPressToMention = (user, searchStringRange) => {
const mentionedMessageText = mentionManager.asMentionedMessageText(user, true);
const range = {
start: searchStringRange.start,
end: searchStringRange.start + mentionedMessageText.length - 1
};
onChangeText(replace(text, searchStringRange.start, searchStringRange.end, mentionedMessageText), {
user,
range
});
};
if (!props.shouldRenderInput) {
return /*#__PURE__*/React.createElement(SafeAreaBottom, {
height: safeArea.paddingBottom
});
}
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(KeyboardAvoidingView, {
keyboardVerticalOffset: -safeArea.paddingBottom + keyboardAvoidOffset,
behavior: KEYBOARD_AVOID_VIEW_BEHAVIOR
}, /*#__PURE__*/React.createElement(View, {
style: {
paddingStart: safeArea.paddingStart,
paddingEnd: safeArea.paddingEnd,
backgroundColor: colors.background
}
}, /*#__PURE__*/React.createElement(View, {
onLayout: e => setInputHeight(e.nativeEvent.layout.height),
style: styles.inputContainer
}, inputMode === 'send' && /*#__PURE__*/React.createElement(SendInput, _extends({}, props, {
key: inputKeyToRemount,
ref: textInputRef,
text: text,
onChangeText: onChangeText,
onSelectionChange: onSelectionChange,
mentionedUsers: mentionedUsers,
VoiceMessageInput: props.VoiceMessageInput ?? VoiceMessageInput,
AttachmentsButton: props.AttachmentsButton ?? AttachmentsButton,
MessageToReplyPreview: props.MessageToReplyPreview ?? MessageToReplyPreview,
style: textInputStyle
})), inputMode === 'edit' && messageToEdit && /*#__PURE__*/React.createElement(EditInput, _extends({}, props, {
key: inputKeyToRemount,
ref: textInputRef,
text: text,
onChangeText: onChangeText,
autoFocus: AUTO_FOCUS,
onSelectionChange: onSelectionChange,
mentionedUsers: mentionedUsers,
messageToEdit: messageToEdit,
setMessageToEdit: setMessageToEdit,
style: textInputStyle
}))), /*#__PURE__*/React.createElement(SafeAreaBottom, {
height: safeArea.paddingBottom
}))), mentionAvailable && props.SuggestedMentionList && /*#__PURE__*/React.createElement(props.SuggestedMentionList, {
text: text,
selection: selection,
inputHeight: inputHeight,
topInset: safeArea.paddingTop,
bottomInset: safeArea.paddingBottom,
onPressToMention: onPressToMention,
mentionedUsers: mentionedUsers
}));
};
const useTypingTrigger = (text, channel) => {
useEffect(() => {
function triggerTyping() {
if (channel.isGroupChannel()) {
const action = () => text.length === 0 ? channel.endTyping() : channel.startTyping();
action().catch(error => {
Logger.debug('ChannelInput: Failed to trigger typing', error);
});
}
}
triggerTyping();
}, channel.isGroupChannel() ? [text] : []);
};
const useTextClearOnDisabled = (setText, chatDisabled) => {
useEffect(() => {
if (chatDisabled) setText('');
}, [chatDisabled]);
};
const useAutoFocusOnEditMode = (textInputRef, messageToEdit) => {
useEffect(() => {
if (messageToEdit !== null && messageToEdit !== void 0 && messageToEdit.isUserMessage()) {
if (!AUTO_FOCUS) setTimeout(() => {
var _textInputRef$current;
return (_textInputRef$current = textInputRef.current) === null || _textInputRef$current === void 0 ? void 0 : _textInputRef$current.focus();
}, 500);
}
}, [messageToEdit]);
};
const SafeAreaBottom = ({
height
}) => {
return /*#__PURE__*/React.createElement(View, {
style: {
height
}
});
};
const styles = createStyleSheet({
inputContainer: {
justifyContent: 'center',
width: '100%'
},
inputDefault: {
height: 56
},
input: {
flex: 1,
marginEnd: 4,
borderRadius: 20,
paddingTop: 8,
paddingBottom: 8,
minHeight: 36,
// Android - padding area is hidden
// iOS - padding area is visible
maxHeight: Platform.select({
ios: 36 * 2 + 16,
android: 36 * 2
})
}
});
export default /*#__PURE__*/React.memo(ChannelInput);
//# sourceMappingURL=index.js.map