Skip to content

Commit 961fd9d

Browse files
committed
fix(app-shell): keep the Tab visibility name input across keystrokes
Each row was keyed by the tab name its input edits, so every keystroke remounted the row and the input lost focus after the first character. Rows are now keyed by position, and the name input buffers what is typed, writing the rename only while the name is no other row's (a name passing through another row's merged the two rows and dropped one). Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JQwxbXbEWQehrHjkZUViYN
1 parent 7eb613c commit 961fd9d

2 files changed

Lines changed: 156 additions & 7 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* A Tab visibility row keeps its name input across keystrokes (objectui#26748).
5+
*
6+
* Each row was keyed by the tab name its input edits, so every keystroke
7+
* renamed the key, remounted the row, and the input lost focus after the first
8+
* character: typing `showcase_app` stored `{"s":"hidden"}`.
9+
*
10+
* What is pinned:
11+
* - a name typed one key at a time (not pasted) into a new row keeps focus
12+
* and lands whole in `tabPermissions`, with the row's visibility;
13+
* - renaming an existing row keeps the other rows and their order;
14+
* - a name that passes through another row's name merges nothing, and the
15+
* row writes again once the name is free;
16+
* - the remove button drops only its own row.
17+
*
18+
* DIRECTION: with `key={tab}` restored the typed-name pin is red (focus leaves
19+
* after the first key).
20+
*/
21+
22+
import "@testing-library/jest-dom/vitest";
23+
import * as React from "react";
24+
import { afterEach, describe, expect, it } from "vitest";
25+
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
26+
import userEvent from "@testing-library/user-event";
27+
import { PermissionAdvancedFacets } from "./PermissionAdvancedFacets";
28+
29+
afterEach(cleanup);
30+
31+
const t = (k: string) => k;
32+
33+
type Draft = Record<string, unknown>;
34+
35+
/** A stateful host, so each write re-renders the facets like the editor does. */
36+
function Host({
37+
initial,
38+
onDraft,
39+
}: {
40+
initial: Draft;
41+
onDraft: (d: Draft) => void;
42+
}) {
43+
const [draft, setDraft] = React.useState<Draft>(initial);
44+
React.useEffect(() => {
45+
onDraft(draft);
46+
}, [draft, onDraft]);
47+
return (
48+
<PermissionAdvancedFacets
49+
draft={draft}
50+
setDraft={setDraft}
51+
writable
52+
allSetNames={[]}
53+
t={t}
54+
/>
55+
);
56+
}
57+
58+
function mount(tabPermissions: Record<string, string>) {
59+
let latest: Draft = {};
60+
render(
61+
<Host
62+
initial={{ tabPermissions }}
63+
onDraft={(d) => {
64+
latest = d;
65+
}}
66+
/>,
67+
);
68+
fireEvent.click(screen.getByText("perm.tabs.title"));
69+
return { tabs: () => latest.tabPermissions };
70+
}
71+
72+
describe("a Tab visibility row keeps its name input across keystrokes (objectui#26748)", () => {
73+
it("stores a name typed key by key into a new row, focus kept", async () => {
74+
const user = userEvent.setup();
75+
const { tabs } = mount({ "": "hidden" });
76+
const input = screen.getByDisplayValue("");
77+
await user.click(input);
78+
await user.type(input, "showcase_app");
79+
expect(document.activeElement).toBe(input);
80+
expect(input).toHaveValue("showcase_app");
81+
expect(tabs()).toEqual({ showcase_app: "hidden" });
82+
});
83+
84+
it("renames an existing row, keeping the others in order", async () => {
85+
const user = userEvent.setup();
86+
const { tabs } = mount({ a_tab: "hidden", b_tab: "visible" });
87+
const input = screen.getByDisplayValue("a_tab");
88+
await user.type(input, "_x");
89+
expect(document.activeElement).toBe(input);
90+
expect(JSON.stringify(tabs())).toBe(
91+
'{"a_tab_x":"hidden","b_tab":"visible"}',
92+
);
93+
});
94+
95+
it("merges nothing while the typed name is another row’s", async () => {
96+
const user = userEvent.setup();
97+
const { tabs } = mount({ app: "visible", "": "hidden" });
98+
const input = screen.getByDisplayValue("");
99+
await user.type(input, "app");
100+
expect(input).toHaveValue("app");
101+
expect(JSON.stringify(tabs())).toBe('{"app":"visible","ap":"hidden"}');
102+
await user.type(input, "_x");
103+
expect(JSON.stringify(tabs())).toBe('{"app":"visible","app_x":"hidden"}');
104+
expect(screen.getByDisplayValue("app")).not.toBe(input);
105+
});
106+
107+
it("removes only its own row", () => {
108+
const { tabs } = mount({ a_tab: "hidden", b_tab: "visible" });
109+
fireEvent.click(screen.getAllByLabelText("perm.tabs.remove")[0]!);
110+
expect(tabs()).toEqual({ b_tab: "visible" });
111+
expect(screen.getByDisplayValue("b_tab")).toBeInTheDocument();
112+
});
113+
});

‎ui/packages/app-shell/src/views/metadata-admin/PermissionAdvancedFacets.tsx‎

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -745,18 +745,20 @@ export function PermissionAdvancedFacets({
745745
>
746746
<p className="text-xs text-muted-foreground mb-3">{t('perm.tabs.help')}</p>
747747
<div className="space-y-2">
748-
{tabEntries.map(([tab, vis]) => (
749-
<div key={tab} className="flex items-center gap-2">
750-
<Input
751-
value={tab}
748+
{tabEntries.map(([tab, vis], row) => (
749+
// objectui#26748 — keyed by position, not by the name the input
750+
// edits: a rename keeps insertion order, so the row (and the
751+
// input's focus) survives each keystroke.
752+
<div key={row} className="flex items-center gap-2">
753+
<TabNameInput
754+
name={tab}
752755
disabled={!writable}
753-
onChange={(e) => {
754-
const nextKey = e.target.value;
756+
taken={(name) => tabEntries.some(([k], j) => j !== row && k === name)}
757+
onRename={(nextKey) => {
755758
const next: TabPerms = {};
756759
for (const [k, v] of tabEntries) next[k === tab ? nextKey : k] = v;
757760
setTabs(next);
758761
}}
759-
className="h-8 w-64"
760762
/>
761763
<FacetPicker
762764
testId={`tab-visibility-${tab}`}
@@ -935,3 +937,37 @@ export function PermissionAdvancedFacets({
935937
}
936938

937939
export default PermissionAdvancedFacets;
940+
941+
/**
942+
* objectui#26748 — a Tab visibility row's name input. It holds what is typed
943+
* and writes the rename only while the name is no other row's: a name that
944+
* passes through (or ends at) another row's would otherwise merge the two
945+
* rows in `tabPermissions` and drop one.
946+
*/
947+
function TabNameInput({
948+
name,
949+
disabled,
950+
taken,
951+
onRename,
952+
}: {
953+
name: string;
954+
disabled: boolean;
955+
taken: (name: string) => boolean;
956+
onRename: (name: string) => void;
957+
}) {
958+
const [text, setText] = React.useState(name);
959+
React.useEffect(() => setText(name), [name]);
960+
return (
961+
<Input
962+
value={text}
963+
disabled={disabled}
964+
aria-invalid={text !== name || undefined}
965+
onChange={(e) => {
966+
const next = e.target.value;
967+
setText(next);
968+
if (next !== name && !taken(next)) onRename(next);
969+
}}
970+
className="h-8 w-64"
971+
/>
972+
);
973+
}

0 commit comments

Comments
 (0)