antd-mobile
Version:
基于 React 的移动设计规范实现
108 lines (97 loc) • 4.11 kB
JavaScript
import _extends from 'babel-runtime/helpers/extends';
import _defineProperty from 'babel-runtime/helpers/defineProperty';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
import React from 'react';
import classNames from 'classnames';
import Icon from '../icon';
import getDataAttr from '../_util/getDataAttr';
var Tag = function (_React$Component) {
_inherits(Tag, _React$Component);
function Tag(props) {
_classCallCheck(this, Tag);
var _this = _possibleConstructorReturn(this, (Tag.__proto__ || Object.getPrototypeOf(Tag)).call(this, props));
_this.onClick = function () {
var _this$props = _this.props,
disabled = _this$props.disabled,
onChange = _this$props.onChange;
if (disabled) {
return;
}
var isSelect = _this.state.selected;
_this.setState({
selected: !isSelect
}, function () {
if (onChange) {
onChange(!isSelect);
}
});
};
_this.onTagClose = function () {
if (_this.props.onClose) {
_this.props.onClose();
}
_this.setState({
closed: true
}, _this.props.afterClose);
};
_this.state = {
selected: props.selected,
closed: false
};
return _this;
}
_createClass(Tag, [{
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (this.props.selected !== nextProps.selected) {
this.setState({
selected: nextProps.selected
});
}
}
}, {
key: 'render',
value: function render() {
var _classNames;
var _props = this.props,
children = _props.children,
className = _props.className,
prefixCls = _props.prefixCls,
disabled = _props.disabled,
closable = _props.closable,
small = _props.small,
style = _props.style;
var wrapCls = classNames((_classNames = {}, _defineProperty(_classNames, className, !!className), _defineProperty(_classNames, '' + prefixCls, true), _defineProperty(_classNames, prefixCls + '-normal', !disabled && (!this.state.selected || small || closable)), _defineProperty(_classNames, prefixCls + '-small', small), _defineProperty(_classNames, prefixCls + '-active', this.state.selected && !disabled && !small && !closable), _defineProperty(_classNames, prefixCls + '-disabled', disabled), _defineProperty(_classNames, prefixCls + '-closable', closable), _classNames));
var closableDom = closable && !disabled && !small ? React.createElement(
'div',
{ className: prefixCls + '-close', role: 'button', onClick: this.onTagClose, 'aria-label': 'remove tag' },
React.createElement(Icon, { type: 'cross-circle', size: 'xs', 'aria-hidden': 'true' })
) : null;
return !this.state.closed ? React.createElement(
'div',
_extends({}, getDataAttr(this.props), { className: wrapCls, onClick: this.onClick, style: style }),
React.createElement(
'div',
{ className: prefixCls + '-text' },
children
),
closableDom
) : null;
}
}]);
return Tag;
}(React.Component);
export default Tag;
Tag.defaultProps = {
prefixCls: 'am-tag',
disabled: false,
selected: false,
closable: false,
small: false,
onChange: function onChange() {},
onClose: function onClose() {},
afterClose: function afterClose() {}
};