選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Button.tsx 3.5KB

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