@payfit/unity-components
Version:
61 lines (60 loc) • 2.35 kB
JavaScript
import { AvatarGroupProvider as e } from "./AvatarGroup.context.js";
import { useResponsiveValue as t } from "../../hooks/use-responsive-value.js";
import { avatarGroup as n } from "./AvatarGroup.variants.js";
import { AvatarGroupCount as r } from "./parts/AvatarGroupCount.js";
import * as i from "react";
import { Fragment as a, jsx as o, jsxs as s } from "react/jsx-runtime";
//#region src/components/avatar-group/AvatarGroup.tsx
var c = 2, l = 4, u = 1, d = -1;
function f(e) {
if (!Number.isInteger(e) || e !== d && e < c) throw Error(`AvatarGroup maxItems must be -1 or an integer greater than or equal to ${c}.`);
}
function p({ items: e, maxItems: t, getItemKey: n, renderItem: l, renderCount: f }) {
if (e.length < c) throw Error(`AvatarGroup expects at least ${c} items.`);
let p = t !== d && e.length > t, m = p ? t - u : e.length;
return /* @__PURE__ */ s(a, { children: [(p ? e.slice(0, m) : e).map((e, t) => /* @__PURE__ */ o(i.Fragment, { children: l(e, t) }, n(e, t))), p ? f?.(e.length - m, e.slice(m)) ?? /* @__PURE__ */ o(r, { count: e.length - m }) : null] });
}
function m(e, t) {
if (typeof e == "function") throw Error("AvatarGroup requires items, getItemKey, and a render function as children when using the dynamic API.");
let n = i.Children.count(e);
if (n < c || t !== d && n > t) {
let e = t === d ? `at least ${c}` : `between ${c} and ${t}`;
throw Error(`AvatarGroup expects ${e} visible children; received ${n}. Use the dynamic API for automatic truncation.`);
}
return e;
}
function h({ variant: r = "stack", maxItems: i = l, items: a, getItemKey: s, renderCount: c, children: u, "aria-label": d, ...h }, g) {
let _ = t({
base: "sm",
md: "md"
});
f(i);
let v = a !== void 0, y;
if (v) {
if (typeof u != "function" || typeof s != "function") throw Error("AvatarGroup dynamic API requires items, getItemKey, and a render function as children.");
y = p({
items: a,
maxItems: i,
getItemKey: s,
renderItem: u,
renderCount: c
});
} else y = m(u, i);
return /* @__PURE__ */ o(e, {
defaultSize: _,
variant: r,
children: /* @__PURE__ */ o("div", {
ref: g,
...h,
role: "group",
"aria-label": d,
className: n({ variant: r }),
children: y
})
});
}
var g = i.forwardRef(h);
g.displayName = "AvatarGroup";
var _ = g;
//#endregion
export { _ as AvatarGroup };