import React, { useState } from 'react';
import { TextStyle, Text, StyleSheet } from 'react-native';
import sharedStyles from '../../views/Styles';
import { useTheme } from '../../theme';
import { previewFormatText } from '../markdown/previewFormatText';
import I18n from '../../i18n';
interface ICollapsibleText {
msg?: string;
style?: TextStyle[];
linesToTruncate?: number;
}
const styles = StyleSheet.create({
text: {
fontSize: 16,
...sharedStyles.textRegular,
textAlignVertical: 'center'
},
textInfo: {
fontSize: 14,
...sharedStyles.textRegular
}
});
const CollapsibleText = ({ msg, style = [], linesToTruncate = 1 }: ICollapsibleText) => {
const [truncatedText, setTruncatedText] = useState('');
const [showTruncated, setShowTruncated] = useState(true);
const { colors } = useTheme();
if (!msg) {
return null;
}
const m = previewFormatText(msg);
if (truncatedText && showTruncated) {
return (
{`${truncatedText}... `}
setShowTruncated(false)} style={[styles.textInfo, { color: colors.actionTintColor }]}>
{I18n.t('Show_more')}
);
}
return (
{
const { lines } = event.nativeEvent;
if (lines.length > linesToTruncate) {
const text = lines
.splice(0, linesToTruncate)
.map(line => line.text)
.join('');
const truncatedTextLengthWithShowMore = text.length - (4 + I18n.t('Show_more').length);
const clippedText = text.slice(0, truncatedTextLengthWithShowMore);
setTruncatedText(clippedText);
} else {
setShowTruncated(false);
}
}}
>
{m}
{truncatedText ? (
setShowTruncated(true)}
style={[styles.textInfo, { color: colors.actionTintColor }]}
>
{` ${I18n.t('Show_less')}`}
) : null}
);
};
export default CollapsibleText;