UNPKG

react-native-ui-lib

Version:

UI Components Library for React Native ###### Lateset version support RN44

339 lines (200 loc) • 9.43 kB
Object.defineProperty(exports,"__esModule",{value:true});var _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;};var _createClass=function(){function defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};}();var _react=require('react');var _react2=_interopRequireDefault(_react); var _reactNative=require('react-native'); var _lodash=require('lodash');var _lodash2=_interopRequireDefault(_lodash); var _commons=require('../../commons'); var _text=require('../text');var _text2=_interopRequireDefault(_text); var _touchableOpacity=require('../touchableOpacity');var _touchableOpacity2=_interopRequireDefault(_touchableOpacity); var _style=require('../../style');function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}function _objectWithoutProperties(obj,keys){var target={};for(var i in obj){if(keys.indexOf(i)>=0)continue;if(!Object.prototype.hasOwnProperty.call(obj,i))continue;target[i]=obj[i];}return target;}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _possibleConstructorReturn(self,call){if(!self){throw new ReferenceError("this hasn't been initialised - super() hasn't been called");}return call&&(typeof call==="object"||typeof call==="function")?call:self;}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass);}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass;}var Button=function(_BaseComponent){_inherits(Button,_BaseComponent);function Button(){_classCallCheck(this,Button);return _possibleConstructorReturn(this,(Button.__proto__||Object.getPrototypeOf(Button)).apply(this,arguments));}_createClass(Button,[{key:'generateStyles',value:function generateStyles() { this.styles=createStyles(this.props); }},{key:'getBackgroundColor',value:function getBackgroundColor() {var _props= this.props,disabled=_props.disabled,outline=_props.outline,link=_props.link,propsBackgroundColor=_props.backgroundColor;var stateBackgroundColor=this.state.backgroundColor; if(!outline&&!link){ if(disabled){ return _style.ThemeManager.CTADisabledColor; } return propsBackgroundColor||stateBackgroundColor; } return'transparent'; }},{key:'getLabelColor',value:function getLabelColor() {var _props2= this.props,link=_props2.link,outline=_props2.outline,disabled=_props2.disabled; var color=_style.ThemeManager.CTATextColor; if(link||outline){ color=_style.Colors.dark10; if(disabled){ return _style.ThemeManager.CTADisabledColor; } } color=this.props.color||this.extractColorValue()||color; return color; }},{key:'getLabelSizeStyle',value:function getLabelSizeStyle() {var _props3= this.props,size=_props3.size,link=_props3.link; var style={ paddingHorizontal:36}; if(size==='small'){ style=_extends({ paddingHorizontal:15}, _style.Typography.text80); }else if(size==='medium'){ style=_extends({ paddingHorizontal:24}, _style.Typography.text80); } if(link){ style.paddingHorizontal=0; } return style; }},{key:'getContainerSizeStyle',value:function getContainerSizeStyle() {var size=this.props.size; var style={ paddingVertical:16, minWidth:138}; if(size==='small'){ style={ paddingVertical:5, minWidth:74}; }else if(size==='medium'){ style={ paddingVertical:11, minWidth:125}; } return style; }},{key:'getOutlineStyle',value:function getOutlineStyle() {var _props4= this.props,outline=_props4.outline,outlineColor=_props4.outlineColor,link=_props4.link; if((outline||outlineColor)&&!link){ return{ borderWidth:_reactNative.StyleSheet.hairlineWidth, borderColor:outlineColor||_style.Colors.dark70}; } return undefined; }},{key:'getBorderRadiusStyle',value:function getBorderRadiusStyle() {var _props5= this.props,link=_props5.link,borderRadius=_props5.borderRadius,fullWidth=_props5.fullWidth; if(link||fullWidth){ return{borderRadius:0}; }else if(!_lodash2.default.isUndefined(borderRadius)){ return{borderRadius:borderRadius}; } }},{key:'getShadowStyle',value:function getShadowStyle() { var backgroundColor=this.getBackgroundColor();var enableShadow=this.props.enableShadow; if(enableShadow){ return[this.styles.shadowStyle,backgroundColor&&{shadowColor:backgroundColor}]; } }},{key:'renderIcon',value:function renderIcon() {var _props6= this.props,iconSource=_props6.iconSource,iconStyle=_props6.iconStyle,label=_props6.label,link=_props6.link,disabled=_props6.disabled; if(iconSource){ return( _react2.default.createElement(_reactNative.Image,{ source:iconSource, style:[ this.styles.icon, link&&disabled&&this.styles.iconDisabled, label&&this.styles.iconSpacing, iconStyle]})); } return null; }},{key:'renderLabel',value:function renderLabel() {var _props7= this.props,label=_props7.label,labelStyle=_props7.labelStyle; var sizeStyle=this.getLabelSizeStyle(); var typography=this.extractTypographyValue(); var color=this.getLabelColor(); if(label){ return( _react2.default.createElement(_text2.default,{ style:[ this.styles.text, color&&{color:color}, sizeStyle,_extends({}, typography), labelStyle], numberOfLines:1}, label)); } return null; }},{key:'render',value:function render() {var _props8= this.props,onPress=_props8.onPress,disabled=_props8.disabled,link=_props8.link,style=_props8.style,containerStyle=_props8.containerStyle,testID=_props8.testID,others=_objectWithoutProperties(_props8,['onPress','disabled','link','style','containerStyle','testID']); var shadowStyle=this.getShadowStyle();var margins=this.state.margins; var backgroundColor=this.getBackgroundColor(); var outlineStyle=this.getOutlineStyle(); var containerSizeStyle=this.getContainerSizeStyle(); var borderRadiusStyle=this.getBorderRadiusStyle(); return( _react2.default.createElement(_touchableOpacity2.default,_extends({ style:[ this.styles.container, shadowStyle, margins, containerStyle], activeOpacity:0.6, onPress:onPress, disabled:disabled, testID:testID}, others), _react2.default.createElement(_reactNative.View,{ style:[ this.styles.innerContainer, containerSizeStyle, backgroundColor&&{backgroundColor:backgroundColor}, borderRadiusStyle, outlineStyle, disabled&&this.styles.innerContainerDisabled, link&&this.styles.innerContainerLink, style]}, this.props.children, this.renderIcon(), this.renderLabel()))); }}]);return Button;}(_commons.BaseComponent);Button.displayName='Button';Button.propTypes=_extends({},_text2.default.propTypes,{label:_react.PropTypes.string,iconSource:_react.PropTypes.oneOfType([_react.PropTypes.object,_react.PropTypes.number]),iconStyle:_react.PropTypes.oneOfType([_react.PropTypes.object,_react.PropTypes.number,_react.PropTypes.array]),backgroundColor:_react.PropTypes.string,size:_react.PropTypes.oneOf(['small','medium','large']),borderRadius:_react.PropTypes.number,onPress:_react.PropTypes.func,disabled:_react.PropTypes.bool,outline:_react.PropTypes.bool,outlineColor:_react.PropTypes.string,link:_react.PropTypes.bool,containerStyle:_react.PropTypes.oneOfType([_react.PropTypes.object,_react.PropTypes.number,_react.PropTypes.array]),labelStyle:_react.PropTypes.oneOfType([_react.PropTypes.object,_react.PropTypes.number,_react.PropTypes.array]),fullWidth:_react.PropTypes.bool,enableShadow:_react.PropTypes.bool,testID:_react.PropTypes.string});Button.defaultProps={containerStyle:{},labelStyle:{},size:'large',outline:false};Button.sizes={small:'small',medium:'medium',large:'large'};exports.default=Button; function createStyles(_ref){var outline=_ref.outline,outlineColor=_ref.outlineColor,link=_ref.link,color=_ref.color; var showBorder=outline&&!link; return _reactNative.StyleSheet.create({ container:{ backgroundColor:'transparent'}, innerContainer:{ backgroundColor:_style.ThemeManager.CTABackgroundColor, borderWidth:showBorder?1:0, borderColor:showBorder?outlineColor:undefined, flexDirection:'row', justifyContent:'center', alignItems:'center', borderRadius:_style.BorderRadiuses.br100}, innerContainerDisabled:{ backgroundColor:_style.Colors.dark60}, innerContainerLink:{ minWidth:undefined, paddingHorizontal:undefined, paddingVertical:undefined, borderRadius:_style.BorderRadiuses.br0, backgroundColor:undefined}, shadowStyle:{ shadowColor:'#3082C8', shadowOffset:{height:5,width:0}, shadowOpacity:0.35, shadowRadius:9.5}, text:_extends({ backgroundColor:'transparent', flex:0, flexDirection:'row'}, _style.Typography.text70,{ fontWeight:'100'}), icon:{ tintColor:color}, iconDisabled:{ tintColor:_style.Colors.dark60}, iconSpacing:{ marginRight:7, marginBottom:2, paddingRight:0}}); }