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.1KB

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