@fruits-chain/react-native-xiaoshu
Version:
🌈 React Native UI library
136 lines (135 loc) • 5.92 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _color = _interopRequireDefault(require("color"));
var _isNil = _interopRequireDefault(require("lodash/isNil"));
var _isUndefined = _interopRequireDefault(require("lodash/isUndefined"));
var _noop = _interopRequireDefault(require("lodash/noop"));
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
var _index = _interopRequireDefault(require("../flex/index.js"));
var _index2 = require("../helpers/index.js");
var _index3 = require("../hooks/index.js");
var _index4 = _interopRequireDefault(require("../loading/index.js"));
var _index5 = _interopRequireDefault(require("../theme/index.js"));
var _style = require("./style.js");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
/**
* Button 按钮
* @description 按钮用于触发一个操作,如提交表单。
*/const Button = ({
children,
style,
theme,
text,
subtext,
textStyle,
type = 'primary',
danger = false,
size = 'l',
hairline = false,
disabled = false,
loading = false,
loadingText,
loadingIcon,
square = false,
round = false,
renderLeftIcon,
color,
textColor,
onPressDebounceWait = 0,
...restProps
}) => {
const [CV, STYLES] = _index5.default.useStyle({
varCreator: _style.varCreator,
styleCreator: _style.styleCreator,
theme
});
const {
run: runOnPress
} = (0, _index3.useDebounceFn)(restProps.onPress || _noop.default, {
wait: onPressDebounceWait,
leading: true,
trailing: false
});
color = (0, _index2.getDefaultValue)(color, danger ? CV.button_danger_color : CV.button_primary_color);
textColor = (0, _index2.getDefaultValue)(textColor, CV.button_text_color);
const [_backgroundColor, _borderColor, _textColor, _borderWidth] = (0, _react.useMemo)(() => {
switch (type) {
case 'hazy':
{
const hazyColor = (0, _color.default)(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, STYLES.button_column, {
height: CV[`button_${size}_height`],
backgroundColor: _backgroundColor,
borderColor: _borderColor,
borderWidth: _borderWidth ? hairline ? _reactNative.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 = _reactNative.StyleSheet.flatten([STYLES.text, {
fontSize: CV[`button_${size}_font_size`],
color: _textColor,
marginLeft: renderLeftIcon ? CV.button_icon_gap : 0
}]);
const textStyleSummary = _reactNative.StyleSheet.flatten([commonTextStyle, textStyle]);
const iconSize = CV[`button_${size}_loading_size`] || textStyleSummary.fontSize;
const iconColor = textStyleSummary.color;
const contextJSX = loading ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_index4.default, {
type: "spinner",
color: iconColor,
size: iconSize,
textSize: textStyleSummary.fontSize,
loadingIcon: loadingIcon,
children: (0, _isUndefined.default)(loadingText) ? text : loadingText
}) : /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_index.default, {
direction: "row",
align: "center",
justify: "center",
children: [renderLeftIcon ? renderLeftIcon(iconColor, iconSize) : null, /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.Text, {
style: textStyleSummary,
numberOfLines: 1,
children: !(0, _isNil.default)(text) ? text : children
})]
}), (0, _index2.renderTextLikeJSX)(subtext, [{
color: iconColor,
lineHeight: CV.button_subtext_line_height,
fontSize: CV.button_subtext_font_size,
opacity: CV.button_subtext_opacity
}], {
numberOfLines: 1
})]
});
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.TouchableOpacity, {
accessibilityRole: "button",
...restProps,
disabled: disabled || loading,
style: buttonStyles,
activeOpacity: CV.button_active_opacity,
onPress: restProps.onPress ? onPressDebounceWait ? runOnPress : restProps.onPress : undefined,
children: contextJSX
});
};
var _default = exports.default = /*#__PURE__*/(0, _react.memo)(Button);
//# sourceMappingURL=button.js.map
;