Button.js 4.4 KB

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