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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* eslint-disable lines-around-comment */
  2. import React from 'react';
  3. import { useTranslation } from 'react-i18next';
  4. import {
  5. Button as NativePaperButton,
  6. Text,
  7. TouchableRipple
  8. } from 'react-native-paper';
  9. import { BUTTON_MODES, BUTTON_TYPES } from '../../constants';
  10. import BaseTheme from '../BaseTheme.native';
  11. import { ButtonProps } from '../types';
  12. import styles from './buttonStyles';
  13. export interface IButtonProps extends ButtonProps {
  14. color?: string;
  15. labelStyle?: Object | undefined;
  16. style?: Object | undefined;
  17. }
  18. const Button: React.FC<IButtonProps> = ({
  19. accessibilityLabel,
  20. color: buttonColor,
  21. disabled,
  22. icon,
  23. labelKey,
  24. labelStyle,
  25. onClick: onPress,
  26. style,
  27. type
  28. }: IButtonProps) => {
  29. const { t } = useTranslation();
  30. const { CONTAINED } = BUTTON_MODES;
  31. const { DESTRUCTIVE, PRIMARY, SECONDARY, TERTIARY } = BUTTON_TYPES;
  32. let buttonLabelStyles;
  33. let buttonStyles;
  34. let color;
  35. let mode;
  36. if (type === PRIMARY) {
  37. buttonLabelStyles = styles.buttonLabelPrimary;
  38. color = BaseTheme.palette.action01;
  39. mode = CONTAINED;
  40. } else if (type === SECONDARY) {
  41. buttonLabelStyles = styles.buttonLabelSecondary;
  42. color = BaseTheme.palette.action02;
  43. mode = CONTAINED;
  44. } else if (type === DESTRUCTIVE) {
  45. color = BaseTheme.palette.actionDanger;
  46. buttonLabelStyles = styles.buttonLabelDestructive;
  47. mode = CONTAINED;
  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. return (
  60. <TouchableRipple
  61. accessibilityLabel = { accessibilityLabel }
  62. disabled = { disabled }
  63. onPress = { onPress }
  64. rippleColor = 'transparent'
  65. style = { [
  66. buttonStyles,
  67. style
  68. ] }>
  69. <Text
  70. style = { [
  71. buttonLabelStyles,
  72. labelStyle
  73. ] }>{ t(labelKey ?? '') }</Text>
  74. </TouchableRipple>
  75. );
  76. }
  77. return (
  78. <NativePaperButton
  79. accessibilityLabel = { t(accessibilityLabel ?? '') }
  80. children = { t(labelKey ?? '') }
  81. color = { color }
  82. disabled = { disabled }
  83. // @ts-ignore
  84. icon = { icon }
  85. labelStyle = { [
  86. buttonLabelStyles,
  87. labelStyle
  88. ] }
  89. mode = { mode }
  90. onPress = { onPress }
  91. style = { [
  92. buttonStyles,
  93. style
  94. ] } />
  95. );
  96. };
  97. export default Button;