analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
163 lines (152 loc) • 5.75 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/components/Accordation/AccordionGroup.tsx
var _react = require('react');
var _zustand = require('zustand');
var _jsxruntime = require('react/jsx-runtime');
function createAccordionGroupStore(type, initialValue, collapsible) {
return _zustand.create.call(void 0, (set, get) => ({
type,
value: initialValue,
collapsible,
setValue: (value) => set({ value }),
isItemExpanded: (itemValue) => {
const state = get();
if (state.type === "single") {
return state.value === itemValue;
} else {
return Array.isArray(state.value) && state.value.includes(itemValue);
}
}
}));
}
var injectStore = (children, store, indexRef, onItemToggle) => {
return _react.Children.map(children, (child) => {
if (!_react.isValidElement.call(void 0, child)) {
return child;
}
const typedChild = child;
const displayName = _optionalChain([typedChild, 'access', _ => _.type, 'optionalAccess', _2 => _2.displayName]);
if (displayName === "AccordionGroup") {
return child;
}
let newProps = {};
if (displayName === "CardAccordation") {
const itemValue = typedChild.props.value || `accordion-item-${indexRef.current++}`;
const storeState = store.getState();
const expanded = storeState.isItemExpanded(itemValue);
newProps.value = itemValue;
newProps.expanded = expanded;
newProps.onToggleExpanded = (isExpanded) => {
onItemToggle(itemValue, isExpanded);
_optionalChain([typedChild, 'access', _3 => _3.props, 'access', _4 => _4.onToggleExpanded, 'optionalCall', _5 => _5(isExpanded)]);
};
}
if (typedChild.props.children) {
const processedChildren = injectStore(
typedChild.props.children,
store,
indexRef,
onItemToggle
);
if (displayName === "CardAccordation") {
newProps.children = processedChildren;
} else if (processedChildren !== typedChild.props.children) {
return _react.cloneElement.call(void 0, typedChild, { children: processedChildren });
}
}
if (Object.keys(newProps).length > 0) {
return _react.cloneElement.call(void 0, typedChild, newProps);
}
return child;
});
};
var AccordionGroup = _react.forwardRef.call(void 0,
({
type = "single",
defaultValue,
value: controlledValue,
onValueChange,
collapsible = true,
children,
className,
...props
}, ref) => {
const [internalValue, setInternalValue] = _react.useState.call(void 0,
defaultValue || (type === "single" ? "" : [])
);
const isControlled = controlledValue !== void 0;
const currentValue = isControlled ? controlledValue : internalValue;
const storeRef = _react.useRef.call(void 0, null);
if (storeRef.current) {
storeRef.current.setState((prev) => {
const nextState = {};
if (prev.type !== type) {
nextState.type = type;
}
if (prev.collapsible !== collapsible) {
nextState.collapsible = collapsible;
}
return nextState;
});
} else {
storeRef.current = createAccordionGroupStore(
type,
currentValue,
collapsible
);
}
const store = storeRef.current;
_react.useEffect.call(void 0, () => {
store.setState({ value: currentValue });
}, [currentValue, store]);
_react.useEffect.call(void 0, () => {
if (!isControlled) {
setInternalValue((prev) => {
if (type === "single") {
if (Array.isArray(prev)) {
return _nullishCoalesce(prev[0], () => ( ""));
}
return typeof prev === "string" ? prev : "";
}
if (Array.isArray(prev)) {
return prev;
}
return prev ? [prev] : [];
});
}
}, [isControlled, type]);
const handleItemToggle = (itemValue, isExpanded) => {
const storeState = store.getState();
let newValue;
if (type === "single") {
if (isExpanded) {
newValue = itemValue;
} else {
newValue = collapsible ? "" : storeState.value;
}
} else {
const currentArray = Array.isArray(storeState.value) ? storeState.value : [];
if (isExpanded) {
newValue = [...currentArray, itemValue];
} else {
newValue = currentArray.filter((v) => v !== itemValue);
}
}
if (!isControlled) {
setInternalValue(newValue);
}
store.setState({ value: newValue });
_optionalChain([onValueChange, 'optionalCall', _6 => _6(newValue)]);
};
const indexRef = { current: 0 };
const enhancedChildren = injectStore(
children,
store,
indexRef,
handleItemToggle
);
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { ref, className, ...props, children: enhancedChildren });
}
);
AccordionGroup.displayName = "AccordionGroup";
exports.AccordionGroup = AccordionGroup;
//# sourceMappingURL=chunk-7W6XZZKK.js.map