Button.js 4.9 KB

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