@txdfe/at
Version:
一个设计体系组件库
206 lines (157 loc) • 8.81 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
var _excluded = ["prefix", "className", "style", "text", "starredText", "align"];
function _extends() { _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; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React from 'react';
import classNames from 'classnames';
import PropTypes from 'prop-types';
import ConfigProvider from '../config-provider';
import Icon from '../icon';
import { KEYCODE, obj } from '../util';
import Balloon from '../balloon';
var Tooltip = Balloon.Tooltip;
var Star = /*#__PURE__*/function (_React$Component) {
_inherits(Star, _React$Component);
var _super = _createSuper(Star);
function Star(props) {
var _this;
_classCallCheck(this, Star);
_this = _super.call(this, props);
_defineProperty(_assertThisInitialized(_this), "onChange", function (e) {
var starred = !_this.state.starred;
if (!('starred' in _this.props)) {
_this.setState({
starred: starred
});
}
_this.props.onChange(starred, e);
_this.props.onClick && _this.props.onClick(e);
});
_defineProperty(_assertThisInitialized(_this), "onKeyDown", function (e) {
if (e.keyCode === KEYCODE.ENTER || e.keyCode === KEYCODE.SPACE) {
_this.onChange(e);
}
_this.props.onKeyDown && _this.props.onKeyDown(e);
});
var _starred = 'starred' in props ? props.starred : props.defaultStarred;
_this.state = {
starred: typeof _starred === 'undefined' ? '' : _starred
};
return _this;
}
_createClass(Star, [{
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(nextProps) {
if ('starred' in nextProps) {
var starred = nextProps.starred;
if (starred === undefined) {
starred = false;
}
this.setState({
starred: starred
});
}
}
}, {
key: "render",
value: function render() {
var _classNames;
var _this$props = this.props,
prefix = _this$props.prefix,
className = _this$props.className,
style = _this$props.style,
text = _this$props.text,
starredText = _this$props.starredText,
align = _this$props.align,
others = _objectWithoutProperties(_this$props, _excluded);
var starred = this.state.starred;
var type = starred ? 'star-fill' : 'starstroke';
var iconCls = classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefix, "star"), true), _defineProperty(_classNames, "".concat(prefix, "starred"), starred), _defineProperty(_classNames, className, className), _classNames));
var trigger = /*#__PURE__*/React.createElement("span", _extends({
role: "star",
tabIndex: "0",
className: "".concat(prefix, "star-wrapper"),
onClick: this.onChange,
onKeyDown: this.onKeyDown
}, obj.pickAttrs(others)), /*#__PURE__*/React.createElement(Icon, {
type: type,
className: iconCls
}));
if (text) {
return /*#__PURE__*/React.createElement(Tooltip, {
trigger: trigger,
align: align
}, starred ? starredText : text);
} else {
return trigger;
}
}
}]);
return Star;
}(React.Component);
_defineProperty(Star, "propTypes", {
prefix: PropTypes.string,
/**
* 自定义类名
*/
className: PropTypes.string,
/**
* 自定义内敛样式
*/
style: PropTypes.object,
/**
* 是否收藏(针对受控组件)
*/
starred: PropTypes.bool,
/**
* 星标默认值(针对非受控组件)
*/
defaultStarred: PropTypes.bool,
/**
* starred 为 false 的时候弹出文字
*/
text: PropTypes.string,
/**
* starred 为 true 的时候弹出文字
*/
starredText: PropTypes.string,
/**
* 与 tooltip 的 align 属性一致
*/
align: PropTypes.string,
/**
* 星标状态改变时触发此事件
* @param {Boolean} starred
* @param {Event} e DOM事件对象
*/
onChange: PropTypes.func,
/**
* 鼠标点击事件
* @param {Event} e DOM事件对象
*/
onClick: PropTypes.func,
/**
* 键盘按键事件
* @param {Event} e DOM事件对象
*/
onKeyDown: PropTypes.func
});
_defineProperty(Star, "defaultProps", {
prefix: 'next-',
onChange: function onChange() {},
defaultStarred: false,
align: 't'
});
export default ConfigProvider.config(Star);