| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- import React from 'react';
- import { Pressable, StyleSheet, Text, View } from 'react-native';
- import TextView from './TextView';
- export default Button = ({
- style = styles.buttonView,
- text,
- textSize,
- textColor,
- textStyle = styles.buttonText,
- viewStyle = styles.button,
- children,
- onClick,
- onLongClick,
- disabled = false,
- elevation = 0,
- borderRadius = 40,
- iconLeft,
- iconRight
- }) => {
- //var start = {}, end = {};
- //const isSamsung = BRAND == 'samsung';
- return (
- <View style={getElevation(style, disabled, elevation, borderRadius)}>
- <Pressable
- style={({pressed }) => [
- pressed && isIOS && {
- backgroundColor: rippleColor
- },
- isIOS && getRadius(style, borderRadius),
- viewStyle
- ]}
- disabled={disabled}
- onPress={e => {
- if (onClick) onClick(e)
- }}
- onLongPress={e => {
- if (onLongClick) {
- onLongClick(e)
- //start = {}
- }
- }}
- onPressIn={(e) => {
- /*if (isSamsung && e.nativeEvent)
- start = e.nativeEvent*/
- }
- }
- onPressOut={e => {
- /*if (isSamsung && e.nativeEvent && start.timestamp) {
- end = e.nativeEvent
- var x = Math.abs(start.pageX - end.pageX)
- var y = Math.abs(start.pageY - end.pageY)
- //console.log(x, y, viewStyle.height)
- if (x < 50 && y < 10) {
- if (end.timestamp - start.timestamp > 600) { //长按
- //console.log('LongClick')
- if (onClick) onClick(e)
- } else { //click
- //console.log('Click')
- if (onClick) onClick(e)
- }
- }
- }*/
- }}
- android_ripple={ripple}>
- {iconLeft ?? <></>}
- { children ? children :
- <TextView style={mergeStyle(textStyle, textColor, textSize, disabled)}>{text}</TextView>
- }
- {iconRight ?? <></>}
- </Pressable>
- </View>
- );
- }
- const mergeStyle = (style, color, size, disabled) => {
- if (Array.isArray(style)) {
- let res = {}
- for (let s of style) {
- res = {...res, ...s};
- }
- style = res;
- }
- var s = Object.assign({}, style);
- if (disabled) {
- s.color = '#999';
- } else if (color) {
- s.color = color;
- }
- if (size) {
- s.fontSize = size;
- }
- return s;
- }
- const getRadius = (style, r) => {
- if (Array.isArray(style)) {
- let res = {}
- for (let s of style) {
- res = {...res, ...s};
- }
- style = res;
- }
- const s = {}
- if (style.borderTopLeftRadius !== undefined)
- s.borderTopLeftRadius = style.borderTopLeftRadius
- if (style.borderTopRightRadius !== undefined)
- s.borderTopRightRadius = style.borderTopRightRadius
- if (style.borderBottomLeftRadius !== undefined)
- s.borderBottomLeftRadius = style.borderBottomLeftRadius
- if (style.borderBottomRightRadius !== undefined)
- s.borderBottomRightRadius = style.borderBottomRightRadius
- if (style.borderRadius !== undefined)
- s.borderRadius = style.borderRadius
- if (Object.keys(s).length == 0 && r) {
- return $borderRadius(r)
- }
- return s;
- }
- const getElevation = (style, d, e, r) => {
- if (Array.isArray(style)) {
- let res = {}
- for (let s of style) {
- res = {...res, ...s};
- }
- style = res;
- }
- var s = Object.assign({}, style);
- s.padding = 0;
- if (!isIOS)
- s.overflow = 'hidden';
- s.flexDirection = 'row';
- if (style.borderRadius == undefined && r) {
- s.borderRadius = r;
- }
- if (!s.backgroundColor) {
- s.backgroundColor = styles.buttonView.backgroundColor
- }
- if (!d) {
- var ele = e ? e : style.elevation ? style.elevation : 0
- s = Object.assign(ElevationObject(ele), s)
- } else {
- s = Object.assign(ElevationObject(0), s)
- s.backgroundColor = '#CCC';
- }
- return s;
- }
- /**
- * Shadow阴影生成
- * @param {*} e 阴影大小
- * @returns 样式json
- */
- export const ElevationObject = (e) => {
- if (e) {
- return {
- elevation: e,
- shadowOpacity: .5,
- shadowColor: '#999999',
- shadowOffset: {width: 0, height: e},
- };
- } else {
- return {};
- }
- }
- export const ViewHeight = (height) => {
- return {
- flex: 1,
- height: height,
- alignItems: 'center',
- flexDirection: 'row',
- justifyContent: 'center',
- }
- }
- export const ViewHeightPadding = (height, padding=0) => {
- return {
- flex: 1,
- height: height,
- paddingLeft: padding,
- paddingRight: padding,
- alignItems: 'center',
- flexDirection: 'row',
- justifyContent: 'center',
- }
- }
- const styles = StyleSheet.create({
- buttonView: {
- //调整默认按钮颜色
- backgroundColor: colorAccent,
- //backgroundColor: colorPrimary
- },
- button: {
- flex: 1,
- height: 50,
- paddingLeft: 16,
- paddingRight: 16,
- alignItems: 'center',
- flexDirection: 'row',
- justifyContent: 'center',
- },
- buttonMini: {
- height: 42,
- borderRadius: 6,
- paddingLeft: 16,
- paddingRight: 16,
- alignItems: 'center',
- justifyContent: 'center',
- backgroundColor: colorAccent//colorPrimary
- },
- buttonText: {
- flex: 1,
- color: textButton,
- fontSize: 16,
- fontWeight: 'bold',
- textAlign: 'center'
- }
- });
|