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

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