@proedis/react 1.6.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +238 -0
- package/cjs/components/RenderWhen/RenderWhen.js +1 -1
- package/cjs/components/RenderWhen/RenderWhen.js.map +1 -1
- package/cjs/hooks/useAutoControlledState.js.map +1 -1
- package/cjs/hooks/useAutoSizer.js +246 -0
- package/cjs/hooks/useAutoSizer.js.map +1 -0
- package/cjs/hooks/useClickOutside.js +7 -0
- package/cjs/hooks/useClickOutside.js.map +1 -1
- package/cjs/hooks/useDataManager.js.map +1 -1
- package/cjs/hooks/useDataSelector.js +53 -19
- package/cjs/hooks/useDataSelector.js.map +1 -1
- package/cjs/hooks/useDebouncedCallback.js.map +1 -1
- package/cjs/hooks/useElementType.js.map +1 -1
- package/cjs/hooks/useEnhancedEffect.js.map +1 -1
- package/cjs/hooks/useEvent.js.map +1 -1
- package/cjs/hooks/useForkRef.js +37 -9
- package/cjs/hooks/useForkRef.js.map +1 -1
- package/cjs/hooks/useInputValue.js.map +1 -1
- package/cjs/hooks/useObjectState.js.map +1 -1
- package/cjs/hooks/useSafeState.js.map +1 -1
- package/cjs/hooks/useSyncedRef.js +37 -5
- package/cjs/hooks/useSyncedRef.js.map +1 -1
- package/cjs/hooks/useUnmountEffect.js.map +1 -1
- package/cjs/hooks/useWindowSize.js.map +1 -1
- package/cjs/index.js +3 -0
- package/cjs/index.js.map +1 -1
- package/cjs/package.json +3 -0
- package/cjs/utils/compose.js.map +1 -1
- package/cjs/utils/contextBuilder.js.map +1 -1
- package/cjs/utils/shorthands.js +5 -6
- package/cjs/utils/shorthands.js.map +1 -1
- package/esm/components/RenderWhen/RenderWhen.js +1 -1
- package/esm/components/RenderWhen/RenderWhen.js.map +1 -1
- package/esm/hooks/useAutoControlledState.js.map +1 -1
- package/esm/hooks/useAutoSizer.js +224 -0
- package/esm/hooks/useAutoSizer.js.map +1 -0
- package/esm/hooks/useClickOutside.js +7 -0
- package/esm/hooks/useClickOutside.js.map +1 -1
- package/esm/hooks/useDataManager.js.map +1 -1
- package/esm/hooks/useDataSelector.js +53 -19
- package/esm/hooks/useDataSelector.js.map +1 -1
- package/esm/hooks/useDebouncedCallback.js.map +1 -1
- package/esm/hooks/useElementType.js.map +1 -1
- package/esm/hooks/useEnhancedEffect.js.map +1 -1
- package/esm/hooks/useEvent.js.map +1 -1
- package/esm/hooks/useForkRef.js +37 -9
- package/esm/hooks/useForkRef.js.map +1 -1
- package/esm/hooks/useInputValue.js.map +1 -1
- package/esm/hooks/useObjectState.js.map +1 -1
- package/esm/hooks/useSafeState.js.map +1 -1
- package/esm/hooks/useSyncedRef.js +37 -5
- package/esm/hooks/useSyncedRef.js.map +1 -1
- package/esm/hooks/useUnmountEffect.js.map +1 -1
- package/esm/hooks/useWindowSize.js.map +1 -1
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/package.json +3 -0
- package/esm/utils/compose.js.map +1 -1
- package/esm/utils/contextBuilder.js.map +1 -1
- package/esm/utils/shorthands.js +5 -6
- package/esm/utils/shorthands.js.map +1 -1
- package/package.json +13 -3
- package/types/hooks/index.d.ts +1 -0
- package/types/hooks/useAutoSizer.d.ts +82 -0
- package/types/hooks/useClickOutside.d.ts +10 -2
- package/types/hooks/useForkRef.d.ts +31 -4
- package/types/hooks/useSyncedRef.d.ts +36 -5
- package/tsconfig.declaration.tsbuildinfo +0 -1
package/README.md
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# `@proedis/react`
|
|
4
|
+
|
|
5
|
+
**The framework-agnostic half of a Proedis React app: typed contexts, shorthand props, and the
|
|
6
|
+
twenty hooks you would otherwise rewrite.** ⚛️
|
|
7
|
+
|
|
8
|
+
[](https://www.npmjs.com/package/@proedis/react)
|
|
9
|
+
[](https://github.com/proedis/Npm.Suite/blob/master/LICENSE)
|
|
10
|
+
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## ✨ What's in the box
|
|
16
|
+
|
|
17
|
+
No UI kit, no styling, no opinions about how anything looks. 🎨
|
|
18
|
+
|
|
19
|
+
| | |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| **Contexts** | `contextBuilder` — a context and its hook, with a real error instead of `undefined` |
|
|
22
|
+
| **Component props** | `UIComponentProps` / `UIVoidComponentProps`, the shared prop surface every component starts from |
|
|
23
|
+
| **Shorthands** | `ShorthandItem`, `createShorthandFactory`, `creatableComponent` — accept props *or* a node for the same slot |
|
|
24
|
+
| **Components** | `RenderWhen`, `compose` |
|
|
25
|
+
| **State hooks** | `useAutoControlledState`, `useObjectState`, `useSafeState`, `useInputValue`, `useDataManager`, `useDataSelector` |
|
|
26
|
+
| **Ref hooks** | `useSyncedRef`, `useForkRef` |
|
|
27
|
+
| **Effect hooks** | `useEnhancedEffect`, `useUnmountEffect`, `useDebouncedCallback` |
|
|
28
|
+
| **DOM hooks** | `useEvent`, `useClickOutside`, `useWindowSize`, `useElementType` |
|
|
29
|
+
| **Measurement** | `useAutoSizer` — the room an element has, kept up to date; `getAncestorsSpace` |
|
|
30
|
+
|
|
31
|
+
## 📦 Installation
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
yarn add @proedis/react
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`react` is a peer, accepted from **18 up to 19** — a range verified by compiling the emitted
|
|
38
|
+
declarations against `@types/react` 18 and 19 with `skipLibCheck` off, not by hoping.
|
|
39
|
+
|
|
40
|
+
## 🚀 Quick start
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
import { contextBuilder, RenderWhen, useSyncedRef } from '@proedis/react';
|
|
44
|
+
|
|
45
|
+
/** A context that fails loudly when consumed outside its provider */
|
|
46
|
+
const [ TicketContext, useTicketContext ] = contextBuilder<TicketContextValue>('Ticket');
|
|
47
|
+
|
|
48
|
+
function TicketPanel() {
|
|
49
|
+
const { ticket } = useTicketContext();
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<RenderWhen
|
|
53
|
+
condition={ticket.isOpen}
|
|
54
|
+
isTrue={<TicketBody ticket={ticket} />}
|
|
55
|
+
isFalse={<ClosedBanner />}
|
|
56
|
+
/>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## 📖 API
|
|
62
|
+
|
|
63
|
+
### 🏗️ `contextBuilder(name, initialContext?)`
|
|
64
|
+
|
|
65
|
+
Returns a `[ Context, useContext ]` pair. The hook throws a named error when it runs outside its
|
|
66
|
+
provider, so a missing provider is a message that tells you which context is missing — instead of an
|
|
67
|
+
`undefined` that explodes three components later.
|
|
68
|
+
|
|
69
|
+
```tsx
|
|
70
|
+
const [ AuthContext, useAuthContext ] = contextBuilder<AuthContextValue>('Auth');
|
|
71
|
+
|
|
72
|
+
<AuthContext.Provider value={value}>…</AuthContext.Provider>
|
|
73
|
+
|
|
74
|
+
const { user } = useAuthContext(); // throws outside the provider, with the name in the message
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 🎛️ `UIComponentProps` and `UIVoidComponentProps`
|
|
78
|
+
|
|
79
|
+
The prop surface every Proedis component starts from — `className`, `style`, the element type
|
|
80
|
+
override, and children for the non-void one. Compose your own strict props on top:
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
interface StrictButtonProps {
|
|
84
|
+
color?: ButtonColor;
|
|
85
|
+
loading?: boolean;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
type ButtonProps = UIComponentProps<StrictButtonProps>;
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
The second parameter is there for a component wrapping another one's props:
|
|
92
|
+
`UIComponentProps<StrictProps, BaseProps>` keeps your strict props authoritative and inherits the rest.
|
|
93
|
+
|
|
94
|
+
### 🎁 Shorthands
|
|
95
|
+
|
|
96
|
+
A shorthand slot accepts **either** a ready node **or** the props to build one — which is what lets a
|
|
97
|
+
component expose `icon={'user'}` and `icon={<CustomIcon />}` through a single prop.
|
|
98
|
+
|
|
99
|
+
| Symbol | What it is |
|
|
100
|
+
| --- | --- |
|
|
101
|
+
| `ShorthandItem<Props>` | a node, or the props of the component filling the slot |
|
|
102
|
+
| `ShorthandCollection<Props>` | an array of them, each carrying a `key` |
|
|
103
|
+
| `ShorthandContent` | plain renderable content |
|
|
104
|
+
| `createShorthandFactory(Component, mapValueToProps, computeKey?)` | build the factory that turns a value into an element |
|
|
105
|
+
| `creatableComponent(Component, …)` | attach a `create` method to a component |
|
|
106
|
+
| `renderShorthandContent({ children, content })` | pick between `children` and a `content` prop, in that order |
|
|
107
|
+
|
|
108
|
+
### 🔀 `RenderWhen` and `compose`
|
|
109
|
+
|
|
110
|
+
`RenderWhen` takes a `condition` and then **one of two shapes**, and the type will not let you mix
|
|
111
|
+
them: either children with an optional boolean `isFalse` inversion, or an `isTrue` / `isFalse` pair of
|
|
112
|
+
nodes and no children at all.
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
/** children form — render them when the condition holds */
|
|
116
|
+
<RenderWhen condition={isAdmin}>
|
|
117
|
+
<AdminPanel />
|
|
118
|
+
</RenderWhen>
|
|
119
|
+
|
|
120
|
+
/** children form, inverted — 'isFalse' is a boolean here */
|
|
121
|
+
<RenderWhen condition={isAdmin} isFalse>
|
|
122
|
+
<RequestAccess />
|
|
123
|
+
</RenderWhen>
|
|
124
|
+
|
|
125
|
+
/** ternary form — no children, both branches as nodes */
|
|
126
|
+
<RenderWhen condition={isAdmin} isTrue={<AdminPanel />} isFalse={<RequestAccess />} />
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
⚠️ `isFalse` means two different things across the two shapes: a boolean inversion with children, a
|
|
130
|
+
node without them. Passing children *and* a node is a compile error, which is the point.
|
|
131
|
+
|
|
132
|
+
`compose(...components)` nests a list of providers into one component, which turns a stack of eight
|
|
133
|
+
providers at the root of an app into a single element.
|
|
134
|
+
|
|
135
|
+
### 🪝 The hooks
|
|
136
|
+
|
|
137
|
+
| Hook | What it gives you |
|
|
138
|
+
| --- | --- |
|
|
139
|
+
| `useSyncedRef(value)` | a **stable** container whose `current` always reads the latest value — the way to let an effect or a handler see fresh data without being rebuilt |
|
|
140
|
+
| `useForkRef(...refs)` | one ref callback assigning to several refs, for a component that both exposes a ref and keeps one |
|
|
141
|
+
| `useEnhancedEffect` | `useLayoutEffect` in a browser, `useEffect` everywhere else, so SSR stays quiet |
|
|
142
|
+
| `useUnmountEffect(callback)` | run something once, on unmount, always the latest version of it |
|
|
143
|
+
| `useSafeState(initial)` | `useState` whose setter is a no-op after unmount |
|
|
144
|
+
| `useAutoControlledState(…)` | one state that works controlled *or* uncontrolled, the standard prop/defaultProp pattern |
|
|
145
|
+
| `useObjectState(values, options?)` | object state with per-key setters and an `onChange` notification |
|
|
146
|
+
| `useInputValue(initial?)` | value plus change handler for an uncontrolled-ish input |
|
|
147
|
+
| `useDebouncedCallback(fn, delay)` | a debounced callback that always calls the newest `fn` |
|
|
148
|
+
| `useDataManager()` | add / update / remove over a local collection |
|
|
149
|
+
| `useDataSelector(data, options?)` | a selection that is **always** backed by the current data |
|
|
150
|
+
| `useEvent(target, type, handler)` | a DOM listener attached once, calling the newest handler |
|
|
151
|
+
| `useClickOutside(target, callback)` | the outside-click primitive behind every dropdown |
|
|
152
|
+
| `useWindowSize(options?)` | viewport size, debounced |
|
|
153
|
+
| `useElementType(props, default)` | resolve the `as` prop into the element to render |
|
|
154
|
+
| `useAutoSizer(options?)` | the room an element has, in pixels, kept up to date — see below |
|
|
155
|
+
|
|
156
|
+
💡 Several of these are built on `useSyncedRef`, and that is the pattern worth internalising: keep the
|
|
157
|
+
*subscription* stable and let it read a moving value, instead of tearing it down whenever the value
|
|
158
|
+
changes.
|
|
159
|
+
|
|
160
|
+
⚠️ Read a `useSyncedRef` container from an effect, a handler or a cleanup — **not while rendering**. The
|
|
161
|
+
value is written during render, which is what makes it current for the commit it belongs to, and a
|
|
162
|
+
render that gets discarded leaves the container holding something that was never committed.
|
|
163
|
+
|
|
164
|
+
### 📐 `useAutoSizer(options?)`
|
|
165
|
+
|
|
166
|
+
The replacement for `height: calc(100vh - 320px)`.
|
|
167
|
+
|
|
168
|
+
A virtualized table, a map or a chart needs a pixel height; the height available depends on
|
|
169
|
+
everything rendered above it; and that number is not a constant — a filter bar wraps onto a second
|
|
170
|
+
line, a sidebar collapses, a notification appears, the font finally loads.
|
|
171
|
+
|
|
172
|
+
```tsx
|
|
173
|
+
const [ AutoSizer, { height } ] = useAutoSizer({ minHeight: 240 });
|
|
174
|
+
|
|
175
|
+
return (
|
|
176
|
+
<AutoSizer>
|
|
177
|
+
<VirtualizedTable height={height} rows={rows} />
|
|
178
|
+
</AutoSizer>
|
|
179
|
+
);
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Each axis answers with **the room left between the element and the far edge of the viewport**, minus
|
|
183
|
+
what the ancestors still need for their own padding, margin and border. That subtraction is the
|
|
184
|
+
point: without it a table filling "the rest of the screen" overflows its card by exactly the card's
|
|
185
|
+
padding, and the page grows a scrollbar nobody asked for.
|
|
186
|
+
|
|
187
|
+
| Option | Effect |
|
|
188
|
+
| --- | --- |
|
|
189
|
+
| `fixedHeight` / `fixedWidth` | pin an axis and skip its computation |
|
|
190
|
+
| `minHeight` / `maxHeight`, `minWidth` / `maxWidth` | clamp the result |
|
|
191
|
+
| `useOwnHeight` / `useOwnWidth` | measure the element itself instead of the room around it |
|
|
192
|
+
| `disabled` | stop measuring, keeping the last size — for a collapsed panel or a hidden tab |
|
|
193
|
+
|
|
194
|
+
Options are read through a ref, so changing them mid-life is safe and never re-subscribes anything.
|
|
195
|
+
|
|
196
|
+
**What it watches**: the element and its parent through a `ResizeObserver`, the viewport through a
|
|
197
|
+
resize listener, and its own visibility through an `IntersectionObserver` — so a tab that was hidden
|
|
198
|
+
when it mounted measures itself the moment it is shown, instead of reporting zero until the next
|
|
199
|
+
window resize. Measurements are coalesced into one animation frame, because a resize observer fires
|
|
200
|
+
several times per frame while a layout settles.
|
|
201
|
+
|
|
202
|
+
The returned component is a plain `div` that forwards its ref and every prop, and its **identity is
|
|
203
|
+
stable**: a component whose identity changes remounts its subtree, which for a virtualized table
|
|
204
|
+
means losing the scroll position on every resize.
|
|
205
|
+
|
|
206
|
+
`getAncestorsSpace(element)` is that subtraction on its own, for anything sizing an element it does
|
|
207
|
+
not own — a canvas, a third-party widget.
|
|
208
|
+
|
|
209
|
+
⚠️ Server-side it reports the `fixed*` values, or zero: there is nothing to measure until the effect
|
|
210
|
+
runs. Give the sized content a sensible `minHeight` if the first paint matters.
|
|
211
|
+
|
|
212
|
+
## 🔀 Migrating to 2.x
|
|
213
|
+
|
|
214
|
+
| Change | What to do |
|
|
215
|
+
| --- | --- |
|
|
216
|
+
| `useDataSelector` reconciles during render | Nothing, in most cases — and one render fewer per data change. The selection was previously corrected by an effect calling `setState`, so a render could show a selection the data no longer backed. |
|
|
217
|
+
| `onSelectedChange` fires once per real transition | Check any listener that counted calls. It used to fire from two places — synchronously inside the setter, and again from the reconciliation effect — so it also reported things that were not changes: selecting what was already selected, clearing an already empty selection (twice), and resolving `defaultSelected` against the data at mount. It now fires after the commit, once, and only when the resolved selection actually changed. The notification on unmount is unchanged. |
|
|
218
|
+
| No notification for the initial selection | If you relied on being told at mount what `defaultSelected` resolved to, read `selected` instead. The first value is not a transition, so nothing is reported — where before a `defaultSelected` that was not identity-equal to its item in the data produced a notification and an extra render. |
|
|
219
|
+
| A selection restores itself when its item comes back | Nothing to do, but know the shape: the hook stores what you *asked* for and resolves it against the current data on every render. An item that leaves the data and later returns — a filter applied and removed — becomes selected again, and that is reported. It used to store the resolved item and drop it for good. |
|
|
220
|
+
| `useForkRef` parameter type | Now a minimal structural `{ current: T \| null \| undefined }` rather than `React.MutableRefObject`. That is what keeps the same signature valid across `@types/react` 18 and 19; anything that was passing a real ref keeps working. |
|
|
221
|
+
| `RenderWhen` condition grouping | Its condition is now explicitly parenthesised. Same behaviour, but note the shape it always had: with the `isFalse` **inversion**, children render when the condition is false *even if there are no children* to render. |
|
|
222
|
+
|
|
223
|
+
## 🤝 Compatibility
|
|
224
|
+
|
|
225
|
+
| Requirement | Range |
|
|
226
|
+
| --- | --- |
|
|
227
|
+
| `react` | `>=18.0.0 <20.0.0` (peer) |
|
|
228
|
+
| `typescript` | `>=5.2.0` |
|
|
229
|
+
| Runtime | ES2022 — roughly Safari 16.4 / Chrome 94 |
|
|
230
|
+
|
|
231
|
+
Public signatures deliberately avoid `React.RefObject`, `React.MutableRefObject` and
|
|
232
|
+
`ReturnType<typeof React.useRef<T>>`: those aliases changed meaning between React 18 and 19, and are
|
|
233
|
+
the reason a hook can typecheck under one major and be rejected under the other. Minimal structural
|
|
234
|
+
shapes are used instead.
|
|
235
|
+
|
|
236
|
+
## 📄 License
|
|
237
|
+
|
|
238
|
+
MIT © [Proedis S.r.l.](https://proedis.net)
|
|
@@ -35,7 +35,7 @@ const RenderWhen = (props) => {
|
|
|
35
35
|
/** Get props from StrictConditionNode type */
|
|
36
36
|
const { children, isFalse: invertConditionLogic } = props;
|
|
37
37
|
/** Check if the node must render or not */
|
|
38
|
-
if (children &&
|
|
38
|
+
if ((children && !invertConditionLogic && condition) || (invertConditionLogic && !condition)) {
|
|
39
39
|
return (jsxRuntime.jsx(React__namespace.Fragment, { children: children }));
|
|
40
40
|
}
|
|
41
41
|
/** Return Empty */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RenderWhen.js","sources":["../../../../src/components/RenderWhen/RenderWhen.tsx"],"sourcesContent":[null],"names":["_jsx","React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA;;AAEc;AACd,MAAM,UAAU,GAA6C,CAAC,KAAK,KAAI;;;;AAKrE,IAAA,MAAM,EACJ,SAAS,EACV,GAAG,KAAK
|
|
1
|
+
{"version":3,"file":"RenderWhen.js","sources":["../../../../src/components/RenderWhen/RenderWhen.tsx"],"sourcesContent":[null],"names":["_jsx","React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAKA;;AAEc;AACd,MAAM,UAAU,GAA6C,CAAC,KAAK,KAAI;;;;AAKrE,IAAA,MAAM,EACJ,SAAS,EACV,GAAG,KAAK;;AAIT,IAAA,IAAI,UAAU,IAAI,KAAK,EAAE;;QAEvB,MAAM,EAAE,QAAQ,EAAE,OAAO,EAAE,oBAAoB,EAAE,GAAG,KAAK;;AAGzD,QAAA,IAAI,CAAC,QAAQ,IAAI,CAAC,oBAAoB,IAAI,SAAS,MAAM,oBAAoB,IAAI,CAAC,SAAS,CAAC,EAAE;YAC5F,QACEA,eAACC,gBAAK,CAAC,QAAQ,EAAA,EAAA,QAAA,EACZ,QAAQ,EAAA,CACM;QAErB;;AAGA,QAAA,OAAO,IAAI;IACb;;IAIA,MAAM,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,EAAE,eAAe,EAAE,GAAG,KAAK;;AAIpE,IAAA,QACED,cAAA,CAACC,gBAAK,CAAC,QAAQ,cACZ,SAAS,GAAG,eAAe,GAAG,gBAAgB,EAAA,CAChC;AAGrB;AAEA,UAAU,CAAC,WAAW,GAAG,YAAY;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAutoControlledState.js","sources":["../../../src/hooks/useAutoControlledState.ts"],"sourcesContent":[null],"names":["React","useEnhancedEffect","useSafeState"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA+BA;;AAEc;AAEd;;;;;;;;AAQG;
|
|
1
|
+
{"version":3,"file":"useAutoControlledState.js","sources":["../../../src/hooks/useAutoControlledState.ts"],"sourcesContent":[null],"names":["React","useEnhancedEffect","useSafeState"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA+BA;;AAEc;AAEd;;;;;;;;AAQG;AACG,SAAU,sBAAsB,CACpC,YAA2B,EAC3B,OAA0C,EAAA;AAI1C;;;;AAIG;IACH,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,GAAG,OAAO,IAAI,EAAE;AAG7C;;;;;AAKG;IACH,MAAM,gBAAgB,GAAGA,gBAAK,CAAC,MAAM,CAAU,KAAK,KAAK,SAAS,CAAC;AAGnE;;;AAGG;IACHC,mCAAiB,CACf,MAAK;AACH,QAAA,gBAAgB,CAAC,OAAO,GAAG,KAAK,KAAK,SAAS;AAChD,IAAA,CAAC,EACD,CAAE,KAAK,CAAE,CACV;AAGD;;;;;;AAMG;IACH,MAAM,CAAE,KAAK,EAAE,QAAQ,CAAE,GAAGC,yBAAY,CACtC,MAAK;;AAEH,QAAA,MAAM,WAAW,GAAG,KAAK,KAAK,SAAS,GAAG,YAAY,GAAG,SAAS;;AAGlE,QAAA,IAAI,WAAW,KAAK,SAAS,EAAE;AAC7B,YAAA,OAAO,WAAW;QACpB;;QAGA,OAAO,OAAO,YAAY,KAAK;cAC1B,YAAwB;cACzB,YAAY;AAClB,IAAA,CAAC,CACF;AAGD;;;;AAIG;IACH,MAAM,WAAW,GAA4CF,gBAAK,CAAC,WAAW,CAC5E,CAAC,QAAQ,KAAI;;AAEX,QAAA,IAAI,gBAAgB,CAAC,OAAO,EAAE;YAC5B,QAAQ,CAAC,QAAQ,CAAC;QACpB;AACF,IAAA,CAAC,EACD,CAAE,QAAQ,CAAE,CACb;;IAID,OAAO;QACL,KAAK,KAAK,SAAS,GAAG,KAAK,GAAG,KAAK;QACnC,WAAW;QACX;KACQ;AAEZ;;;;"}
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var useSyncedRef = require('./useSyncedRef.js');
|
|
6
|
+
|
|
7
|
+
function _interopNamespaceDefault(e) {
|
|
8
|
+
var n = Object.create(null);
|
|
9
|
+
if (e) {
|
|
10
|
+
Object.keys(e).forEach(function (k) {
|
|
11
|
+
if (k !== 'default') {
|
|
12
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
13
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () { return e[k]; }
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
n.default = e;
|
|
21
|
+
return Object.freeze(n);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
|
|
25
|
+
|
|
26
|
+
/* --------
|
|
27
|
+
* Utilities
|
|
28
|
+
* -------- */
|
|
29
|
+
/**
|
|
30
|
+
* Cross-realm `instanceof HTMLElement`.
|
|
31
|
+
*
|
|
32
|
+
* An element rendered into a portal, an iframe or a popup window belongs to a different realm, where
|
|
33
|
+
* the global `HTMLElement` is a different constructor — so the plain `instanceof` answers `false` on
|
|
34
|
+
* a perfectly valid element. Asking its own document's view keeps the walk working there.
|
|
35
|
+
*/
|
|
36
|
+
function isHtmlElement(element) {
|
|
37
|
+
const view = element?.ownerDocument?.defaultView;
|
|
38
|
+
return !!view && element instanceof view.HTMLElement;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Sum the space every ancestor still needs *after* the element, walking up to the document.
|
|
42
|
+
*
|
|
43
|
+
* This is what makes the hook worth having. An element's `getBoundingClientRect().top` says where it
|
|
44
|
+
* starts; the room left below it is the rest of the viewport **minus** the bottom padding, margin and
|
|
45
|
+
* border of each ancestor — otherwise a table filling "the rest of the screen" overflows its card by
|
|
46
|
+
* exactly the card's own padding, and the page grows a scrollbar nobody asked for.
|
|
47
|
+
*
|
|
48
|
+
* Exported because it is the useful half on its own: anything sizing an element it does not own — a
|
|
49
|
+
* canvas, a third-party widget — needs the same number and has no reason to reimplement the walk.
|
|
50
|
+
*
|
|
51
|
+
* @param element - Where to start. Usually the parent of the element being sized.
|
|
52
|
+
*/
|
|
53
|
+
function getAncestorsSpace(element) {
|
|
54
|
+
const space = { bottom: 0, right: 0 };
|
|
55
|
+
let current = element;
|
|
56
|
+
while (isHtmlElement(current)) {
|
|
57
|
+
const styles = current.ownerDocument.defaultView.getComputedStyle(current);
|
|
58
|
+
space.bottom += (parseFloat(styles.marginBottom) || 0)
|
|
59
|
+
+ (parseFloat(styles.paddingBottom) || 0)
|
|
60
|
+
+ (parseFloat(styles.borderBottomWidth) || 0);
|
|
61
|
+
space.right += (parseFloat(styles.marginRight) || 0)
|
|
62
|
+
+ (parseFloat(styles.paddingRight) || 0)
|
|
63
|
+
+ (parseFloat(styles.borderRightWidth) || 0);
|
|
64
|
+
current = current.parentElement;
|
|
65
|
+
}
|
|
66
|
+
return { bottom: Math.ceil(space.bottom), right: Math.ceil(space.right) };
|
|
67
|
+
}
|
|
68
|
+
function clamp(value, min, max) {
|
|
69
|
+
return Math.min(max ?? Number.MAX_SAFE_INTEGER, Math.max(min ?? 0, value));
|
|
70
|
+
}
|
|
71
|
+
/* --------
|
|
72
|
+
* Hook Definition
|
|
73
|
+
* -------- */
|
|
74
|
+
/**
|
|
75
|
+
* Measure the room an element has, and keep measuring it.
|
|
76
|
+
*
|
|
77
|
+
* The problem it replaces is `height: calc(100vh - 320px)`: a virtualized table, a map or a chart
|
|
78
|
+
* needs a pixel height, the height available depends on everything rendered above it, and that
|
|
79
|
+
* number is not a constant — a filter bar wraps onto a second line, a sidebar collapses, a
|
|
80
|
+
* notification appears, the font finally loads.
|
|
81
|
+
*
|
|
82
|
+
* Render the returned component where the sized content goes, and use the numbers:
|
|
83
|
+
*
|
|
84
|
+
* ```tsx
|
|
85
|
+
* const [ AutoSizer, { height } ] = useAutoSizer({ minHeight: 240 });
|
|
86
|
+
*
|
|
87
|
+
* return (
|
|
88
|
+
* <AutoSizer>
|
|
89
|
+
* <VirtualizedTable height={height} rows={rows} />
|
|
90
|
+
* </AutoSizer>
|
|
91
|
+
* );
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* By default each axis answers with **the room left between the element and the far edge of the
|
|
95
|
+
* viewport**, minus what the ancestors still need for their own padding, margin and border. Switch an
|
|
96
|
+
* axis to `useOwnHeight` / `useOwnWidth` to measure the element itself instead, or pin it with
|
|
97
|
+
* `fixedHeight` / `fixedWidth`.
|
|
98
|
+
*
|
|
99
|
+
* What it watches: the element and its parent through a `ResizeObserver`, the viewport through a
|
|
100
|
+
* resize listener, and its own visibility through an `IntersectionObserver` — so a tab that was
|
|
101
|
+
* hidden when it mounted measures itself the moment it is shown, instead of reporting zero until the
|
|
102
|
+
* next window resize. Measurements are coalesced into one animation frame, because a resize observer
|
|
103
|
+
* fires several times per frame while a layout settles.
|
|
104
|
+
*
|
|
105
|
+
* @param options - See `UseAutoSizerOptions`. Read through a ref, so changing them mid-life is safe
|
|
106
|
+
* and never re-subscribes the observers.
|
|
107
|
+
*/
|
|
108
|
+
function useAutoSizer(options) {
|
|
109
|
+
// ----
|
|
110
|
+
// Internal State
|
|
111
|
+
// ----
|
|
112
|
+
/**
|
|
113
|
+
* The element lives in state, not only in a ref: the observers must be attached when it mounts,
|
|
114
|
+
* and an effect cannot depend on a ref's content.
|
|
115
|
+
*/
|
|
116
|
+
const [element, setElement] = React__namespace.useState(null);
|
|
117
|
+
const [size, setSize] = React__namespace.useState(() => ({
|
|
118
|
+
height: options?.fixedHeight ?? 0,
|
|
119
|
+
width: options?.fixedWidth ?? 0
|
|
120
|
+
}));
|
|
121
|
+
// ----
|
|
122
|
+
// Internal Hooks
|
|
123
|
+
// ----
|
|
124
|
+
const optionsRef = useSyncedRef.useSyncedRef(options);
|
|
125
|
+
/** The last emitted size, so the measure function never depends on the state it writes */
|
|
126
|
+
const sizeRef = React__namespace.useRef(size);
|
|
127
|
+
const frameRef = React__namespace.useRef(null);
|
|
128
|
+
// ----
|
|
129
|
+
// Handlers
|
|
130
|
+
// ----
|
|
131
|
+
const measure = React__namespace.useCallback(() => {
|
|
132
|
+
const { disabled, fixedHeight, fixedWidth, maxHeight, maxWidth, minHeight, minWidth, useOwnHeight, useOwnWidth } = optionsRef.current ?? {};
|
|
133
|
+
if (!element || disabled) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const view = element.ownerDocument.defaultView;
|
|
137
|
+
if (!view) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const space = getAncestorsSpace(element.parentElement);
|
|
141
|
+
const rect = element.getBoundingClientRect();
|
|
142
|
+
const nextHeight = clamp(fixedHeight
|
|
143
|
+
?? (useOwnHeight
|
|
144
|
+
? element.clientHeight
|
|
145
|
+
: Math.max(view.innerHeight - rect.top - space.bottom, 0)), minHeight, maxHeight);
|
|
146
|
+
const nextWidth = clamp(fixedWidth
|
|
147
|
+
?? (useOwnWidth
|
|
148
|
+
? element.clientWidth
|
|
149
|
+
: Math.max(view.innerWidth - rect.left - space.right, 0)), minWidth, maxWidth);
|
|
150
|
+
const { current: lastSize } = sizeRef;
|
|
151
|
+
if (nextHeight === lastSize.height && nextWidth === lastSize.width) {
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
sizeRef.current = { height: nextHeight, width: nextWidth };
|
|
155
|
+
setSize(sizeRef.current);
|
|
156
|
+
}, [element, optionsRef]);
|
|
157
|
+
/**
|
|
158
|
+
* Coalesce into one frame. A `ResizeObserver` fires repeatedly while a layout settles, and each
|
|
159
|
+
* measurement reads the layout back — measuring once per frame is the difference between a smooth
|
|
160
|
+
* resize and a janky one.
|
|
161
|
+
*/
|
|
162
|
+
const scheduleMeasure = React__namespace.useCallback(() => {
|
|
163
|
+
const view = element?.ownerDocument.defaultView;
|
|
164
|
+
if (!view) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (frameRef.current !== null) {
|
|
168
|
+
view.cancelAnimationFrame(frameRef.current);
|
|
169
|
+
}
|
|
170
|
+
frameRef.current = view.requestAnimationFrame(() => {
|
|
171
|
+
frameRef.current = null;
|
|
172
|
+
measure();
|
|
173
|
+
});
|
|
174
|
+
}, [element, measure]);
|
|
175
|
+
// ----
|
|
176
|
+
// Lifecycle Events
|
|
177
|
+
// ----
|
|
178
|
+
React__namespace.useEffect(() => {
|
|
179
|
+
const view = element?.ownerDocument.defaultView;
|
|
180
|
+
if (!element || !view) {
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
/** Measure once, synchronously, so the first paint is not a frame late */
|
|
184
|
+
measure();
|
|
185
|
+
const observed = [element, element.parentElement].filter(Boolean);
|
|
186
|
+
const resizeObserver = new view.ResizeObserver(scheduleMeasure);
|
|
187
|
+
observed.forEach(target => resizeObserver.observe(target));
|
|
188
|
+
/**
|
|
189
|
+
* A hidden element has a zero rect, so measuring it produces zero. Watching its visibility is
|
|
190
|
+
* what makes a tab, an accordion or a collapsed panel measure itself the moment it appears.
|
|
191
|
+
*/
|
|
192
|
+
const intersectionObserver = new view.IntersectionObserver((entries) => {
|
|
193
|
+
if (entries.some(entry => entry.isIntersecting)) {
|
|
194
|
+
scheduleMeasure();
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
intersectionObserver.observe(element);
|
|
198
|
+
view.addEventListener('resize', scheduleMeasure);
|
|
199
|
+
view.addEventListener('orientationchange', scheduleMeasure);
|
|
200
|
+
return () => {
|
|
201
|
+
if (frameRef.current !== null) {
|
|
202
|
+
view.cancelAnimationFrame(frameRef.current);
|
|
203
|
+
frameRef.current = null;
|
|
204
|
+
}
|
|
205
|
+
resizeObserver.disconnect();
|
|
206
|
+
intersectionObserver.disconnect();
|
|
207
|
+
view.removeEventListener('resize', scheduleMeasure);
|
|
208
|
+
view.removeEventListener('orientationchange', scheduleMeasure);
|
|
209
|
+
};
|
|
210
|
+
}, [element, measure, scheduleMeasure]);
|
|
211
|
+
// ----
|
|
212
|
+
// Memoized Data
|
|
213
|
+
// ----
|
|
214
|
+
/**
|
|
215
|
+
* One element, and a stable component identity.
|
|
216
|
+
*
|
|
217
|
+
* Both matter. The ancestor of this hook rendered *two* divs — a zero-height "viewport detector"
|
|
218
|
+
* plus the measured one — and spread the caller's props onto both, so a `className` with padding
|
|
219
|
+
* was applied twice. And a component whose identity changes remounts its whole subtree, which for
|
|
220
|
+
* a virtualized table means losing its scroll position on every resize.
|
|
221
|
+
*/
|
|
222
|
+
const AutoSizer = React__namespace.useMemo(() => {
|
|
223
|
+
const Component = React__namespace.forwardRef((props, forwardedRef) => {
|
|
224
|
+
const handleRef = React__namespace.useCallback((node) => {
|
|
225
|
+
setElement(node);
|
|
226
|
+
if (typeof forwardedRef === 'function') {
|
|
227
|
+
forwardedRef(node);
|
|
228
|
+
}
|
|
229
|
+
else if (forwardedRef) {
|
|
230
|
+
forwardedRef.current = node;
|
|
231
|
+
}
|
|
232
|
+
}, [forwardedRef]);
|
|
233
|
+
return jsxRuntime.jsx("div", { ...props, ref: handleRef });
|
|
234
|
+
});
|
|
235
|
+
Component.displayName = 'AutoSizer';
|
|
236
|
+
return Component;
|
|
237
|
+
}, []);
|
|
238
|
+
// ----
|
|
239
|
+
// Hook Return
|
|
240
|
+
// ----
|
|
241
|
+
return [AutoSizer, size];
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
exports.getAncestorsSpace = getAncestorsSpace;
|
|
245
|
+
exports.useAutoSizer = useAutoSizer;
|
|
246
|
+
//# sourceMappingURL=useAutoSizer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useAutoSizer.js","sources":["../../../src/hooks/useAutoSizer.tsx"],"sourcesContent":[null],"names":["React","useSyncedRef","_jsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA8DA;;AAEc;AAEd;;;;;;AAMG;AACH,SAAS,aAAa,CAAC,OAAmC,EAAA;AACxD,IAAA,MAAM,IAAI,GAAG,OAAO,EAAE,aAAa,EAAE,WAAW;IAEhD,OAAO,CAAC,CAAC,IAAI,IAAI,OAAO,YAAY,IAAI,CAAC,WAAW;AACtD;AAGA;;;;;;;;;;;;AAYG;AACG,SAAU,iBAAiB,CAAC,OAA2B,EAAA;IAC3D,MAAM,KAAK,GAAmB,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,E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B,MAAM,SAAS,GAAGA,gBAAK,CAAC,UAAU,CAChC,CAAC,KAAK,EAAE,YAAY,KAAI;YACtB,MAAM,SAAS,GAAGA,gBAAK,CAAC,WAAW,CACjC,CAAC,IAA2B,KAAI;gBAC9B,UAAU,CAAC,IAAI,CAAC;AAEhB,gBAAA,IAAI,OAAO,YAAY,KAAK,UAAU,EAAE;oBACtC,YAAY,CAAC,IAAI,CAAC;gBACpB;qBACK,IAAI,YAAY,EAAE;AACrB,oBAAA,YAAY,CAAC,OAAO,GAAG,IAAI;gBAC7B;AACF,YAAA,CAAC,EACD,CAAE,YAAY,CAAE,CACjB;AAED,YAAA,OAAOE,2BAAS,KAAK,EAAE,GAAG,EAAE,SAAS,GAAI;AAC3C,QAAA,CAAC,CACF;AAED,QAAA,SAAS,CAAC,WAAW,GAAG,WAAW;AAEnC,QAAA,OAAO,SAAS;IAClB,CAAC,EACD,EAAE,CACH;;;;AAMD,IAAA,OAAO,CAAE,SAAS,EAAE,IAAI,CAAE;AAE5B;;;;;"}
|
|
@@ -11,6 +11,13 @@ const DEFAULT_EVENTS = [
|
|
|
11
11
|
/**
|
|
12
12
|
* Use this hook to fire an event every time a click occurs outside
|
|
13
13
|
* the provided target element
|
|
14
|
+
*
|
|
15
|
+
* The target is typed as the minimal structural shape the hook actually reads instead of
|
|
16
|
+
* 'React.RefObject<T> | React.MutableRefObject<T>'. Those aliases changed meaning between
|
|
17
|
+
* @types/react 18 and 19: in 19 'useRef<T>(null)' yields 'RefObject<T | null>', which no
|
|
18
|
+
* longer satisfies 'RefObject<T>' and made every call site fail to compile. The
|
|
19
|
+
* implementation already null-checks 'current', so admitting it in the type costs nothing.
|
|
20
|
+
*
|
|
14
21
|
* @param target
|
|
15
22
|
* @param callback
|
|
16
23
|
* @param events
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useClickOutside.js","sources":["../../../src/hooks/useClickOutside.ts"],"sourcesContent":[null],"names":["useSyncedRef","useEnhancedEffect"],"mappings":";;;;;
|
|
1
|
+
{"version":3,"file":"useClickOutside.js","sources":["../../../src/hooks/useClickOutside.ts"],"sourcesContent":[null],"names":["useSyncedRef","useEnhancedEffect"],"mappings":";;;;;AAIA;AACA,MAAM,cAAc,GAA6C;IAC/D,WAAW;IACX;CACD;AAGD;;;;;;;;;;;;;AAaG;AACG,SAAU,eAAe,CAC7B,MAAkD,EAClD,QAAuB,EACvB,SAAmD,cAAc,EAAA;;AAIjE,IAAA,MAAM,aAAa,GAAGA,yBAAY,CAAC,MAAM,CAAC;AAC1C,IAAA,MAAM,gBAAgB,GAAGA,yBAAY,CAAC,QAAQ,CAAC;;IAG/CC,mCAAiB,CACf,MAAK;AAEH;;;;;;AAMG;QACH,SAAS,OAAO,CAAoB,KAAY,EAAA;;AAE9C,YAAA,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,OAAO,EAAE;gBAClC;YACF;;AAGA,YAAA,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,GAAG,KAAK;AACrC,YAAA,MAAM,EAAE,GAAG,gBAAgB,CAAC,OAAO;AAEnC,YAAA,IACE,CAAC;AACE,oBAAC,CAAC,CAAC,WAAW,IAAI,CAAC,aAAa,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,WAAmB,CAAC,CAAC,EAAE;;AAEpF,gBAAA,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,KAAK,CAAC;YACtB;QACF;;AAGA,QAAA,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,KAAI;;AAE3B,YAAA,QAAQ,CAAC,gBAAgB,CAAC,SAAS,EAAE,OAAO,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;AAClE,QAAA,CAAC,CAAC;;AAGF,QAAA,OAAO,MAAK;AACV,YAAA,MAAM,CAAC,OAAO,CAAC,CAAC,SAAS,KAAI;;AAE3B,gBAAA,QAAQ,CAAC,mBAAmB,CAAC,SAAS,EAAE,OAAO,CAAC;AAClD,YAAA,CAAC,CAAC;AACJ,QAAA,CAAC;IACH,CAAC;;IAED,CAAE,gBAAgB,EAAE,aAAa,EAAE,GAAG,MAAM,CAAE,CAC/C;AAEH;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useDataManager.js","sources":["../../../src/hooks/useDataManager.ts"],"sourcesContent":[null],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAiCA;;AAEc;SACE,cAAc,GAAA;;;;AAK5B,IAAA,MAAM,CAAE,QAAQ,EAAE,WAAW,CAAE,GAAGA,gBAAK,CAAC,QAAQ,CAAgB,SAAS,CAAC
|
|
1
|
+
{"version":3,"file":"useDataManager.js","sources":["../../../src/hooks/useDataManager.ts"],"sourcesContent":[null],"names":["React"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAiCA;;AAEc;SACE,cAAc,GAAA;;;;AAK5B,IAAA,MAAM,CAAE,QAAQ,EAAE,WAAW,CAAE,GAAGA,gBAAK,CAAC,QAAQ,CAAgB,SAAS,CAAC;AAC1E,IAAA,MAAM,CAAE,QAAQ,EAAE,WAAW,CAAE,GAAGA,gBAAK,CAAC,QAAQ,CAAgB,SAAS,CAAC;;;;AAM1E,IAAA,MAAM,aAAa,GAAGA,gBAAK,CAAC,WAAW,CACrC,MAAM,WAAW,CAAC,SAAS,CAAC,EAC5B,EAAE,CACH;AAED,IAAA,MAAM,aAAa,GAAGA,gBAAK,CAAC,WAAW,CACrC,MAAM,WAAW,CAAC,SAAS,CAAC,EAC5B,EAAE,CACH;IAED,MAAM,qBAAqB,GAAGA,gBAAK,CAAC,WAAW,CAC7C,CAAC,OAAsB,KAAK,MAAM,WAAW,CAAC,OAAO,CAAC,EACtD,EAAE,CACH;IAED,MAAM,qBAAqB,GAAGA,gBAAK,CAAC,WAAW,CAC7C,CAAC,OAAsB,KAAK,MAAM,WAAW,CAAC,OAAO,CAAC,EACtD,EAAE,CACH;;;;IAMD,OAAO;QACL,QAAQ;QACR,aAAa;QACb,aAAa;QACb,qBAAqB;QACrB,qBAAqB;QACrB,WAAW;QACX,WAAW;QACX;KACD;AAEH;;;;"}
|
|
@@ -36,7 +36,7 @@ function useDataSelector(data, options) {
|
|
|
36
36
|
// ----
|
|
37
37
|
// Internal State
|
|
38
38
|
// ----
|
|
39
|
-
const [
|
|
39
|
+
const [requestedSelection, setRequestedSelection] = React__namespace.useState(options?.defaultSelected);
|
|
40
40
|
// ----
|
|
41
41
|
// Internal Hooks
|
|
42
42
|
// ----
|
|
@@ -61,34 +61,68 @@ function useDataSelector(data, options) {
|
|
|
61
61
|
return selectableData.includes(item) ? item : undefined;
|
|
62
62
|
}, [data, userDefinedComparer]);
|
|
63
63
|
const setSelected = React__namespace.useCallback((item) => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const { current: currentUserDefinedOnSelectChange } = userDefinedOnSelectChange;
|
|
67
|
-
if (typeof currentUserDefinedOnSelectChange === 'function') {
|
|
68
|
-
currentUserDefinedOnSelectChange(sourceItem);
|
|
69
|
-
}
|
|
70
|
-
}, [getSourceItem, userDefinedOnSelectChange]);
|
|
64
|
+
setRequestedSelection(item);
|
|
65
|
+
}, []);
|
|
71
66
|
const clearSelected = React__namespace.useCallback(() => setSelected(undefined), [setSelected]);
|
|
72
67
|
const createSelectorHandler = React__namespace.useCallback((item) => () => setSelected(item), [setSelected]);
|
|
73
68
|
// ----
|
|
74
69
|
// Selected Assertion
|
|
75
70
|
// ----
|
|
71
|
+
/**
|
|
72
|
+
* Resolve the requested selection against the data as it is *now*, while rendering.
|
|
73
|
+
*
|
|
74
|
+
* 'getSourceItem' already answers every case the reconciliation has to cover: it returns the matching
|
|
75
|
+
* source item, or 'undefined' when the requested one is not in the data any more — and 'undefined' in,
|
|
76
|
+
* 'undefined' out. So the whole thing is one derivation.
|
|
77
|
+
*
|
|
78
|
+
* It used to be an effect that called setState, which meant every data change that invalidated the
|
|
79
|
+
* selection rendered twice: once with the stale selection, once with the corrected one. Deriving it
|
|
80
|
+
* here means a render never shows a selection the data does not back.
|
|
81
|
+
*/
|
|
82
|
+
const selected = getSourceItem(requestedSelection);
|
|
83
|
+
// ----
|
|
84
|
+
// Selection Change Notification
|
|
85
|
+
// ----
|
|
86
|
+
/**
|
|
87
|
+
* Notify the consumer once per actual change, from a single place.
|
|
88
|
+
*
|
|
89
|
+
* There used to be two sources for this callback — a synchronous call inside the setter and the
|
|
90
|
+
* reconciliation effect calling that same setter — and between them they reported things that were not
|
|
91
|
+
* changes: selecting what was already selected notified, clearing an already empty selection notified
|
|
92
|
+
* twice, and resolving `defaultSelected` against the data notified at mount. Watching the resolved
|
|
93
|
+
* value reports each transition exactly once, after the commit that made it real.
|
|
94
|
+
*/
|
|
95
|
+
const lastNotifiedSelection = React__namespace.useRef(selected);
|
|
76
96
|
React__namespace.useEffect(() => {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
/** If selected exists, but source item not, remove selection */
|
|
80
|
-
if (selected && !sourceItem) {
|
|
81
|
-
setSelected(undefined);
|
|
97
|
+
if (lastNotifiedSelection.current === selected) {
|
|
98
|
+
return;
|
|
82
99
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
100
|
+
lastNotifiedSelection.current = selected;
|
|
101
|
+
const { current: currentUserDefinedOnSelectChange } = userDefinedOnSelectChange;
|
|
102
|
+
if (typeof currentUserDefinedOnSelectChange === 'function') {
|
|
103
|
+
currentUserDefinedOnSelectChange(selected);
|
|
86
104
|
}
|
|
87
|
-
}, [
|
|
105
|
+
}, [selected, userDefinedOnSelectChange]);
|
|
88
106
|
// ----
|
|
89
|
-
//
|
|
107
|
+
// Unmount Notification
|
|
90
108
|
// ----
|
|
91
|
-
|
|
109
|
+
/**
|
|
110
|
+
* Report the selection as gone when the component unmounts.
|
|
111
|
+
*
|
|
112
|
+
* This was `useUnmountEffect(clearSelected)`, and it notified only as a side effect of how the
|
|
113
|
+
* callback used to be wired: `clearSelected` went through the setter, and the setter notified
|
|
114
|
+
* synchronously. Now that notifying belongs to an effect — and effects do not run on the way out —
|
|
115
|
+
* that call became inert, which dropped a notification every consumer had always received.
|
|
116
|
+
*
|
|
117
|
+
* It fires unconditionally, which is what the old path did: it resolved `undefined` through
|
|
118
|
+
* `getSourceItem` and reported it even when nothing was selected to begin with.
|
|
119
|
+
*/
|
|
120
|
+
useUnmountEffect.useUnmountEffect(() => {
|
|
121
|
+
const { current: currentUserDefinedOnSelectChange } = userDefinedOnSelectChange;
|
|
122
|
+
if (typeof currentUserDefinedOnSelectChange === 'function') {
|
|
123
|
+
currentUserDefinedOnSelectChange(undefined);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
92
126
|
// ----
|
|
93
127
|
// Return utilities
|
|
94
128
|
// ----
|