@ant-design/react-native
Version:
基于蚂蚁金服移动设计规范的 React Native 组件库
1 lines • 6.7 kB
JavaScript
"use client";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");var _typeof3=require("@babel/runtime/helpers/typeof");Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _extends2=_interopRequireDefault(require("@babel/runtime/helpers/extends"));var _typeof2=_interopRequireDefault(require("@babel/runtime/helpers/typeof"));var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _react=_interopRequireWildcard(require("react"));var _reactNative=require("react-native");var _style=require("../style");var _view=_interopRequireDefault(require("../view"));var _index=_interopRequireDefault(require("./style/index"));function _getRequireWildcardCache(nodeInterop){if(typeof WeakMap!=="function")return null;var cacheBabelInterop=new WeakMap();var cacheNodeInterop=new WeakMap();return(_getRequireWildcardCache=function _getRequireWildcardCache(nodeInterop){return nodeInterop?cacheNodeInterop:cacheBabelInterop;})(nodeInterop);}function _interopRequireWildcard(obj,nodeInterop){if(!nodeInterop&&obj&&obj.__esModule){return obj;}if(obj===null||_typeof3(obj)!=="object"&&typeof obj!=="function"){return{default:obj};}var cache=_getRequireWildcardCache(nodeInterop);if(cache&&cache.has(obj)){return cache.get(obj);}var newObj={};var hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj){if(key!=="default"&&Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;if(desc&&(desc.get||desc.set)){Object.defineProperty(newObj,key,desc);}else{newObj[key]=obj[key];}}}newObj.default=obj;if(cache){cache.set(obj,newObj);}return newObj;}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 Button=function Button(props){var _useState=(0,_react.useState)(false),_useState2=(0,_slicedToArray2.default)(_useState,2),pressIn=_useState2[0],setPressIn=_useState2[1];var _useState3=(0,_react.useState)(false),_useState4=(0,_slicedToArray2.default)(_useState3,2),touchIt=_useState4[0],setTouchIt=_useState4[1];var _props$type=props.type,type=_props$type===void 0?'default':_props$type,_props$size=props.size,size=_props$size===void 0?'large':_props$size,_props$loading=props.loading,loading=_props$loading===void 0?false:_props$loading,_props$disabled=props.disabled,disabled=_props$disabled===void 0?false:_props$disabled,children=props.children,style=props.style,activeStyle=props.activeStyle,underlayColor=props.underlayColor,styles=props.styles,_props$onPress=props.onPress,onPress=_props$onPress===void 0?function(){}:_props$onPress,_props$onPressIn=props.onPressIn,onPressIn=_props$onPressIn===void 0?function(){}:_props$onPressIn,_props$onPressOut=props.onPressOut,onPressOut=_props$onPressOut===void 0?function(){}:_props$onPressOut,_props$onShowUnderlay=props.onShowUnderlay,onShowUnderlay=_props$onShowUnderlay===void 0?function(){}:_props$onShowUnderlay,_props$onHideUnderlay=props.onHideUnderlay,onHideUnderlay=_props$onHideUnderlay===void 0?function(){}:_props$onHideUnderlay,restProps=__rest(props,["type","size","loading","disabled","children","style","activeStyle","underlayColor","styles","onPress","onPressIn","onPressOut","onShowUnderlay","onHideUnderlay"]);var isActiveStyleEnabled=activeStyle!==false;var handlePressIn=function handlePressIn(event){setPressIn(true);onPressIn===null||onPressIn===void 0?void 0:onPressIn(event);if(isActiveStyleEnabled){setTouchIt(true);onShowUnderlay();}};var handlePressOut=function handlePressOut(event){setPressIn(false);onPressOut===null||onPressOut===void 0?void 0:onPressOut(event);if(isActiveStyleEnabled){setTouchIt(false);onHideUnderlay();}};var getDynamicStyle=(0,_react.useCallback)(function(_styles){var wrapperStyle=[_styles.wrapperStyle,_styles["".concat(size,"Raw")]||{},_styles["".concat(type,"Raw")]||_styles.defaultRaw||{}];if(disabled){wrapperStyle.push(_styles["".concat(type,"DisabledRaw")]||{});}if(style){wrapperStyle.push(style);}return wrapperStyle.filter(Boolean);},[disabled,size,style,type]);var getDynamicTextStyle=(0,_react.useCallback)(function(pressed,_styles){var textStyles=[_styles["".concat(size,"RawText")]||{},_styles["".concat(type,"RawText")]||_styles.defaultRawText||{},_styles.rawText];if(disabled){textStyles.push(_styles["".concat(type,"DisabledRawText")]||{});}if(pressed&&touchIt){textStyles.push(activeStyle||_styles["".concat(type,"HighlightText")]||{});}return textStyles.filter(Boolean);},[activeStyle,disabled,size,touchIt,type]);var getDynamicIndicatorStyle=(0,_react.useCallback)(function(pressed,_styles){var styleKey=disabled?"".concat(type,"DisabledRawText"):pressed&&activeStyle!==false?"".concat(type,"HighlightText"):"".concat(type,"RawText");var indicatorStyle=_styles[styleKey];return indicatorStyle&&(0,_typeof2.default)(indicatorStyle)==='object'&&'color'in indicatorStyle?indicatorStyle.color:_styles.defaultRawText.color;},[activeStyle,disabled,type]);var getDynamicUnderlayStyle=(0,_react.useCallback)(function(pressed,_styles){var containerStyle=[_styles.underlayStyle,_styles["".concat(size,"UnderlayContainerRaw")]||{},_styles["".concat(type,"UnderlayContainerRaw")]||_styles.defaultUnderlayContainerRaw||{}];if(pressed){containerStyle.push(activeStyle||_styles["".concat(type,"Highlight")]||{});}if(pressed&&underlayColor){containerStyle.push({backgroundColor:underlayColor});}return containerStyle.filter(Boolean);},[activeStyle,size,type,underlayColor]);return _react.default.createElement(_style.WithTheme,{themeStyles:_index.default,styles:styles},function(_styles){return _react.default.createElement(_reactNative.Pressable,(0,_extends2.default)({accessibilityRole:"button",accessibilityState:{disabled:!!disabled}},restProps,{style:getDynamicStyle(_styles),disabled:disabled,android_ripple:undefined,onPress:onPress,onPressIn:handlePressIn,onPressOut:handlePressOut}),_react.default.createElement(_reactNative.View,{style:getDynamicUnderlayStyle(touchIt,_styles)},_react.default.createElement(_reactNative.View,{style:_styles.container},loading?_react.default.createElement(_reactNative.ActivityIndicator,{style:_styles.indicator,animating:true,color:getDynamicIndicatorStyle(pressIn,_styles),size:"small"}):null,_react.default.createElement(_view.default,{style:getDynamicTextStyle(pressIn,_styles)},children))));});};var _default=Button;exports.default=_default;