Skip to content

Commit 615c034

Browse files
tonyespinoza1claude
authored andcommitted
Add nav-test pattern demonstrating navigateTo with self-instantiation
Simple example pattern showing: - Text field with bidirectional binding using Cell.of() - Button with handler that creates new instance of itself - Uses navigateTo() to navigate to the new charm with custom name 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 2256b75 commit 615c034

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

packages/patterns/nav-test.tsx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/// <cts-enable />
2+
import { Cell, Default, handler, NAME, navigateTo, pattern, str, UI } from "commontools";
3+
4+
/**
5+
* Simple Nav Test Pattern
6+
*
7+
* Features:
8+
* - Text field to enter a name
9+
* - Button that creates a new instance of this pattern with that name
10+
* - Uses navigateTo to navigate to the new instance
11+
*/
12+
13+
interface Input {
14+
label: Default<string, "Unnamed">;
15+
}
16+
17+
// Need to define the pattern first, then reference it in the handler
18+
// Using a forward declaration approach
19+
20+
// Handler to create a new instance and navigate to it
21+
const createAndNavigate = handler<
22+
unknown,
23+
{ inputText: Cell<string> }
24+
>(
25+
(_event, { inputText }) => {
26+
const name = inputText.get().trim();
27+
if (!name) {
28+
console.log("[Handler] No name entered, skipping navigation");
29+
return;
30+
}
31+
32+
console.log("[Handler] Creating new instance with name:", name);
33+
34+
// Create a new instance of this pattern with the entered name
35+
const newInstance = NavTestPattern({
36+
label: name,
37+
});
38+
39+
console.log("[Handler] Navigating to new instance...");
40+
return navigateTo(newInstance);
41+
}
42+
);
43+
44+
// The main pattern
45+
const NavTestPattern = pattern<Input, Input>(
46+
({ label }) => {
47+
// Local cell to hold the text input value
48+
const inputText = Cell.of("");
49+
50+
return {
51+
[NAME]: str`Nav Test: ${label}`,
52+
[UI]: (
53+
<div style={{ padding: "2rem", maxWidth: "400px", margin: "0 auto" }}>
54+
<h2 style={{ marginBottom: "1rem" }}>
55+
Current: {label}
56+
</h2>
57+
58+
<div style={{ marginBottom: "1rem" }}>
59+
<label style={{ display: "block", marginBottom: "0.5rem", fontWeight: "500" }}>
60+
Enter name for new instance:
61+
</label>
62+
<ct-input
63+
$value={inputText}
64+
placeholder="Type a name..."
65+
style="width: 100%; padding: 0.5rem; border: 1px solid #ccc; border-radius: 4px;"
66+
/>
67+
</div>
68+
69+
<ct-button
70+
onClick={createAndNavigate({ inputText })}
71+
style="background-color: #3b82f6; color: white; padding: 0.75rem 1.5rem; border-radius: 6px; font-weight: 600; cursor: pointer;"
72+
>
73+
Create & Navigate
74+
</ct-button>
75+
76+
<div style={{ marginTop: "1rem", padding: "1rem", backgroundColor: "#f3f4f6", borderRadius: "4px", fontSize: "0.875rem" }}>
77+
Click the button to create a new instance of this pattern named with the text you entered.
78+
</div>
79+
</div>
80+
),
81+
label,
82+
};
83+
}
84+
);
85+
86+
export default NavTestPattern;

0 commit comments

Comments
 (0)