File size: 3,324 Bytes
5144199
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use client';

import { createContext, useContext } from "react";

//#region src/createTableHookContexts.tsx
/**
* Creates a fresh, scoped set of table/cell/header contexts (plus matching
* context hooks) that you can pass into {@link createTableHook}. This mirrors
* TanStack Form's `createFormHookContexts`.
*
* You usually do NOT need this: by default `createTableHook` wires its
* `AppTable`/`AppCell`/`AppHeader` providers to a shared module-scoped context,
* and you read it with the `useTableContext`/`useCellContext`/`useHeaderContext`
* hooks returned from `createTableHook`. Reach for `createTableHookContexts`
* when you need an *isolated* context, e.g. when nesting one table inside
* another and a consumer would otherwise read the wrong (nearest) provider.
*
* Type-safety note: the hooks returned here are typed with `TFeatures` only.
* They do NOT know the component maps you register in `createTableHook`
* (`tableComponents`/`cellComponents`/`headerComponents`), because those are
* defined later. For the richest types (the `App*` components and your
* registered components attached), prefer the `use*Context` hooks returned from
* your `createTableHook` call. The hooks here are the escape hatch for reading
* context from a module that does not / cannot import the `createTableHook`
* result.
*
* @example
* ```tsx
* // scoped-table-context.ts
* export const {
*   tableContext,
*   cellContext,
*   headerContext,
*   useTableContext,
*   useCellContext,
*   useHeaderContext,
* } = createTableHookContexts<typeof features>()
*
* // table.ts
* export const { useAppTable } = createTableHook({
*   features,
*   tableContext, // <- pass the scoped contexts so the providers use them
*   cellContext,
*   headerContext,
*   tableComponents: { PaginationControls },
* })
* ```
*/
function createTableHookContexts() {
	const tableContext = createContext(null);
	const cellContext = createContext(null);
	const headerContext = createContext(null);
	/**
	* Access the table instance from within an `AppTable` wrapper bound to these
	* scoped contexts. `TFeatures` is known; the registered component maps are not
	* (see {@link createTableHookContexts}).
	*/
	function useTableContext() {
		const table = useContext(tableContext);
		if (!table) throw new Error("`useTableContext` must be used within an `AppTable` component. Make sure your component is wrapped with `<table.AppTable>...</table.AppTable>`.");
		return table;
	}
	/**
	* Access the cell instance from within an `AppCell` wrapper bound to these
	* scoped contexts.
	*/
	function useCellContext() {
		const cell = useContext(cellContext);
		if (!cell) throw new Error("`useCellContext` must be used within an `AppCell` component. Make sure your component is wrapped with `<table.AppCell cell={cell}>...</table.AppCell>`.");
		return cell;
	}
	/**
	* Access the header instance from within an `AppHeader` or `AppFooter` wrapper
	* bound to these scoped contexts.
	*/
	function useHeaderContext() {
		const header = useContext(headerContext);
		if (!header) throw new Error("`useHeaderContext` must be used within an `AppHeader` or `AppFooter` component.");
		return header;
	}
	return {
		tableContext,
		cellContext,
		headerContext,
		useTableContext,
		useCellContext,
		useHeaderContext
	};
}

//#endregion
export { createTableHookContexts };