@applicaster/zapp-react-native-utils 17.0.0-alpha.1984136194 → 17.0.0-alpha.3352797522

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.
@@ -222,30 +222,37 @@ class FocusManager {
222
222
  return () => this.unregisterFocusable(focusableId);
223
223
  }
224
224
 
225
- unregisterFocusable(focusableId: string) {
225
+ unregisterFocusable(component: string | FocusManager.TouchableReactRef) {
226
+ const focusableId = isString(component)
227
+ ? component
228
+ : getFocusableId(component);
229
+
226
230
  const node = this.tree.find(focusableId);
227
231
  const parentNode = this.tree.findParent(focusableId);
228
232
 
233
+ const removesFocusedSubtree =
234
+ this._focusedId === focusableId ||
235
+ !!(this._focusedId && node?.findNode(this._focusedId));
236
+
229
237
  this.focusableComponents = this.focusableComponents.filter(
230
- (c) => c !== node?.component
238
+ (c) => c !== node?.component && getFocusableId(c) !== focusableId
231
239
  );
232
240
 
233
241
  this.tree.remove(focusableId);
234
242
 
235
- if (this._focusedId === focusableId) {
243
+ if (removesFocusedSubtree) {
236
244
  this._focusedId = null;
237
245
  this.retainFocus(parentNode);
238
246
  }
239
247
  }
240
248
 
241
- // Empty groups drop their focusable list after the feed fails, which is
242
- // after Layout's one-shot focus pass. Land on the nearest ancestor that is
243
- // still mounted so key handlers keep a target.
249
+ // Recover before parent-first unmounts discard the focused child's ancestry.
250
+ // Land on a mounted ancestor so key handlers keep a target.
244
251
  private retainFocus(node: TreeNode | null) {
245
252
  let current = node;
246
253
 
247
254
  while (current) {
248
- const ref = current.component;
255
+ const ref = FocusManager.findFocusable(current.id);
249
256
 
250
257
  if (ref?.current?.props?.id) {
251
258
  this.setFocusLocal(ref);
@@ -1,4 +1,7 @@
1
1
  import { focusManager } from "../FocusManager";
2
+ import { Touchable } from "@applicaster/zapp-react-native-ui-components/Components/Focusable/Touchable";
3
+ import { Tree } from "../Tree";
4
+ import { TreeNode } from "../TreeNode";
2
5
 
3
6
  const isFocusableCell = true;
4
7
  const parentFocusableId = "parentFocusableId";
@@ -266,3 +269,112 @@ describe("FocusManager", () => {
266
269
  expect(focusManager.focusedId).toBe("child2");
267
270
  });
268
271
  });
272
+
273
+ describe("FocusManager subtree replacement", () => {
274
+ const createFocusable = (id: string) => ({
275
+ current: new Touchable({ id, children: null, onFocus: jest.fn() }),
276
+ });
277
+
278
+ let root: FocusManager.TouchableReactRef;
279
+ let rail: FocusManager.TouchableReactRef;
280
+ let cell: FocusManager.TouchableReactRef;
281
+ let sibling: FocusManager.TouchableReactRef;
282
+
283
+ const register = (
284
+ touchableRef: FocusManager.TouchableReactRef,
285
+ parentFocusableRef: FocusManager.TouchableReactRef
286
+ ) =>
287
+ focusManager.registerFocusable({
288
+ touchableRef,
289
+ parentFocusableRef,
290
+ isFocusableCell: true,
291
+ parentFocusableId: "",
292
+ });
293
+
294
+ beforeEach(() => {
295
+ root = createFocusable("replacement-root");
296
+ rail = createFocusable("replacement-rail");
297
+ cell = createFocusable("replacement-cell");
298
+ sibling = createFocusable("replacement-sibling");
299
+ register(root, { current: null });
300
+ register(rail, root);
301
+ register(cell, rail);
302
+ register(sibling, root);
303
+ focusManager.setFocus("replacement-cell");
304
+ });
305
+
306
+ afterEach(() => {
307
+ [
308
+ "replacement-cell",
309
+ "replacement-rail",
310
+ "replacement-sibling",
311
+ "replacement-root",
312
+ ].forEach((id) => focusManager.unregisterFocusable(id));
313
+
314
+ focusManager.resetFocus();
315
+ });
316
+
317
+ it("retains focus before a parent-first unmount removes the focused descendant's ancestry", () => {
318
+ focusManager.unregisterFocusable("replacement-rail");
319
+
320
+ expect(focusManager.focusedId).toBe("replacement-root");
321
+ expect(focusManager.focused).toBe(root.current);
322
+
323
+ focusManager.unregisterFocusable("replacement-cell");
324
+
325
+ expect(focusManager.focused).toBe(root.current);
326
+ });
327
+
328
+ it("removes descendant registrations even after their parent removed the tree nodes", () => {
329
+ focusManager.unregisterFocusable("replacement-rail");
330
+ focusManager.unregisterFocusable("replacement-cell");
331
+
332
+ expect(focusManager.focusableComponents).not.toContain(cell);
333
+ expect(focusManager.focusableComponents).not.toContain(rail);
334
+
335
+ const replacementRail = createFocusable("replacement-rail");
336
+ const replacementCell = createFocusable("replacement-cell");
337
+ register(replacementRail, root);
338
+ register(replacementCell, replacementRail);
339
+ focusManager.setFocus("replacement-cell");
340
+
341
+ expect(focusManager.focused).toBe(replacementCell.current);
342
+ expect(focusManager.isFocusableChildOf(replacementCell, root)).toBe(true);
343
+ });
344
+
345
+ it("does not move focus when an unrelated subtree unregisters", () => {
346
+ focusManager.setFocus("replacement-sibling");
347
+ focusManager.unregisterFocusable("replacement-rail");
348
+ focusManager.unregisterFocusable("replacement-cell");
349
+
350
+ expect(focusManager.focused).toBe(sibling.current);
351
+ });
352
+
353
+ it("resolves a placeholder ancestor through its mounted registration", () => {
354
+ const findParent = jest
355
+ .spyOn(Tree.prototype, "findParent")
356
+ .mockReturnValueOnce(new TreeNode(null, "replacement-root", "", false));
357
+
358
+ focusManager.unregisterFocusable("replacement-rail");
359
+ findParent.mockRestore();
360
+
361
+ expect(focusManager.focused).toBe(root.current);
362
+ });
363
+
364
+ it("retains focus through child-first cleanup", () => {
365
+ focusManager.unregisterFocusable("replacement-cell");
366
+ expect(focusManager.focused).toBe(rail.current);
367
+
368
+ focusManager.unregisterFocusable("replacement-rail");
369
+ expect(focusManager.focused).toBe(root.current);
370
+ });
371
+
372
+ it("clears focus when the entire root containing the focused cell unregisters", () => {
373
+ focusManager.unregisterFocusable("replacement-root");
374
+ focusManager.unregisterFocusable("replacement-rail");
375
+ focusManager.unregisterFocusable("replacement-cell");
376
+
377
+ expect(focusManager.focusedId).toBeNull();
378
+ expect(focusManager.focused).toBeNull();
379
+ });
380
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/zapp-react-native-utils",
3
- "version": "17.0.0-alpha.1984136194",
3
+ "version": "17.0.0-alpha.3352797522",
4
4
  "description": "Applicaster Zapp React Native utilities package",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "homepage": "https://github.com/applicaster/quickbrick#readme",
29
29
  "dependencies": {
30
- "@applicaster/applicaster-types": "17.0.0-alpha.1984136194",
30
+ "@applicaster/applicaster-types": "17.0.0-alpha.3352797522",
31
31
  "buffer": "^5.2.1",
32
32
  "camelize": "^1.0.0",
33
33
  "dayjs": "^1.11.10",