UNPKG

@ifed/component

Version:
81 lines 6.38 kB
var _excluded = ["children"], _excluded2 = ["size", "direction", "block", "prefixCls", "className", "children"]; 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); } function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } import classNames from 'classnames'; import toArray from 'rc-util/lib/Children/toArray'; import * as React from 'react'; import { ConfigContext } from "../provider"; export var SpaceCompactItemContext = /*#__PURE__*/React.createContext(null); export var useCompactItemContext = function useCompactItemContext(prefixCls, direction) { var compactItemContext = React.useContext(SpaceCompactItemContext); var compactItemClassnames = React.useMemo(function () { var _classNames; if (!compactItemContext) return ''; var compactDirection = compactItemContext.compactDirection, isFirstItem = compactItemContext.isFirstItem, isLastItem = compactItemContext.isLastItem; var separator = compactDirection === 'vertical' ? '-vertical-' : '-'; return classNames((_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-compact").concat(separator, "item"), true), _defineProperty(_classNames, "".concat(prefixCls, "-compact").concat(separator, "first-item"), isFirstItem), _defineProperty(_classNames, "".concat(prefixCls, "-compact").concat(separator, "last-item"), isLastItem), _defineProperty(_classNames, "".concat(prefixCls, "-compact").concat(separator, "item-rtl"), direction === 'rtl'), _classNames)); }, [prefixCls, direction, compactItemContext]); return { compactSize: compactItemContext === null || compactItemContext === void 0 ? void 0 : compactItemContext.compactSize, compactDirection: compactItemContext === null || compactItemContext === void 0 ? void 0 : compactItemContext.compactDirection, compactItemClassnames: compactItemClassnames }; }; export var NoCompactStyle = function NoCompactStyle(_ref) { var children = _ref.children; return /*#__PURE__*/React.createElement(SpaceCompactItemContext.Provider, { value: null }, children); }; var CompactItem = function CompactItem(_ref2) { var children = _ref2.children, otherProps = _objectWithoutProperties(_ref2, _excluded); return /*#__PURE__*/React.createElement(SpaceCompactItemContext.Provider, { value: otherProps }, children); }; var Compact = function Compact(props) { var _classNames2; var _React$useContext = React.useContext(ConfigContext), getPrefixCls = _React$useContext.getPrefixCls, directionConfig = _React$useContext.direction; var _props$size = props.size, size = _props$size === void 0 ? 'middle' : _props$size, direction = props.direction, block = props.block, customizePrefixCls = props.prefixCls, className = props.className, children = props.children, restProps = _objectWithoutProperties(props, _excluded2); var prefixCls = getPrefixCls('space-compact', customizePrefixCls); var clx = classNames(prefixCls, (_classNames2 = {}, _defineProperty(_classNames2, "".concat(prefixCls, "-rtl"), directionConfig === 'rtl'), _defineProperty(_classNames2, "".concat(prefixCls, "-block"), block), _defineProperty(_classNames2, "".concat(prefixCls, "-vertical"), direction === 'vertical'), _classNames2), className); var compactItemContext = React.useContext(SpaceCompactItemContext); var childNodes = toArray(children); var nodes = React.useMemo(function () { return childNodes.map(function (child, i) { var key = child && child.key || "".concat(prefixCls, "-item-").concat(i); return /*#__PURE__*/React.createElement(CompactItem, { key: key, compactSize: size, compactDirection: direction, isFirstItem: i === 0 && (!compactItemContext || (compactItemContext === null || compactItemContext === void 0 ? void 0 : compactItemContext.isFirstItem)), isLastItem: i === childNodes.length - 1 && (!compactItemContext || (compactItemContext === null || compactItemContext === void 0 ? void 0 : compactItemContext.isLastItem)) }, child); }); }, [size, childNodes, compactItemContext]); if (childNodes.length === 0) { return null; } return /*#__PURE__*/React.createElement("div", _extends({ className: clx }, restProps), nodes); }; export default Compact;