2022-07-07 12:29:18 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2022-07-08 08:48:30 +00:00
|
|
|
import {
|
|
|
|
Button as NativePaperButton,
|
|
|
|
Text,
|
|
|
|
TouchableRipple
|
|
|
|
} from 'react-native-paper';
|
2022-07-07 12:29:18 +00:00
|
|
|
|
|
|
|
import { BUTTON_MODES, BUTTON_TYPES } from '../../constants';
|
2022-07-27 08:40:34 +00:00
|
|
|
import BaseTheme from '../BaseTheme.native';
|
|
|
|
import { ButtonProps } from '../types';
|
|
|
|
|
|
|
|
import styles from './buttonStyles';
|
2022-07-11 12:30:37 +00:00
|
|
|
|
2022-07-27 08:40:34 +00:00
|
|
|
export interface IButtonProps extends ButtonProps {
|
|
|
|
color?: string;
|
2022-10-11 08:26:09 +00:00
|
|
|
contentStyle?: Object | undefined;
|
2022-07-27 08:40:34 +00:00
|
|
|
labelStyle?: Object | undefined;
|
|
|
|
style?: Object | undefined;
|
|
|
|
}
|
2022-07-07 12:29:18 +00:00
|
|
|
|
2022-07-20 06:19:59 +00:00
|
|
|
const Button: React.FC<IButtonProps> = ({
|
2022-07-07 12:29:18 +00:00
|
|
|
accessibilityLabel,
|
|
|
|
color: buttonColor,
|
2022-10-11 08:26:09 +00:00
|
|
|
contentStyle,
|
2022-07-07 12:29:18 +00:00
|
|
|
disabled,
|
|
|
|
icon,
|
2022-08-22 09:40:59 +00:00
|
|
|
labelKey,
|
2022-07-07 12:29:18 +00:00
|
|
|
labelStyle,
|
2022-08-22 09:40:59 +00:00
|
|
|
onClick: onPress,
|
2022-07-07 12:29:18 +00:00
|
|
|
style,
|
|
|
|
type
|
2022-07-20 06:19:59 +00:00
|
|
|
}: IButtonProps) => {
|
2022-07-07 12:29:18 +00:00
|
|
|
const { t } = useTranslation();
|
2022-07-08 08:48:30 +00:00
|
|
|
const { CONTAINED } = BUTTON_MODES;
|
2022-07-07 12:29:18 +00:00
|
|
|
const { DESTRUCTIVE, PRIMARY, SECONDARY, TERTIARY } = BUTTON_TYPES;
|
|
|
|
|
|
|
|
let buttonLabelStyles;
|
|
|
|
let buttonStyles;
|
|
|
|
let color;
|
|
|
|
let mode;
|
|
|
|
|
|
|
|
if (type === PRIMARY) {
|
|
|
|
buttonLabelStyles = styles.buttonLabelPrimary;
|
|
|
|
color = BaseTheme.palette.action01;
|
2022-07-11 12:30:37 +00:00
|
|
|
mode = CONTAINED;
|
2022-07-07 12:29:18 +00:00
|
|
|
} else if (type === SECONDARY) {
|
|
|
|
buttonLabelStyles = styles.buttonLabelSecondary;
|
|
|
|
color = BaseTheme.palette.action02;
|
2022-07-11 12:30:37 +00:00
|
|
|
mode = CONTAINED;
|
2022-07-07 12:29:18 +00:00
|
|
|
} else if (type === DESTRUCTIVE) {
|
|
|
|
color = BaseTheme.palette.actionDanger;
|
|
|
|
buttonLabelStyles = styles.buttonLabelDestructive;
|
2022-07-11 12:30:37 +00:00
|
|
|
mode = CONTAINED;
|
|
|
|
} else {
|
2022-07-07 12:29:18 +00:00
|
|
|
color = buttonColor;
|
|
|
|
buttonLabelStyles = styles.buttonLabel;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disabled) {
|
|
|
|
buttonLabelStyles = styles.buttonLabelDisabled;
|
|
|
|
buttonStyles = styles.buttonDisabled;
|
|
|
|
} else {
|
|
|
|
buttonStyles = styles.button;
|
|
|
|
}
|
|
|
|
|
2022-07-11 12:30:37 +00:00
|
|
|
if (type === TERTIARY) {
|
2022-07-08 08:48:30 +00:00
|
|
|
return (
|
|
|
|
<TouchableRipple
|
|
|
|
accessibilityLabel = { accessibilityLabel }
|
|
|
|
disabled = { disabled }
|
|
|
|
onPress = { onPress }
|
|
|
|
rippleColor = 'transparent'
|
|
|
|
style = { [
|
|
|
|
buttonStyles,
|
|
|
|
style
|
|
|
|
] }>
|
|
|
|
<Text
|
|
|
|
style = { [
|
2022-07-11 12:30:37 +00:00
|
|
|
buttonLabelStyles,
|
|
|
|
labelStyle
|
2022-08-22 09:40:59 +00:00
|
|
|
] }>{ t(labelKey ?? '') }</Text>
|
2022-07-08 08:48:30 +00:00
|
|
|
</TouchableRipple>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-07 12:29:18 +00:00
|
|
|
return (
|
|
|
|
<NativePaperButton
|
2022-07-18 13:16:08 +00:00
|
|
|
accessibilityLabel = { t(accessibilityLabel ?? '') }
|
2022-08-22 09:40:59 +00:00
|
|
|
children = { t(labelKey ?? '') }
|
2022-07-07 12:29:18 +00:00
|
|
|
color = { color }
|
2022-10-11 08:26:09 +00:00
|
|
|
contentStyle = { [
|
|
|
|
styles.buttonContent,
|
|
|
|
contentStyle
|
|
|
|
] }
|
2022-07-07 12:29:18 +00:00
|
|
|
disabled = { disabled }
|
2022-10-11 10:47:54 +00:00
|
|
|
|
2022-08-22 09:40:59 +00:00
|
|
|
// @ts-ignore
|
2022-07-07 12:29:18 +00:00
|
|
|
icon = { icon }
|
|
|
|
labelStyle = { [
|
|
|
|
buttonLabelStyles,
|
|
|
|
labelStyle
|
|
|
|
] }
|
|
|
|
mode = { mode }
|
|
|
|
onPress = { onPress }
|
|
|
|
style = { [
|
|
|
|
buttonStyles,
|
|
|
|
style
|
|
|
|
] } />
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Button;
|