Button.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import React, { useEffect } from 'react';
  2. import { Pressable, StyleSheet, Text, View } from 'react-native';
  3. export default Button = ({
  4. style = styles.buttonView,
  5. text,
  6. textSize,
  7. textColor,
  8. textStyle = styles.buttonText,
  9. viewStyle = styles.button,
  10. children,
  11. onClick,
  12. onLongClick,
  13. disabled = false,
  14. elevation = 0,
  15. borderRadius = 4
  16. }) => {
  17. var start = {}, end = {};
  18. const isSamsung = BRAND == 'samsung';
  19. return (
  20. <View style={getElevation(style, disabled, elevation, borderRadius)}>
  21. <Pressable
  22. style={({pressed }) => [
  23. pressed && isIOS && {
  24. backgroundColor: 'rgba(0,0,0,.2)'
  25. },
  26. isIOS && getRadius(style, borderRadius),
  27. viewStyle
  28. ]}
  29. disabled={disabled}
  30. onPress={e => {
  31. //if (!isSamsung && onClick)
  32. onClick(e)
  33. }}
  34. onLongPress={e => {
  35. if (onLongClick) {
  36. onLongClick(e)
  37. start = {}
  38. }
  39. }}
  40. onPressIn={(e) => {
  41. /*if (isSamsung && e.nativeEvent)
  42. start = e.nativeEvent*/
  43. }
  44. }
  45. onPressOut={e => {
  46. /*if (isSamsung && e.nativeEvent && start.timestamp) {
  47. end = e.nativeEvent
  48. var x = Math.abs(start.pageX - end.pageX)
  49. var y = Math.abs(start.pageY - end.pageY)
  50. //console.log(x, y, viewStyle.height)
  51. if (x < 50 && y < 10) {
  52. if (end.timestamp - start.timestamp > 600) { //长按
  53. //console.log('LongClick')
  54. if (onClick) onClick(e)
  55. } else { //click
  56. //console.log('Click')
  57. if (onClick) onClick(e)
  58. }
  59. }
  60. }*/
  61. }}
  62. android_ripple={ripple}>
  63. { children ? children :
  64. <Text style={mergeStyle(textStyle, textColor, textSize, disabled)}>{text}</Text>
  65. }
  66. </Pressable>
  67. </View>
  68. );
  69. }
  70. const mergeStyle = (style, color, size, disabled) => {
  71. if (Array.isArray(style)) {
  72. let res = {}
  73. for (let s of style) {
  74. res = {...res, ...s};
  75. }
  76. style = res;
  77. }
  78. var s = Object.assign({}, style);
  79. if (disabled) {
  80. s.color = '#999';
  81. } else if (color) {
  82. s.color = color;
  83. }
  84. if (size) {
  85. s.fontSize = size;
  86. }
  87. return s;
  88. }
  89. const getRadius = (style, r) => {
  90. if (Array.isArray(style)) {
  91. let res = {}
  92. for (let s of style) {
  93. res = {...res, ...s};
  94. }
  95. style = res;
  96. }
  97. const s = {}
  98. if (style.borderTopLeftRadius !== undefined)
  99. s.borderTopLeftRadius = style.borderTopLeftRadius
  100. if (style.borderTopRightRadius !== undefined)
  101. s.borderTopRightRadius = style.borderTopRightRadius
  102. if (style.borderBottomLeftRadius !== undefined)
  103. s.borderBottomLeftRadius = style.borderBottomLeftRadius
  104. if (style.borderBottomRightRadius !== undefined)
  105. s.borderBottomRightRadius = style.borderBottomRightRadius
  106. if (style.borderRadius !== undefined)
  107. s.borderRadius = style.borderRadius
  108. if (Object.keys(s).length == 0 && r) {
  109. return $borderRadius(r)
  110. }
  111. return s;
  112. }
  113. const getElevation = (style, d, e, r) => {
  114. if (Array.isArray(style)) {
  115. let res = {}
  116. for (let s of style) {
  117. res = {...res, ...s};
  118. }
  119. style = res;
  120. }
  121. var s = Object.assign({}, style);
  122. s.padding = 0;
  123. if (!isIOS)
  124. s.overflow = 'hidden';
  125. s.flexDirection = 'row';
  126. if (style.borderRadius == undefined && r) {
  127. s.borderRadius = r;
  128. }
  129. if (!s.backgroundColor) {
  130. s.backgroundColor = colorAccent
  131. }
  132. if (!d) {
  133. var ele = e ? e : style.elevation ? style.elevation : 0
  134. s = Object.assign(ElevationObject(ele), s)
  135. } else {
  136. s = Object.assign(ElevationObject(0), s)
  137. s.backgroundColor = '#CCC';
  138. }
  139. return s;
  140. }
  141. export const ElevationObject = (e) => {
  142. if (e) {
  143. return {
  144. elevation: e,
  145. shadowOpacity: .5,
  146. shadowColor: '#999999',
  147. shadowOffset: {width: 0, height: e},
  148. };
  149. } else {
  150. return {};
  151. }
  152. }
  153. export const ViewHeight = (height) => {
  154. return {
  155. flex: 1,
  156. height: height,
  157. alignItems: 'center',
  158. flexDirection: 'row',
  159. justifyContent: 'center',
  160. }
  161. }
  162. export const ViewHeightPadding = (height, padding=0) => {
  163. return {
  164. flex: 1,
  165. height: height,
  166. paddingLeft: padding,
  167. paddingRight: padding,
  168. alignItems: 'center',
  169. flexDirection: 'row',
  170. justifyContent: 'center',
  171. }
  172. }
  173. const styles = StyleSheet.create({
  174. buttonView: {
  175. backgroundColor: colorAccent
  176. },
  177. button: {
  178. flex: 1,
  179. height: 50,
  180. paddingLeft: 16,
  181. paddingRight: 16,
  182. alignItems: 'center',
  183. flexDirection: 'row',
  184. justifyContent: 'center',
  185. },
  186. buttonMini: {
  187. height: 42,
  188. borderRadius: 6,
  189. paddingLeft: 16,
  190. paddingRight: 16,
  191. alignItems: 'center',
  192. justifyContent: 'center',
  193. backgroundColor: colorAccent
  194. },
  195. buttonText: {
  196. color: '#333',
  197. fontSize: 16,
  198. fontWeight: 'bold',
  199. textAlign: 'center',
  200. }
  201. });