UNPKG

@fruits-chain/react-native-xiaoshu

Version:
106 lines (99 loc) 4.07 kB
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 Color from 'color'; import isNil from 'lodash/isNil'; import React, { memo, useMemo } from 'react'; import { Text, TouchableOpacity, StyleSheet } from 'react-native'; import { getDefaultValue } from '../helpers'; import { useDebounceFn } from '../hooks'; import Loading from '../loading'; import Theme from '../theme'; import { varCreator, styleCreator } from './style'; /** * Button 按钮 * @description 按钮用于触发一个操作,如提交表单。 */ const Button = _ref => { let { children, style, text, textStyle, type = 'primary', danger = false, size = 'l', hairline = false, disabled = false, loading = false, loadingText, square = false, round = false, renderLeftIcon, color, textColor, onPressDebounceWait = 0, ...restProps } = _ref; const TOKENS = Theme.useThemeTokens(); const CV = Theme.createVar(TOKENS, varCreator); const STYLES = Theme.createStyle(CV, styleCreator); const { run: runOnPress } = useDebounceFn(restProps.onPress, { wait: onPressDebounceWait }); color = getDefaultValue(color, danger ? CV.button_danger_color : CV.button_primary_color); textColor = getDefaultValue(textColor, TOKENS.white); const [_backgroundColor, _borderColor, _textColor, _borderWidth] = useMemo(() => { switch (type) { case 'hazy': { const hazyColor = Color(color).lightness(CV.button_hazy_lightness).hex(); return [hazyColor, hazyColor, color, 0]; } case 'outline': { return [CV.button_ghost_background_color, CV.button_border_color, color, 1]; } case 'ghost': { return [CV.button_ghost_background_color, color, color, 1]; } case 'link': return [CV.button_ghost_background_color, CV.button_ghost_background_color, color, 0]; case 'primary': default: return [color, color, textColor, 0]; } }, [CV.button_border_color, CV.button_ghost_background_color, CV.button_hazy_lightness, color, textColor, type]); const buttonStyles = [STYLES.button, { height: CV[`button_${size}_height`], backgroundColor: _backgroundColor, borderColor: _borderColor, borderWidth: _borderWidth ? hairline ? StyleSheet.hairlineWidth : _borderWidth : _borderWidth }, STYLES[`button_${size}_padding_horizontal`], square ? STYLES.button_square : null, round ? STYLES.button_round : null, disabled ? STYLES.button_disabled : null, loading ? STYLES.button_loading : null, style]; const commonTextStyle = StyleSheet.flatten([STYLES.text, { fontSize: CV[`button_${size}_font_size`], color: _textColor, marginLeft: renderLeftIcon ? CV.button_icon_gap : 0 }]); const textStyleSummary = StyleSheet.flatten([commonTextStyle, textStyle]); const iconSize = CV[`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 }, !isNil(text) ? text : children)); return /*#__PURE__*/React.createElement(TouchableOpacity, _extends({}, restProps, { disabled: disabled || loading, style: buttonStyles, activeOpacity: CV.button_active_opacity, onPress: restProps.onPress ? onPressDebounceWait ? runOnPress : restProps.onPress : undefined }), contextJSX); }; export default /*#__PURE__*/memo(Button); //# sourceMappingURL=index.js.map