Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Button.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. return (
  62. <TouchableRipple
  63. accessibilityLabel = { accessibilityLabel }
  64. disabled = { disabled }
  65. onPress = { onPress }
  66. rippleColor = 'transparent'
  67. style = { [
  68. buttonStyles,
  69. style
  70. ] }>
  71. <Text
  72. style = { [
  73. buttonLabelStyles,
  74. labelStyle
  75. ] }>{ t(labelKey ?? '') }</Text>
  76. </TouchableRipple>
  77. );
  78. }
  79. return (
  80. <NativePaperButton
  81. accessibilityLabel = { t(accessibilityLabel ?? '') }
  82. children = { t(labelKey ?? '') }
  83. color = { color }
  84. contentStyle = { [
  85. styles.buttonContent,
  86. contentStyle
  87. ] }
  88. disabled = { disabled }
  89. // @ts-ignore
  90. icon = { icon }
  91. labelStyle = { [
  92. buttonLabelStyles,
  93. labelStyle
  94. ] }
  95. mode = { mode }
  96. onPress = { onPress }
  97. style = { [
  98. buttonStyles,
  99. style
  100. ] } />
  101. );
  102. };
  103. export default Button;