Button.js 3.1 KB

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