2021-09-13 20:41:05 +00:00
|
|
|
import React from 'react';
|
2022-04-15 03:11:36 +00:00
|
|
|
import { I18nManager, StyleProp, StyleSheet, Text, TextStyle, View } from 'react-native';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-08-17 13:32:21 +00:00
|
|
|
import Touch from '../Touch';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2021-09-13 20:41:05 +00:00
|
|
|
import sharedStyles from '../../views/Styles';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes, useTheme } from '../../theme';
|
2021-09-13 20:41:05 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
import { Icon } from '.';
|
|
|
|
import { BASE_HEIGHT, ICON_SIZE, PADDING_HORIZONTAL } from './constants';
|
2022-03-25 18:09:02 +00:00
|
|
|
import { useDimensions } from '../../dimensions';
|
2022-05-02 19:21:15 +00:00
|
|
|
import { CustomIcon } from '../CustomIcon';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
paddingHorizontal: PADDING_HORIZONTAL
|
|
|
|
},
|
|
|
|
leftContainer: {
|
|
|
|
paddingRight: PADDING_HORIZONTAL
|
|
|
|
},
|
|
|
|
rightContainer: {
|
|
|
|
paddingLeft: PADDING_HORIZONTAL
|
|
|
|
},
|
|
|
|
disabled: {
|
|
|
|
opacity: 0.3
|
|
|
|
},
|
|
|
|
textContainer: {
|
|
|
|
flex: 1,
|
|
|
|
justifyContent: 'center'
|
|
|
|
},
|
|
|
|
textAlertContainer: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
alertIcon: {
|
|
|
|
paddingLeft: 4
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
flexShrink: 1,
|
|
|
|
fontSize: 16,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
},
|
|
|
|
subtitle: {
|
|
|
|
fontSize: 14,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
},
|
|
|
|
actionIndicator: {
|
|
|
|
...(I18nManager.isRTL ? { transform: [{ rotate: '180deg' }] } : {})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
interface IListItemContent {
|
|
|
|
title?: string;
|
|
|
|
subtitle?: string;
|
2022-01-17 16:10:39 +00:00
|
|
|
left?: () => JSX.Element | null;
|
|
|
|
right?: () => JSX.Element | null;
|
2021-09-13 20:41:05 +00:00
|
|
|
disabled?: boolean;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2021-09-13 20:41:05 +00:00
|
|
|
testID?: string;
|
|
|
|
color?: string;
|
|
|
|
translateTitle?: boolean;
|
|
|
|
translateSubtitle?: boolean;
|
|
|
|
showActionIndicator?: boolean;
|
|
|
|
alert?: boolean;
|
2022-04-15 03:11:36 +00:00
|
|
|
heightContainer?: number;
|
|
|
|
styleTitle?: StyleProp<TextStyle>;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const Content = React.memo(
|
|
|
|
({
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
disabled,
|
|
|
|
testID,
|
|
|
|
left,
|
|
|
|
right,
|
|
|
|
color,
|
|
|
|
alert,
|
|
|
|
translateTitle = true,
|
|
|
|
translateSubtitle = true,
|
2022-03-25 18:09:02 +00:00
|
|
|
showActionIndicator = false,
|
2022-04-15 03:11:36 +00:00
|
|
|
theme,
|
|
|
|
heightContainer,
|
|
|
|
styleTitle
|
2022-03-25 18:09:02 +00:00
|
|
|
}: IListItemContent) => {
|
|
|
|
const { fontScale } = useDimensions();
|
|
|
|
|
|
|
|
return (
|
2022-04-15 03:11:36 +00:00
|
|
|
<View
|
|
|
|
style={[styles.container, disabled && styles.disabled, { height: (heightContainer || BASE_HEIGHT) * fontScale }]}
|
2022-08-08 21:02:08 +00:00
|
|
|
testID={testID}
|
|
|
|
>
|
2022-03-25 18:09:02 +00:00
|
|
|
{left ? <View style={styles.leftContainer}>{left()}</View> : null}
|
|
|
|
<View style={styles.textContainer}>
|
|
|
|
<View style={styles.textAlertContainer}>
|
2022-04-15 03:11:36 +00:00
|
|
|
<Text style={[styles.title, styleTitle, { color: color || themes[theme].titleText }]} numberOfLines={1}>
|
2022-04-14 20:30:41 +00:00
|
|
|
{translateTitle && title ? I18n.t(title) : title}
|
2022-03-25 18:09:02 +00:00
|
|
|
</Text>
|
|
|
|
{alert ? (
|
2022-05-02 19:21:15 +00:00
|
|
|
<CustomIcon name='info' size={ICON_SIZE} color={themes[theme].dangerColor} style={styles.alertIcon} />
|
2022-03-25 18:09:02 +00:00
|
|
|
) : null}
|
|
|
|
</View>
|
|
|
|
{subtitle ? (
|
|
|
|
<Text style={[styles.subtitle, { color: themes[theme].auxiliaryText }]} numberOfLines={1}>
|
|
|
|
{translateSubtitle ? I18n.t(subtitle) : subtitle}
|
|
|
|
</Text>
|
2021-09-13 20:41:05 +00:00
|
|
|
) : null}
|
|
|
|
</View>
|
2022-03-25 18:09:02 +00:00
|
|
|
{right || showActionIndicator ? (
|
|
|
|
<View style={styles.rightContainer}>
|
|
|
|
{right ? right() : null}
|
|
|
|
{showActionIndicator ? <Icon name='chevron-right' style={styles.actionIndicator} /> : null}
|
|
|
|
</View>
|
2021-09-13 20:41:05 +00:00
|
|
|
) : null}
|
|
|
|
</View>
|
2022-03-25 18:09:02 +00:00
|
|
|
);
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
);
|
|
|
|
|
2022-03-25 18:09:02 +00:00
|
|
|
interface IListButtonPress extends IListItemButton {
|
|
|
|
onPress: Function;
|
2022-01-17 16:10:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 18:09:02 +00:00
|
|
|
interface IListItemButton {
|
2021-09-13 20:41:05 +00:00
|
|
|
title?: string;
|
|
|
|
disabled?: boolean;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2022-01-17 16:10:39 +00:00
|
|
|
backgroundColor?: string;
|
2021-09-13 20:41:05 +00:00
|
|
|
underlayColor?: string;
|
|
|
|
}
|
|
|
|
|
2022-03-25 18:09:02 +00:00
|
|
|
const Button = React.memo(({ onPress, backgroundColor, underlayColor, ...props }: IListButtonPress) => (
|
2021-09-13 20:41:05 +00:00
|
|
|
<Touch
|
2022-03-25 18:09:02 +00:00
|
|
|
onPress={() => onPress(props.title)}
|
|
|
|
style={{ backgroundColor: backgroundColor || themes[props.theme].backgroundColor }}
|
2021-09-13 20:41:05 +00:00
|
|
|
underlayColor={underlayColor}
|
|
|
|
enabled={!props.disabled}
|
2022-08-08 21:02:08 +00:00
|
|
|
>
|
2021-09-13 20:41:05 +00:00
|
|
|
<Content {...props} />
|
|
|
|
</Touch>
|
|
|
|
));
|
|
|
|
|
2022-03-25 18:09:02 +00:00
|
|
|
interface IListItem extends Omit<IListItemContent, 'theme'>, Omit<IListItemButton, 'theme'> {
|
2022-01-17 16:10:39 +00:00
|
|
|
backgroundColor?: string;
|
2022-03-25 18:09:02 +00:00
|
|
|
onPress?: Function;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-25 18:09:02 +00:00
|
|
|
const ListItem = React.memo(({ ...props }: IListItem) => {
|
|
|
|
const { theme } = useTheme();
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
if (props.onPress) {
|
2022-03-25 18:09:02 +00:00
|
|
|
const { onPress } = props;
|
|
|
|
return <Button {...props} theme={theme} onPress={onPress} />;
|
2021-09-13 20:41:05 +00:00
|
|
|
}
|
|
|
|
return (
|
2022-03-25 18:09:02 +00:00
|
|
|
<View style={{ backgroundColor: props.backgroundColor || themes[theme].backgroundColor }}>
|
|
|
|
<Content {...props} theme={theme} />
|
2021-09-13 20:41:05 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
ListItem.displayName = 'List.Item';
|
|
|
|
|
2022-03-25 18:09:02 +00:00
|
|
|
export default ListItem;
|