@linkdesign/screen
Version:
屏组件库,但使用场景又不局限于屏。主要用于BI、大盘和屏
114 lines (112 loc) • 4.83 kB
JavaScript
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = _interopRequireWildcard(require("react"));
var _classnames = _interopRequireDefault(require("classnames"));
var _title = _interopRequireWildcard(require("../title"));
var _context = _interopRequireDefault(require("../context"));
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var Context = /*#__PURE__*/_react["default"].createContext(null);
var Item = /*#__PURE__*/function (_Component) {
(0, _inheritsLoose2["default"])(Item, _Component);
function Item(props) {
return _Component.call(this, props) || this;
}
var _proto = Item.prototype;
_proto.render = function render() {
var _this$props = this.props,
className = _this$props.className,
children = _this$props.children,
dataKey = _this$props.dataKey;
var _this$context = this.context,
activeKey = _this$context.activeKey,
updateActiveKey = _this$context.updateActiveKey;
var options = (0, _extends2["default"])({}, this.props);
delete options.children;
return /*#__PURE__*/_react["default"].createElement(_title["default"], (0, _extends2["default"])({}, options, {
className: (0, _classnames["default"])(className, {
selected: activeKey === dataKey
}),
onClick: function onClick() {
updateActiveKey(dataKey);
}
}), children);
};
return Item;
}(_react.Component);
Item.contextType = Context;
var SingleSelect = /*#__PURE__*/function (_Component2) {
(0, _inheritsLoose2["default"])(SingleSelect, _Component2);
function SingleSelect(props) {
var _this;
_this = _Component2.call(this, props) || this;
_this.state = {
activeKey: _this.props.defaultActiveKey || _this.props.activeKey
};
return _this;
}
var _proto2 = SingleSelect.prototype;
_proto2.componentDidUpdate = function componentDidUpdate(prevProps) {
var _this$props2 = this.props,
activeKey = _this$props2.activeKey,
onChange = _this$props2.onChange;
if (prevProps.activeKey !== activeKey) {
this.setState({
activeKey: activeKey
}, function () {
onChange(activeKey);
});
}
};
_proto2.render = function render() {
var _this2 = this;
var _cssPrefix = this.context._cssPrefix;
var _this$props3 = this.props,
className = _this$props3.className,
children = _this$props3.children,
align = _this$props3.align,
direction = _this$props3.direction,
onChange = _this$props3.onChange;
var activeKey = this.state.activeKey;
return /*#__PURE__*/_react["default"].createElement("div", {
className: (0, _classnames["default"])(_cssPrefix + "single-select-wrapper", _cssPrefix + "single-select-" + direction + "-" + align, className)
// @ts-ignore
,
style: _title.FLEX_STYLE[direction][align]
}, /*#__PURE__*/_react["default"].createElement(Context.Provider, {
value: {
activeKey: activeKey,
updateActiveKey: function updateActiveKey(key) {
_this2.setState({
activeKey: key
}, function () {
onChange(key);
});
}
}
}, _react.Children.map(children, function (child, i) {
return (
// @ts-ignore
/*#__PURE__*/(0, _react.isValidElement)(child) ? /*#__PURE__*/(0, _react.cloneElement)(child, {
dataKey: child.key || i
}) : child
);
})));
};
return SingleSelect;
}(_react.Component);
SingleSelect.Item = void 0;
SingleSelect.Item = Item;
// @ts-ignore
SingleSelect.defaultProps = {
className: null,
align: 'middle',
direction: 'row',
onChange: function onChange() {}
};
SingleSelect.contextType = _context["default"];
var _default = exports["default"] = SingleSelect;
;