Button.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import React from 'react';
  2. import { Pressable, StyleSheet, Text, View } from 'react-native';
  3. import TextView from './TextView';
  4. export default Button = ({
  5. style = styles.buttonView,
  6. text,
  7. textSize,
  8. textColor,
  9. textStyle = styles.buttonText,
  10. viewStyle = styles.button,
  11. children,
  12. onClick,
  13. onLongClick,
  14. disabled = false,
  15. elevation = 0,
  16. borderRadius = 40,
  17. iconLeft,
  18. iconRight,
  19. numberOfLines=1
  20. }) => {
  21. //var start = {}, end = {};
  22. //const isSamsung = BRAND == 'samsung';
  23. return (
  24. <View style={getElevation(style, disabled, elevation, borderRadius)}>
  25. <Pressable
  26. style={({pressed }) => [
  27. pressed && isIOS && {
  28. backgroundColor: rippleColor
  29. },
  30. isIOS && getRadius(style, borderRadius),
  31. viewStyle
  32. ]}
  33. disabled={disabled}
  34. onPress={e => {
  35. if (onClick) 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. {iconLeft ?? <></>}
  67. { children ? children :
  68. <TextView style={mergeStyle(textStyle, textColor, textSize, disabled)} numberOfLines={numberOfLines}>{text}</TextView>
  69. }
  70. {iconRight ?? <></>}
  71. </Pressable>
  72. </View>
  73. );
  74. }
  75. const mergeStyle = (style, color, size, disabled) => {
  76. if (Array.isArray(style)) {
  77. let res = {}
  78. for (let s of style) {
  79. res = {...res, ...s};
  80. }
  81. style = res;
  82. }
  83. var s = Object.assign({}, style);
  84. if (disabled) {
  85. s.color = '#999';
  86. } else if (color) {
  87. s.color = color;
  88. }
  89. if (size) {
  90. s.fontSize = size;
  91. }
  92. return s;
  93. }
  94. const getRadius = (style, r) => {
  95. if (Array.isArray(style)) {
  96. let res = {}
  97. for (let s of style) {
  98. res = {...res, ...s};
  99. }
  100. style = res;
  101. }
  102. const s = {}
  103. if (style.borderTopLeftRadius !== undefined)
  104. s.borderTopLeftRadius = style.borderTopLeftRadius
  105. if (style.borderTopRightRadius !== undefined)
  106. s.borderTopRightRadius = style.borderTopRightRadius
  107. if (style.borderBottomLeftRadius !== undefined)
  108. s.borderBottomLeftRadius = style.borderBottomLeftRadius
  109. if (style.borderBottomRightRadius !== undefined)
  110. s.borderBottomRightRadius = style.borderBottomRightRadius
  111. if (style.borderRadius !== undefined)
  112. s.borderRadius = style.borderRadius
  113. if (Object.keys(s).length == 0 && r) {
  114. return $borderRadius(r)
  115. }
  116. return s;
  117. }
  118. const getElevation = (style, d, e, r) => {
  119. if (Array.isArray(style)) {
  120. let res = {}
  121. for (let s of style) {
  122. res = {...res, ...s};
  123. }
  124. style = res;
  125. }
  126. var s = Object.assign({}, style);
  127. s.padding = 0;
  128. if (!isIOS)
  129. s.overflow = 'hidden';
  130. s.flexDirection = 'row';
  131. if (style.borderRadius == undefined && r) {
  132. s.borderRadius = r;
  133. }
  134. if (!s.backgroundColor) {
  135. s.backgroundColor = styles.buttonView.backgroundColor
  136. }
  137. if (!d) {
  138. var ele = e ? e : style.elevation ? style.elevation : 0
  139. s = Object.assign(ElevationObject(ele), s)
  140. } else {
  141. s = Object.assign(ElevationObject(0), s)
  142. s.backgroundColor = '#CCC';
  143. }
  144. return s;
  145. }
  146. /**
  147. * Shadow阴影生成
  148. * @param {*} e 阴影大小
  149. * @returns 样式json
  150. */
  151. export const ElevationObject = (e) => {
  152. if (e) {
  153. return {
  154. elevation: e,
  155. shadowOpacity: .5,
  156. shadowColor: '#999999',
  157. shadowOffset: {width: 0, height: e},
  158. };
  159. } else {
  160. return {};
  161. }
  162. }
  163. export const ViewHeight = (height) => {
  164. return {
  165. flex: 1,
  166. height: height,
  167. alignItems: 'center',
  168. flexDirection: 'row',
  169. justifyContent: 'center',
  170. }
  171. }
  172. export const ViewHeightPadding = (height, padding=0) => {
  173. return {
  174. flex: 1,
  175. height: height,
  176. paddingLeft: padding,
  177. paddingRight: padding,
  178. alignItems: 'center',
  179. flexDirection: 'row',
  180. justifyContent: 'center',
  181. }
  182. }
  183. const styles = StyleSheet.create({
  184. buttonView: {
  185. //调整默认按钮颜色
  186. backgroundColor: colorAccent,
  187. //backgroundColor: colorPrimary
  188. },
  189. button: {
  190. flex: 1,
  191. height: 50,
  192. paddingLeft: 16,
  193. paddingRight: 16,
  194. alignItems: 'center',
  195. flexDirection: 'row',
  196. justifyContent: 'center',
  197. },
  198. buttonMini: {
  199. height: 42,
  200. borderRadius: 6,
  201. paddingLeft: 16,
  202. paddingRight: 16,
  203. alignItems: 'center',
  204. justifyContent: 'center',
  205. backgroundColor: colorAccent//colorPrimary
  206. },
  207. buttonText: {
  208. flex: 1,
  209. color: textButton,
  210. fontSize: 16,
  211. fontWeight: 'bold',
  212. textAlign: 'center',
  213. textTransform: 'uppercase'
  214. }
  215. });