rsuite
Version:
A suite of react components
82 lines (79 loc) • 3.13 kB
JavaScript
'use client';
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.canJustifyBadgeButtons = canJustifyBadgeButtons;
exports.default = void 0;
var _react = _interopRequireDefault(require("react"));
var _Button = _interopRequireDefault(require("../Button/Button"));
var _IconButton = _interopRequireDefault(require("../IconButton/IconButton"));
var _Badge = _interopRequireDefault(require("../Badge/Badge"));
var _SafeAnchor = _interopRequireDefault(require("../internals/SafeAnchor"));
function getChildren(children) {
const nodes = [];
const collect = child => {
if (child === null || child === undefined || typeof child === 'boolean') {
return;
}
if (Array.isArray(child)) {
child.forEach(collect);
} else if (/*#__PURE__*/_react.default.isValidElement(child) && child.type === _react.default.Fragment) {
collect(child.props.children);
} else {
// Leave unknown nodes intact, including iterables that inspection could consume.
nodes.push(child);
}
};
collect(children);
return nodes;
}
function getComponentType(child) {
let type = child.type;
// A custom comparator may retain output that no longer matches the current props.
while (typeof type === 'object' && type !== null && '$$typeof' in type && type.$$typeof === Symbol.for('react.memo') && (!('compare' in type) || type.compare === null || type.compare === undefined) && 'type' in type) {
type = type.type;
}
return type;
}
function isButton(child, components) {
if (! /*#__PURE__*/_react.default.isValidElement(child)) {
return false;
}
const type = getComponentType(child);
if (type !== _Button.default && type !== _IconButton.default) {
return false;
}
const props = {
...components.Button?.defaultProps,
...(type === _IconButton.default ? components.IconButton?.defaultProps : undefined),
...child.props
};
const as = props.as || (props.href ? _SafeAnchor.default : 'button');
if (as === _SafeAnchor.default) {
const anchorAs = components.SafeAnchor?.defaultProps?.as;
return anchorAs === undefined || typeof anchorAs === 'string';
}
return typeof as === 'string';
}
/** Only known buttons with a single DOM root can participate in Badge group layouts. */
function isBadgeButton(children, components = {}) {
const nodes = getChildren(children);
return nodes.length === 1 && isButton(nodes[0], components);
}
/** Text and unknown component output can create anonymous grid items. */
function canJustifyBadgeButtons(children, components = {}) {
return getChildren(children).every(child => {
if (isButton(child, components)) {
return true;
}
if (! /*#__PURE__*/_react.default.isValidElement(child) || getComponentType(child) !== _Badge.default) {
return false;
}
const props = {
...components.Badge?.defaultProps,
...child.props
};
return (props.as === undefined || typeof props.as === 'string') && isBadgeButton(props.children, components);
});
}
var _default = exports.default = isBadgeButton;