Button.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import * as React from 'react';
  2. import { View } from 'react-native';
  3. import {
  4. Paragraph,
  5. Button,
  6. Caption,
  7. withTheme,
  8. Text,
  9. Subheading,
  10. } from 'react-native-paper';
  11. function MyButton(props) {
  12. const {
  13. children,
  14. onPress,
  15. outline,
  16. text,
  17. size,
  18. type,
  19. theme,
  20. block,
  21. style,
  22. left,
  23. } = props;
  24. let { fontColor } = props;
  25. const { colors } = theme;
  26. let contentStyle = {};
  27. let color = '';
  28. let mode = 'contained';
  29. let childNode;
  30. let dark = true;
  31. switch (type) {
  32. case 'primary':
  33. color = colors.primary;
  34. break;
  35. case 'info':
  36. if (outline || text) {
  37. color = colors.info;
  38. } else {
  39. color = colors.lightInfo;
  40. fontColor = colors.info;
  41. }
  42. break;
  43. case 'danger':
  44. color = colors.error;
  45. break;
  46. default:
  47. color = colors.font;
  48. break;
  49. }
  50. if (outline) {
  51. mode = 'outline';
  52. dark = false;
  53. }
  54. if (text) {
  55. mode = 'text';
  56. dark = false;
  57. }
  58. switch (size) {
  59. case 'mini':
  60. contentStyle = { height: 22 };
  61. childNode = () => (
  62. <Text
  63. style={[
  64. { fontSize: 10, lineHeight: 10 },
  65. fontColor ? { color: fontColor } : { color: dark ? '#fff' : color },
  66. ]}
  67. >
  68. {children}
  69. </Text>
  70. );
  71. break;
  72. case 'small':
  73. contentStyle = { height: 30 };
  74. childNode = () => (
  75. // 14
  76. <Caption
  77. style={[
  78. { lineHeight: 14 },
  79. fontColor ? { color: fontColor } : { color: dark ? '#fff' : color },
  80. ]}
  81. >
  82. {children}
  83. </Caption>
  84. );
  85. break;
  86. case 'normal':
  87. contentStyle = { width: 120, height: 44 };
  88. childNode = () => (
  89. <Paragraph
  90. style={[
  91. { lineHeight: 16 },
  92. fontColor ? { color: fontColor } : { color: dark ? '#fff' : color },
  93. ]}
  94. >
  95. {children}
  96. </Paragraph>
  97. );
  98. break;
  99. case 'large':
  100. // 16
  101. contentStyle = { width: 120, height: 44 };
  102. if (left) {
  103. contentStyle = { height: 44, paddingRight: 30 };
  104. }
  105. childNode = () => (
  106. <Subheading
  107. style={[
  108. { lineHeight: 18 },
  109. fontColor ? { color: fontColor } : { color: dark ? '#fff' : color },
  110. ]}
  111. >
  112. {children}
  113. </Subheading>
  114. );
  115. break;
  116. default:
  117. contentStyle = { width: 120, height: 30 };
  118. childNode = () => (
  119. <Paragraph
  120. style={[
  121. { lineHeight: 16 },
  122. fontColor ? { color: fontColor } : { color: dark ? '#fff' : color },
  123. ]}
  124. >
  125. {children}
  126. </Paragraph>
  127. );
  128. break;
  129. }
  130. if (block) {
  131. delete contentStyle.width;
  132. return (
  133. <Button
  134. mode={mode}
  135. color={color}
  136. contentStyle={contentStyle}
  137. onPress={onPress}
  138. style={{
  139. elevation: 0,
  140. shadowOffset: {
  141. width: 0,
  142. height: 0,
  143. },
  144. shadowOpacity: 0,
  145. paddingVertical: 0,
  146. }}
  147. >
  148. {childNode()}
  149. </Button>
  150. );
  151. } else {
  152. return (
  153. <View style={{ ...style }}>
  154. <Button
  155. mode={mode}
  156. color={color}
  157. contentStyle={contentStyle}
  158. style={{
  159. elevation: 0,
  160. shadowOffset: {
  161. width: 0,
  162. height: 0,
  163. },
  164. shadowOpacity: 0,
  165. }}
  166. onPress={onPress}
  167. compact={left}
  168. >
  169. {childNode()}
  170. </Button>
  171. </View>
  172. );
  173. }
  174. }
  175. export default withTheme(MyButton);