MCPcopy Index your code
hub / github.com/tinyplex/tinybase / useSortingAndPagination

Function useSortingAndPagination

src/ui-solid-dom/SortedTablePaginator.tsx:18–78  ·  view source on GitHub ↗
(
  cellId: MaybeAccessor<Id | undefined>,
  descending: MaybeAccessor<boolean | undefined>,
  sortOnClick: MaybeAccessor<boolean | undefined>,
  offset: MaybeAccessor<number | undefined>,
  limit: MaybeAccessor<number | undefined>,
  total: MaybeAccessor<number>,
  paginator: MaybeAccessor<boolean | Component<SortedTablePaginatorProps>>,
  onChange: MaybeAccessor<((sortAndOffset: SortAndOffset) => void) | undefined>,
)

Source from the content-addressed store, hash-verified

16const RIGHT_ARROW = '\u2192';
17
18export const useSortingAndPagination = (
19 cellId: MaybeAccessor<Id | undefined>,
20 descending: MaybeAccessor<boolean | undefined>,
21 sortOnClick: MaybeAccessor<boolean | undefined>,
22 offset: MaybeAccessor<number | undefined>,
23 limit: MaybeAccessor<number | undefined>,
24 total: MaybeAccessor<number>,
25 paginator: MaybeAccessor<boolean | Component<SortedTablePaginatorProps>>,
26 onChange: MaybeAccessor<((sortAndOffset: SortAndOffset) => void) | undefined>,
27): [
28 sortAndOffset: () => SortAndOffset,
29 handleSort: HandleSort,
30 paginatorComponent: (() => JSXElement) | undefined,
31] => {
32 const [sortAndOffset, setSortAndOffset] = createSignal<SortAndOffset>([
33 getValue(cellId),
34 !!getValue(descending),
35 getValue(offset) ?? 0,
36 ]);
37 createEffect(() =>
38 setSortAndOffset([
39 getValue(cellId),
40 !!getValue(descending),
41 getValue(offset) ?? 0,
42 ]),
43 );
44 const setStateAndChange = (sortAndOffset: SortAndOffset) => {
45 setSortAndOffset(sortAndOffset);
46 getValue(onChange)?.(sortAndOffset);
47 };
48 const handleSort = (cellId: Id | undefined) => {
49 if (getValue(sortOnClick)) {
50 const [currentCellId, currentDescending, currentOffset] = sortAndOffset();
51 setStateAndChange([
52 cellId,
53 cellId == currentCellId ? !currentDescending : false,
54 currentOffset,
55 ]);
56 }
57 };
58 const handleChangeOffset = (offset: number) => {
59 const [currentCellId, currentDescending] = sortAndOffset();
60 setStateAndChange([currentCellId, currentDescending, offset]);
61 };
62 const paginatorComponent = createMemo(() => {
63 const resolvedPaginator = getValue(paginator);
64 const [_, __, currentOffset] = sortAndOffset();
65 const PaginatorComponent = isTrue(resolvedPaginator)
66 ? SortedTablePaginator
67 : (resolvedPaginator as Component<SortedTablePaginatorProps>);
68 return isFalse(resolvedPaginator) ? null : (
69 <PaginatorComponent
70 offset={currentOffset}
71 limit={getValue(limit)}
72 total={getValue(total)}
73 onChange={handleChangeOffset}
74 />
75 );

Callers 2

SortedTableInHtmlTableFunction · 0.90

Calls 4

getValueFunction · 0.90
isTrueFunction · 0.90
isFalseFunction · 0.90
sortAndOffsetFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…