Button.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. }) => {
  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: rippleColor
  28. },
  29. isIOS && getRadius(style, borderRadius),
  30. viewStyle
  31. ]}
  32. disabled={disabled}
  33. onPress={e => {
  34. if (onClick) onClick(e)
  35. }}
  36. onLongPress={e => {
  37. if (onLongClick) {
  38. onLongClick(e)
  39. //start = {}
  40. }
  41. }}
  42. onPressIn={(e) => {
  43. /*if (isSamsung && e.nativeEvent)
  44. start = e.nativeEvent*/
  45. }
  46. }
  47. onPressOut={e => {
  48. /*if (isSamsung && e.nativeEvent && start.timestamp) {
  49. end = e.nativeEvent
  50. var x = Math.abs(start.pageX - end.pageX)
  51. var y = Math.abs(start.pageY - end.pageY)
  52. //console.log(x, y, viewStyle.height)
  53. if (x < 50 && y < 10) {
  54. if (end.timestamp - start.timestamp > 600) { //长按
  55. //console.log('LongClick')
  56. if (onClick) onClick(e)
  57. } else { //click
  58. //console.log('Click')
  59. if (onClick) onClick(e)
  60. }
  61. }
  62. }*/
  63. }}
  64. android_ripple={ripple}>
  65. {iconLeft ?? <></>}
  66. { children ? children :
  67. <TextView style={mergeStyle(textStyle, textColor, textSize, disabled)}>{text}</TextView>
  68. }
  69. {iconRight ?? <></>}
  70. </Pressable>
  71. </View>
  72. );
  73. }
  74. const mergeStyle = (style, color, size, disabled) => {
  75. if (Array.isArray(style)) {
  76. let res = {}
  77. for (let s of style) {
  78. res = {...res, ...s};
  79. }
  80. style = res;
  81. }
  82. var s = Object.assign({}, style);
  83. if (disabled) {
  84. s.color = '#999';
  85. } else if (color) {
  86. s.color = color;
  87. }
  88. if (size) {
  89. s.fontSize = size;
  90. }
  91. return s;
  92. }
  93. const getRadius = (style, r) => {
  94. if (Array.isArray(style)) {
  95. let res = {}
  96. for (let s of style) {
  97. res = {...res, ...s};
  98. }
  99. style = res;
  100. }
  101. const s = {}
  102. if (style.borderTopLeftRadius !== undefined)
  103. s.borderTopLeftRadius = style.borderTopLeftRadius
  104. if (style.borderTopRightRadius !== undefined)
  105. s.borderTopRightRadius = style.borderTopRightRadius
  106. if (style.borderBottomLeftRadius !== undefined)
  107. s.borderBottomLeftRadius = style.borderBottomLeftRadius
  108. if (style.borderBottomRightRadius !== undefined)
  109. s.borderBottomRightRadius = style.borderBottomRightRadius
  110. if (style.borderRadius !== undefined)
  111. s.borderRadius = style.borderRadius
  112. if (Object.keys(s).length == 0 && r) {
  113. return $borderRadius(r)
  114. }
  115. return s;
  116. }
  117. const getElevation = (style, d, e, r) => {
  118. if (Array.isArray(style)) {
  119. let res = {}
  120. for (let s of style) {
  121. res = {...res, ...s};
  122. }
  123. style = res;
  124. }
  125. var s = Object.assign({}, style);
  126. s.padding = 0;
  127. if (!isIOS)
  128. s.overflow = 'hidden';
  129. s.flexDirection = 'row';
  130. if (style.borderRadius == undefined && r) {
  131. s.borderRadius = r;
  132. }
  133. if (!s.backgroundColor) {
  134. s.backgroundColor = styles.buttonView.backgroundColor
  135. }
  136. if (!d) {
  137. var ele = e ? e : style.elevation ? style.elevation : 0
  138. s = Object.assign(ElevationObject(ele), s)
  139. } else {
  140. s = Object.assign(ElevationObject(0), s)
  141. s.backgroundColor = '#CCC';
  142. }
  143. return s;
  144. }
  145. /**
  146. * Shadow阴影生成
  147. * @param {*} e 阴影大小
  148. * @returns 样式json
  149. */
  150. export const ElevationObject = (e) => {
  151. if (e) {
  152. return {
  153. elevation: e,
  154. shadowOpacity: .5,
  155. shadowColor: '#999999',
  156. shadowOffset: {width: 0, height: e},
  157. };
  158. } else {
  159. return {};
  160. }
  161. }
  162. export const ViewHeight = (height) => {
  163. return {
  164. flex: 1,
  165. height: height,
  166. alignItems: 'center',
  167. flexDirection: 'row',
  168. justifyContent: 'center',
  169. }
  170. }
  171. export const ViewHeightPadding = (height, padding=0) => {
  172. return {
  173. flex: 1,
  174. height: height,
  175. paddingLeft: padding,
  176. paddingRight: padding,
  177. alignItems: 'center',
  178. flexDirection: 'row',
  179. justifyContent: 'center',
  180. }
  181. }
  182. const styles = StyleSheet.create({
  183. buttonView: {
  184. //调整默认按钮颜色
  185. backgroundColor: colorAccent,
  186. //backgroundColor: colorPrimary
  187. },
  188. button: {
  189. flex: 1,
  190. height: 50,
  191. paddingLeft: 16,
  192. paddingRight: 16,
  193. alignItems: 'center',
  194. flexDirection: 'row',
  195. justifyContent: 'center',
  196. },
  197. buttonMini: {
  198. height: 42,
  199. borderRadius: 6,
  200. paddingLeft: 16,
  201. paddingRight: 16,
  202. alignItems: 'center',
  203. justifyContent: 'center',
  204. backgroundColor: colorAccent//colorPrimary
  205. },
  206. buttonText: {
  207. flex: 1,
  208. color: textButton,
  209. fontSize: 16,
  210. fontWeight: 'bold',
  211. textAlign: 'center'
  212. }
  213. });