@kietpt2003/react-native-core-ui
Version:
React Native Core UI components by KietPT
37 lines (36 loc) • 2.33 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { View } from 'react-native';
import AccordionItem from './AccordionItem';
import Text from '../Texts/Text';
import AccordionGroup, { useOptionalAccordionGroup } from './AccordionGroup';
const makeTreeId = (path) => `accordion-node-${path}`;
const AccordionTreeInternal = ({ data, path, level, type, style, itemStyle, itemTextStyle, renderItem, renderRootItem, onItemChange, }) => {
const group = useOptionalAccordionGroup();
const renderContent = (item, nodeId, isExpanded, level) => {
const props = { item, nodeId, isExpanded, level };
if (item === null || item === void 0 ? void 0 : item.label) {
return item === null || item === void 0 ? void 0 : item.label;
}
if (renderRootItem) {
return renderRootItem(props);
}
if (renderItem) {
return renderItem(props);
}
return (_jsx(View, { style: itemStyle, children: _jsx(Text, { style: itemTextStyle, children: item.title }) }));
};
return (_jsx(View, { style: style, children: data.map((item, index) => {
var _a, _b, _c;
const currentPath = `${path}-${index}`;
const nodeId = (_a = item === null || item === void 0 ? void 0 : item.id) !== null && _a !== void 0 ? _a : makeTreeId(currentPath);
const isExpanded = (_b = group === null || group === void 0 ? void 0 : group.openIds.has(nodeId)) !== null && _b !== void 0 ? _b : false;
return (_jsx(AccordionItem, { id: nodeId, header: renderContent(item, nodeId, isExpanded, level), disabled: item.disabled, onChange: (expanded) => {
onItemChange === null || onItemChange === void 0 ? void 0 : onItemChange(item, {
id: nodeId,
path: currentPath,
expanded,
});
}, children: ((_c = item.children) === null || _c === void 0 ? void 0 : _c.length) ? (_jsx(AccordionGroup, { type: type, children: _jsx(AccordionTreeInternal, { data: item.children, path: currentPath, level: level + 1, type: type, itemStyle: itemStyle, itemTextStyle: itemTextStyle, renderItem: renderItem, onItemChange: onItemChange }) })) : null }, nodeId));
}) }));
};
export default AccordionTreeInternal;