UNPKG

@arpitbhalla/rneui-base-dev

Version:
211 lines (210 loc) 11.7 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Button = void 0; var color_1 = __importDefault(require("color")); var react_1 = __importStar(require("react")); var react_native_1 = require("react-native"); var helpers_1 = require("../helpers"); var Icon_1 = require("../Icon"); var defaultLoadingProps = function (type, theme) { var _a; return ({ color: type === 'solid' ? 'white' : (_a = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _a === void 0 ? void 0 : _a.primary, size: 'small', }); }; var positionStyle = { top: 'column', bottom: 'column-reverse', left: 'row', right: 'row-reverse', }; var Button = function (_a) { var _b, _c, _d, _e, _f, _g, _h; var TouchableComponent = _a.TouchableComponent, containerStyle = _a.containerStyle, _j = _a.onPress, onPress = _j === void 0 ? function () { } : _j, buttonStyle = _a.buttonStyle, _k = _a.type, type = _k === void 0 ? 'solid' : _k, _l = _a.loading, loading = _l === void 0 ? false : _l, loadingStyle = _a.loadingStyle, passedLoadingProps = _a.loadingProps, _m = _a.size, size = _m === void 0 ? 'md' : _m, _o = _a.radius, radius = _o === void 0 ? 'xs' : _o, _p = _a.uppercase, uppercase = _p === void 0 ? false : _p, _q = _a.color, buttonColor = _q === void 0 ? 'primary' : _q, _r = _a.title, title = _r === void 0 ? '' : _r, titleProps = _a.titleProps, passedTitleStyle = _a.titleStyle, icon = _a.icon, iconContainerStyle = _a.iconContainerStyle, _s = _a.iconRight, iconRight = _s === void 0 ? false : _s, _t = _a.disabled, disabled = _t === void 0 ? false : _t, disabledStyle = _a.disabledStyle, disabledTitleStyle = _a.disabledTitleStyle, _u = _a.raised, raised = _u === void 0 ? false : _u, linearGradientProps = _a.linearGradientProps, _v = _a.ViewComponent, ViewComponent = _v === void 0 ? react_native_1.View : _v, _w = _a.theme, theme = _w === void 0 ? helpers_1.defaultTheme : _w, _x = _a.iconPosition, iconPosition = _x === void 0 ? 'left' : _x, _y = _a.children, children = _y === void 0 ? title : _y, rest = __rest(_a, ["TouchableComponent", "containerStyle", "onPress", "buttonStyle", "type", "loading", "loadingStyle", "loadingProps", "size", "radius", "uppercase", "color", "title", "titleProps", "titleStyle", "icon", "iconContainerStyle", "iconRight", "disabled", "disabledStyle", "disabledTitleStyle", "raised", "linearGradientProps", "ViewComponent", "theme", "iconPosition", "children"]); (0, react_1.useEffect)(function () { if (linearGradientProps && !ViewComponent) { console.warn("You need to pass a ViewComponent to use linearGradientProps !\nExample: ViewComponent={require('react-native-linear-gradient')}"); } }); var handleOnPress = (0, react_1.useCallback)(function (evt) { if (!loading && !disabled) { onPress(evt); } }, [loading, onPress, disabled]); var TouchableComponentInternal = TouchableComponent || react_native_1.Platform.select({ android: linearGradientProps ? react_native_1.TouchableOpacity : react_native_1.TouchableNativeFeedback, default: react_native_1.TouchableOpacity, }); var titleStyle = (0, react_1.useMemo)(function () { var _a, _b; return react_native_1.StyleSheet.flatten([ { color: type === 'solid' ? 'white' : (_a = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _a === void 0 ? void 0 : _a.primary, }, uppercase && { textTransform: 'uppercase' }, styles.title, passedTitleStyle, disabled && { color: (0, helpers_1.color)((_b = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _b === void 0 ? void 0 : _b.disabled).darken(0.3).string(), }, disabled && disabledTitleStyle, ]); }, [ disabled, disabledTitleStyle, passedTitleStyle, (_b = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _b === void 0 ? void 0 : _b.disabled, (_c = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _c === void 0 ? void 0 : _c.primary, type, uppercase, ]); var background = react_native_1.Platform.OS === 'android' && react_native_1.Platform.Version >= 21 ? react_native_1.TouchableNativeFeedback.Ripple((0, color_1.default)((_d = titleStyle === null || titleStyle === void 0 ? void 0 : titleStyle.color) === null || _d === void 0 ? void 0 : _d.toString()).alpha(0.32).rgb().string(), false) : undefined; var loadingProps = (0, react_1.useMemo)(function () { return (__assign(__assign({}, defaultLoadingProps(type, theme)), passedLoadingProps)); }, [passedLoadingProps, theme, type]); var accessibilityState = (0, react_1.useMemo)(function () { return ({ disabled: !!disabled, busy: !!loading, }); }, [disabled, loading]); var borderRadius = (0, react_1.useMemo)(function () { var _a; return Number((_a = theme.spacing[radius]) !== null && _a !== void 0 ? _a : (radius || '0')) || 0; }, [radius, theme]); return (react_1.default.createElement(react_native_1.View, { style: [ styles.container, { borderRadius: borderRadius }, containerStyle, raised && !disabled && type !== 'clear' && styles.raised, ], testID: "RNE_BUTTON_WRAPPER" }, react_1.default.createElement(TouchableComponentInternal, __assign({ onPress: handleOnPress, delayPressIn: 0, activeOpacity: 0.3, accessibilityRole: "button", accessibilityState: accessibilityState, disabled: disabled, background: background }, rest), react_1.default.createElement(ViewComponent, __assign({}, linearGradientProps, { style: react_native_1.StyleSheet.flatten([ styles.button, { padding: theme.spacing[size], paddingHorizontal: theme.spacing[size] + 2, borderRadius: borderRadius, flexDirection: positionStyle[iconRight ? 'right' : iconPosition] || 'row', backgroundColor: type === 'solid' ? theme.colors[buttonColor] || buttonColor || ((_e = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _e === void 0 ? void 0 : _e.primary) : 'transparent', borderColor: (_f = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _f === void 0 ? void 0 : _f.primary, borderWidth: type === 'outline' ? react_native_1.StyleSheet.hairlineWidth : 0, }, buttonStyle, disabled && type === 'solid' && { backgroundColor: (_g = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _g === void 0 ? void 0 : _g.disabled, }, disabled && type === 'outline' && { borderColor: (0, helpers_1.color)((_h = theme === null || theme === void 0 ? void 0 : theme.colors) === null || _h === void 0 ? void 0 : _h.disabled) .darken(0.3) .string(), }, disabled && disabledStyle, ]) }), loading && (react_1.default.createElement(react_native_1.ActivityIndicator, __assign({ style: react_native_1.StyleSheet.flatten([styles.loading, loadingStyle]), color: loadingProps.color, size: loadingProps.size }, loadingProps))), !loading && icon && (0, helpers_1.renderNode)(Icon_1.Icon, icon, { containerStyle: react_native_1.StyleSheet.flatten([ styles.iconContainer, iconContainerStyle, ]), }), !loading && react_1.default.Children.toArray(children).map(function (child, index) { return (react_1.default.createElement(react_1.default.Fragment, { key: index }, typeof child === 'string' ? (0, helpers_1.renderNode)(react_native_1.Text, child, __assign({ style: __assign({}, titleStyle) }, titleProps)) : child)); }))))); }; exports.Button = Button; var styles = react_native_1.StyleSheet.create({ button: { flexDirection: 'row', justifyContent: 'center', alignItems: 'center', padding: helpers_1.defaultTheme.spacing.md, paddingHorizontal: helpers_1.defaultTheme.spacing.lg, }, container: { overflow: 'hidden', }, title: __assign({ fontSize: 16, textAlign: 'center', paddingVertical: 1 }, react_native_1.Platform.select({ android: { fontFamily: 'sans-serif-medium', }, default: { fontSize: 18, }, })), iconContainer: { marginHorizontal: 5, }, raised: __assign({ backgroundColor: '#fff', overflow: 'visible' }, react_native_1.Platform.select({ android: { elevation: 4, }, default: { shadowColor: 'rgba(0,0,0, .4)', shadowOffset: { height: 1, width: 1 }, shadowOpacity: 1, shadowRadius: 1, }, })), loading: { marginVertical: 2, }, }); exports.Button.displayName = 'Button';