Skip to content

Commit

Permalink
fix: Always provide firstIndex and totalItemsCount in collection resu…
Browse files Browse the repository at this point in the history
…lt (#72)
  • Loading branch information
pan-kot authored Apr 18, 2024
1 parent a13450a commit 57b9bf6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/__tests__/use-collection.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -576,4 +576,15 @@ describe('total items count and page range', () => {
const { getTotalItemsCount } = render(<App />);
expect(getTotalItemsCount()).toEqual('4');
});

test('should return first index and totalItems when no pagination', () => {
const allItems = generateItems(4);
function App() {
const result = useCollection<Item>(allItems, {});
return <Demo {...result} />;
}
const { getRowIndices, getTotalItemsCount } = render(<App />);
expect(getRowIndices()).toEqual(['1', '2', '3', '4']);
expect(getTotalItemsCount()).toEqual('4');
});
});
4 changes: 2 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ interface UseCollectionResultBase<T> {
};
trackBy?: string | ((item: T) => string);
ref: React.RefObject<CollectionRef>;
totalItemsCount?: number;
firstIndex?: number;
totalItemsCount: number;
firstIndex: number;
};
filterProps: {
disabled?: boolean;
Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,10 @@ export function createSyncProps<T>(
}
: {}),
ref: collectionRef,
firstIndex: 1,
totalItemsCount: allPageItems.length,
...(options.pagination?.pageSize
? {
totalItemsCount: allPageItems.length,
firstIndex: ((actualPageIndex ?? currentPageIndex) - 1) * options.pagination.pageSize + 1,
}
: {}),
Expand Down

0 comments on commit 57b9bf6

Please sign in to comment.