@fruits-chain/react-native-xiaoshu
Version:
React Native UI library
80 lines (74 loc) • 3.23 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import React, { memo } from 'react';
import { Text, TouchableOpacity, StyleSheet } from 'react-native';
import { useTheme, widthStyle } from '../theme';
import Loading from '../loading';
import { createStyles } from './style';
/**
* Button 按钮
* @description 按钮用于触发一个操作,如提交表单。
*/
const Button = _ref => {
let {
children,
style,
text,
textStyle,
type = 'default',
size = 'default',
ghost = false,
hairline = false,
disabled = false,
loading = false,
loadingText,
square = false,
round = false,
renderLeftIcon,
color,
textColor,
...otherProps
} = _ref;
const THEME_VAR = useTheme();
const STYLES = widthStyle(THEME_VAR, createStyles);
const commonButtonStyle = StyleSheet.flatten([STYLES.button, {
backgroundColor: THEME_VAR[`button_${type}_background_color`] || THEME_VAR.button_default_background_color,
borderColor: THEME_VAR[`button_${type}_border_color`] || THEME_VAR.button_default_border_color,
height: THEME_VAR[`button_${size}_height`] || THEME_VAR.button_default_font_size
}, STYLES[`button_${size}`], hairline ? STYLES.button_border_width_hairline : null, square ? STYLES.button_square : null, round ? STYLES.button_round : null, color ? {
backgroundColor: color,
borderColor: color
} : null, disabled ? STYLES.button_disabled : null]);
const commonTextStyle = StyleSheet.flatten([STYLES.text, {
color: THEME_VAR[`button_${type}_color`] || THEME_VAR.button_default_color,
fontSize: THEME_VAR[`button_${type}_font_size`] || THEME_VAR.button_default_font_size
}, color || textColor ? {
color: textColor || '#fff'
} : null]);
if (ghost) {
commonButtonStyle.backgroundColor = THEME_VAR.button_ghost_background_color;
commonTextStyle.color = commonButtonStyle.borderColor;
if (type === 'default') {
commonButtonStyle.borderColor = '#BEC6D2';
commonTextStyle.color = THEME_VAR.primary;
}
}
const textStyleSummary = StyleSheet.flatten([commonTextStyle, textStyle]);
const iconSize = THEME_VAR[`button_${size}_loading_size`] || 24;
const iconColor = textStyleSummary.color;
const contextJSX = loading ? /*#__PURE__*/React.createElement(Loading, {
type: "spinner",
color: iconColor,
size: iconSize,
textSize: textStyleSummary.fontSize
}, loadingText) : /*#__PURE__*/React.createElement(React.Fragment, null, renderLeftIcon ? renderLeftIcon(iconColor, iconSize) : null, /*#__PURE__*/React.createElement(Text, {
style: textStyleSummary,
numberOfLines: 1
}, text || children));
return /*#__PURE__*/React.createElement(TouchableOpacity, _extends({
disabled: disabled || loading,
style: [commonButtonStyle, style],
activeOpacity: THEME_VAR.button_active_opacity
}, otherProps), contextJSX);
};
export default /*#__PURE__*/memo(Button);
//# sourceMappingURL=index.js.map