You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Button.tsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import React from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { TouchableHighlight } from 'react-native';
  4. import { Button as NativePaperButton, Text } from 'react-native-paper';
  5. import { BUTTON_MODES, BUTTON_TYPES } from '../../constants.native';
  6. import BaseTheme from '../BaseTheme.native';
  7. import { IButtonProps } from '../types';
  8. import styles from './buttonStyles';
  9. export interface IProps extends IButtonProps {
  10. color?: string | undefined;
  11. contentStyle?: Object | undefined;
  12. labelStyle?: Object | undefined;
  13. mode?: any;
  14. style?: Object | undefined;
  15. }
  16. const Button: React.FC<IProps> = ({
  17. accessibilityLabel,
  18. color: buttonColor,
  19. contentStyle,
  20. disabled,
  21. icon,
  22. labelKey,
  23. labelStyle,
  24. mode = BUTTON_MODES.CONTAINED,
  25. onClick: onPress,
  26. style,
  27. type
  28. }: IProps) => {
  29. const { t } = useTranslation();
  30. const { DESTRUCTIVE, PRIMARY, SECONDARY, TERTIARY } = BUTTON_TYPES;
  31. const { CONTAINED, TEXT } = BUTTON_MODES;
  32. let buttonLabelStyles;
  33. let buttonStyles;
  34. let color;
  35. if (type === PRIMARY) {
  36. buttonLabelStyles = mode === TEXT
  37. ? styles.buttonLabelPrimaryText
  38. : styles.buttonLabelPrimary;
  39. color = mode === CONTAINED && BaseTheme.palette.action01;
  40. } else if (type === SECONDARY) {
  41. buttonLabelStyles = styles.buttonLabelSecondary;
  42. color = mode === CONTAINED && BaseTheme.palette.action02;
  43. } else if (type === DESTRUCTIVE) {
  44. buttonLabelStyles = mode === TEXT
  45. ? styles.buttonLabelDestructiveText
  46. : styles.buttonLabelDestructive;
  47. color = mode === CONTAINED && BaseTheme.palette.actionDanger;
  48. } else {
  49. color = buttonColor;
  50. buttonLabelStyles = styles.buttonLabel;
  51. }
  52. if (disabled) {
  53. buttonLabelStyles = styles.buttonLabelDisabled;
  54. buttonStyles = styles.buttonDisabled;
  55. } else {
  56. buttonStyles = styles.button;
  57. }
  58. if (type === TERTIARY) {
  59. if (disabled) {
  60. buttonLabelStyles = styles.buttonLabelTertiaryDisabled;
  61. }
  62. buttonLabelStyles = styles.buttonLabelTertiary;
  63. return (
  64. <TouchableHighlight
  65. accessibilityLabel = { accessibilityLabel }
  66. disabled = { disabled }
  67. onPress = { onPress }
  68. style = { [
  69. buttonStyles,
  70. style
  71. ] }>
  72. <Text
  73. style = { [
  74. buttonLabelStyles,
  75. labelStyle
  76. ] }>{ t(labelKey ?? '') }</Text>
  77. </TouchableHighlight>
  78. );
  79. }
  80. return (
  81. <NativePaperButton
  82. accessibilityLabel = { t(accessibilityLabel ?? '') }
  83. buttonColor = { color }
  84. children = { t(labelKey ?? '') }
  85. contentStyle = { [
  86. styles.buttonContent,
  87. contentStyle
  88. ] }
  89. disabled = { disabled }
  90. // @ts-ignore
  91. icon = { icon }
  92. labelStyle = { [
  93. buttonLabelStyles,
  94. labelStyle
  95. ] }
  96. mode = { mode }
  97. onPress = { onPress }
  98. style = { [
  99. buttonStyles,
  100. style
  101. ] } />
  102. );
  103. };
  104. export default Button;