Skip to content

Commit

Permalink
Fix PropTable duplicates
Browse files Browse the repository at this point in the history
  • Loading branch information
victortrinh2 committed Dec 27, 2024
1 parent 1cb6ed3 commit bac791a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
22 changes: 20 additions & 2 deletions apps/docs/app/lib/getComponentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,30 @@ export const formatData = async (prop: PropItem) => {
};

async function formatPropTable(data: ComponentDocWithGroups[]) {
const result = [];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result: any[] = [];
for (const item of data) {
const { groups } = item;
for (const group of Object.keys(groups)) {
const groupItems = await Promise.all(Object.keys(groups[group]).map(key => formatData(groups[group][key])));
result.push({ [group]: groupItems });

if (groupItems.length > 0) {
const existingGroup = result.find(entry => entry[group]);

if (existingGroup) {
// Merge new items into the existing group, without duplicates which has the same name
existingGroup[group] = [
...existingGroup[group],
...groupItems.filter(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
newItem => !existingGroup[group].some((existingItem: any) => existingItem.name === newItem.name)
)
];
} else {
// Add a new group to the result
result.push({ [group]: groupItems });
}
}
}
}

Expand Down
18 changes: 1 addition & 17 deletions apps/docs/app/ui/components/propTable/PropTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,9 @@ export default async function PropTable({ component }: PropTableProps) {
const { groups } = await getComponentProps(component);
const formattedGroups = await formatGroup(groups);

const mergedObject = formattedGroups.reduce((acc, current) => {
for (const [key, value] of Object.entries(current)) {
if (acc[key]) {
acc[key] = acc[key].concat(value);
} else {
acc[key] = value;
}
}

return acc;
}, {});

const mergedArray = Object.entries(mergedObject).map(([key, value]) => ({
[key]: value
}));

return (
<>
{mergedArray.map(group => {
{formattedGroups.map(group => {
const [key] = Object.keys(group);
const isEmpty = group[key].length === 0;

Expand Down

0 comments on commit bac791a

Please sign in to comment.