UNPKG

weex-nuke

Version:

基于 Rax 、Weex 的高性能组件体系 ~~

167 lines (133 loc) 6.84 kB
/** @jsx createElement */ 'use strict'; 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 _rax = require('rax'); var _nukeText = require('nuke-text'); var _nukeText2 = _interopRequireDefault(_nukeText); var _nukeEnv = require('nuke-env'); var _nukeThemeProvider = require('nuke-theme-provider'); var _nukeIcon = require('nuke-icon'); var _nukeIcon2 = _interopRequireDefault(_nukeIcon); var _nukeTouchable = require('nuke-touchable'); var _nukeTouchable2 = _interopRequireDefault(_nukeTouchable); var _styles = require('../styles'); var _styles2 = _interopRequireDefault(_styles); 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; } // 下面的属性需要在Text上手工继承 var TextAttrArr = ['color', 'fontSize', 'fontStyle', 'fontWeight', 'lineHeight']; var ActiveAttr = ['color', 'backgroundColor', 'borderColor']; var LoadingPic = 'https://img.alicdn.com/tfs/TB179d4LpXXXXaUXVXXXXXXXXXX-32-32.gif?getAvatar=avatar'; var Button = function (_Component) { _inherits(Button, _Component); function Button(props) { _classCallCheck(this, Button); var _this = _possibleConstructorReturn(this, (Button.__proto__ || Object.getPrototypeOf(Button)).call(this, props)); _this.onPress = _this.onPress.bind(_this); _this.onTextPress = _this.onTextPress.bind(_this); return _this; } _createClass(Button, [{ key: 'onPress', value: function onPress(e) { if (this.props.disabled) return; this.props.onPress(e); } }, { key: 'onTextPress', value: function onTextPress(e) { if (this.props.disabled) return; if (_nukeEnv.isWeb) { e.stopPropagation(); } this.props.onPress(e); } }, { key: 'render', value: function render() { var _props = this.props, size = _props.size, shape = _props.shape, type = _props.type, style = _props.style, disabled = _props.disabled, block = _props.block, loading = _props.loading, icon = _props.icon, text = _props.text, warning = _props.warning, children = _props.children, others = _objectWithoutProperties(_props, ['size', 'shape', 'type', 'style', 'disabled', 'block', 'loading', 'icon', 'text', 'warning', 'children']); var s = this.props.themeStyle; var btnStyle = {}; var textStyle = {}; var iconSrc = loading ? LoadingPic : icon || null; var isWarning = shape === 'warning' || warning; // 伪类有默认样式了,如果伪类的样式没传,需要和原来的一样 ActiveAttr.forEach(function (item) { if (style && style[item] && !style[item + ':active']) { style[item + ':active'] = style[item]; } }); btnStyle = Object.assign({}, s.base, type ? s['' + type] : {}, isWarning ? s['warning-' + type] : {}, disabled && type ? s[type + '-disabled'] : {}, isWarning && disabled ? s['warning-disabled'] : {}, loading ? s['loading '] : {}, size ? s['' + size] : {}, block ? s.block : {}, style); TextAttrArr.forEach(function (item) { if (btnStyle[item]) { textStyle[item] = btnStyle[item]; } if (btnStyle[item + ':active']) { textStyle[item + ':active'] = btnStyle[item + ':active']; } }); return (0, _rax.createElement)( _nukeTouchable2.default, _extends({ onPress: this.onPress, style: btnStyle }, others), iconSrc ? (0, _rax.createElement)(_nukeIcon2.default, { size: 'xs', src: iconSrc, style: { marginRight: 10 } }) : null, typeof children === 'string' ? (0, _rax.createElement)( _nukeText2.default, { style: textStyle, onClick: this.onTextPress }, children ) : children ); } }]); return Button; }(_rax.Component); Button.displayName = 'Button'; Button.propTypes = _extends({}, _nukeTouchable2.default.propTypes, { type: _rax.PropTypes.oneOf(['normal', 'primary', 'secondary', 'third']), size: _rax.PropTypes.oneOf(['small', 'medium', 'large']), shape: _rax.PropTypes.oneOf(['warning']), onPress: _rax.PropTypes.func, block: _rax.PropTypes.boolean, loading: _rax.PropTypes.boolean, warning: _rax.PropTypes.boolean, style: _rax.PropTypes.any, disabled: _rax.PropTypes.boolean }); Button.defaultProps = { shape: null, type: 'normal', size: 'medium', onPress: function onPress() {}, block: false, warning: false, loading: false, style: {}, disabled: false }; Button.contextTypes = { parentPath: _rax.PropTypes.any, parentStyle: _rax.PropTypes.any, androidConfigs: _rax.PropTypes.any, getConfig: _rax.PropTypes.func }; var StyledButton = (0, _nukeThemeProvider.connectStyle)(_styles2.default)(Button); exports.default = StyledButton; module.exports = exports['default'];