@daltonr/pathwrite-react 0.1.4 → 0.1.5
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/package.json +3 -2
- package/src/index.ts +368 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@daltonr/pathwrite-react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "React adapter for @daltonr/pathwrite-core — hooks, context provider, and optional <PathShell> default UI.",
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"types": "dist/index.d.ts",
|
|
33
33
|
"files": [
|
|
34
34
|
"dist",
|
|
35
|
+
"src",
|
|
35
36
|
"README.md",
|
|
36
37
|
"LICENSE"
|
|
37
38
|
],
|
|
@@ -44,7 +45,7 @@
|
|
|
44
45
|
"react": ">=18.0.0"
|
|
45
46
|
},
|
|
46
47
|
"dependencies": {
|
|
47
|
-
"@daltonr/pathwrite-core": "^0.1.
|
|
48
|
+
"@daltonr/pathwrite-core": "^0.1.5"
|
|
48
49
|
},
|
|
49
50
|
"devDependencies": {
|
|
50
51
|
"react": "^18.3.1",
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
createElement,
|
|
4
|
+
useCallback,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useRef,
|
|
8
|
+
useSyncExternalStore
|
|
9
|
+
} from "react";
|
|
10
|
+
import type { PropsWithChildren, ReactElement, ReactNode } from "react";
|
|
11
|
+
import {
|
|
12
|
+
PathData,
|
|
13
|
+
PathDefinition,
|
|
14
|
+
PathEngine,
|
|
15
|
+
PathEvent,
|
|
16
|
+
PathSnapshot
|
|
17
|
+
} from "@daltonr/pathwrite-core";
|
|
18
|
+
|
|
19
|
+
// ---------------------------------------------------------------------------
|
|
20
|
+
// Types
|
|
21
|
+
// ---------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
export interface UsePathOptions {
|
|
24
|
+
/** Called for every engine event (stateChanged, completed, cancelled, resumed). */
|
|
25
|
+
onEvent?: (event: PathEvent) => void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface UsePathReturn<TData extends PathData = PathData> {
|
|
29
|
+
/** Current path snapshot, or `null` when no path is active. Triggers a React re-render on change. */
|
|
30
|
+
snapshot: PathSnapshot<TData> | null;
|
|
31
|
+
/** Start (or restart) a path. */
|
|
32
|
+
start: (path: PathDefinition, initialData?: PathData) => void;
|
|
33
|
+
/** Push a sub-path onto the stack. Requires an active path. */
|
|
34
|
+
startSubPath: (path: PathDefinition, initialData?: PathData) => void;
|
|
35
|
+
/** Advance one step. Completes the path on the last step. */
|
|
36
|
+
next: () => void;
|
|
37
|
+
/** Go back one step. Cancels the path from the first step. */
|
|
38
|
+
previous: () => void;
|
|
39
|
+
/** Cancel the active path (or sub-path). */
|
|
40
|
+
cancel: () => void;
|
|
41
|
+
/** Jump directly to a step by ID. Calls onLeave / onEnter but bypasses guards and shouldSkip. */
|
|
42
|
+
goToStep: (stepId: string) => void;
|
|
43
|
+
/** Update a single data value; triggers a re-render via stateChanged. When `TData` is specified, `key` and `value` are type-checked against your data shape. */
|
|
44
|
+
setData: <K extends string & keyof TData>(key: K, value: TData[K]) => void;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type PathProviderProps = PropsWithChildren<{
|
|
48
|
+
/** Forwarded to the internal usePath hook. */
|
|
49
|
+
onEvent?: (event: PathEvent) => void;
|
|
50
|
+
}>;
|
|
51
|
+
|
|
52
|
+
// ---------------------------------------------------------------------------
|
|
53
|
+
// usePath hook
|
|
54
|
+
// ---------------------------------------------------------------------------
|
|
55
|
+
|
|
56
|
+
export function usePath<TData extends PathData = PathData>(options?: UsePathOptions): UsePathReturn<TData> {
|
|
57
|
+
// Stable engine instance for the lifetime of the hook
|
|
58
|
+
const engineRef = useRef<PathEngine | null>(null);
|
|
59
|
+
if (engineRef.current === null) {
|
|
60
|
+
engineRef.current = new PathEngine();
|
|
61
|
+
}
|
|
62
|
+
const engine = engineRef.current;
|
|
63
|
+
|
|
64
|
+
// Keep the onEvent callback current without changing the subscribe identity
|
|
65
|
+
const onEventRef = useRef(options?.onEvent);
|
|
66
|
+
onEventRef.current = options?.onEvent;
|
|
67
|
+
|
|
68
|
+
// Cached snapshot — updated only inside the subscribe callback
|
|
69
|
+
const snapshotRef = useRef<PathSnapshot<TData> | null>(null);
|
|
70
|
+
|
|
71
|
+
const subscribe = useCallback(
|
|
72
|
+
(callback: () => void) =>
|
|
73
|
+
engine.subscribe((event: PathEvent) => {
|
|
74
|
+
if (event.type === "stateChanged" || event.type === "resumed") {
|
|
75
|
+
snapshotRef.current = event.snapshot as PathSnapshot<TData>;
|
|
76
|
+
} else if (event.type === "completed" || event.type === "cancelled") {
|
|
77
|
+
snapshotRef.current = null;
|
|
78
|
+
}
|
|
79
|
+
onEventRef.current?.(event);
|
|
80
|
+
callback();
|
|
81
|
+
}),
|
|
82
|
+
[engine]
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
const getSnapshot = useCallback(() => snapshotRef.current, []);
|
|
86
|
+
|
|
87
|
+
const snapshot = useSyncExternalStore(subscribe, getSnapshot);
|
|
88
|
+
|
|
89
|
+
// Stable action callbacks
|
|
90
|
+
const start = useCallback(
|
|
91
|
+
(path: PathDefinition, initialData: PathData = {}) =>
|
|
92
|
+
engine.start(path, initialData),
|
|
93
|
+
[engine]
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const startSubPath = useCallback(
|
|
97
|
+
(path: PathDefinition, initialData: PathData = {}) =>
|
|
98
|
+
engine.startSubPath(path, initialData),
|
|
99
|
+
[engine]
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const next = useCallback(() => engine.next(), [engine]);
|
|
103
|
+
const previous = useCallback(() => engine.previous(), [engine]);
|
|
104
|
+
const cancel = useCallback(() => engine.cancel(), [engine]);
|
|
105
|
+
|
|
106
|
+
const goToStep = useCallback(
|
|
107
|
+
(stepId: string) => engine.goToStep(stepId),
|
|
108
|
+
[engine]
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const setData = useCallback(
|
|
112
|
+
<K extends string & keyof TData>(key: K, value: TData[K]) => engine.setData(key, value as unknown),
|
|
113
|
+
[engine]
|
|
114
|
+
) as UsePathReturn<TData>["setData"];
|
|
115
|
+
|
|
116
|
+
return { snapshot, start, startSubPath, next, previous, cancel, goToStep, setData };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// ---------------------------------------------------------------------------
|
|
120
|
+
// Context + Provider
|
|
121
|
+
// ---------------------------------------------------------------------------
|
|
122
|
+
|
|
123
|
+
const PathContext = createContext<UsePathReturn | null>(null);
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Provides a single `usePath` instance to all descendants.
|
|
127
|
+
* Consume with `usePathContext()`.
|
|
128
|
+
*/
|
|
129
|
+
export function PathProvider({ children, onEvent }: PathProviderProps): ReactElement {
|
|
130
|
+
const path = usePath({ onEvent });
|
|
131
|
+
return createElement(PathContext.Provider, { value: path }, children);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Access the nearest `PathProvider`'s path instance.
|
|
136
|
+
* Throws if used outside of a `<PathProvider>`.
|
|
137
|
+
*
|
|
138
|
+
* The optional generic narrows `snapshot.data` for convenience — it is a
|
|
139
|
+
* **type-level assertion**, not a runtime guarantee.
|
|
140
|
+
*/
|
|
141
|
+
export function usePathContext<TData extends PathData = PathData>(): UsePathReturn<TData> {
|
|
142
|
+
const ctx = useContext(PathContext);
|
|
143
|
+
if (ctx === null) {
|
|
144
|
+
throw new Error("usePathContext must be used within a <PathProvider>.");
|
|
145
|
+
}
|
|
146
|
+
return ctx as UsePathReturn<TData>;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// ---------------------------------------------------------------------------
|
|
150
|
+
// Default UI — PathShell
|
|
151
|
+
// ---------------------------------------------------------------------------
|
|
152
|
+
|
|
153
|
+
export interface PathShellProps {
|
|
154
|
+
/** The path definition to drive. */
|
|
155
|
+
path: PathDefinition;
|
|
156
|
+
/** Map of step ID → content. The shell renders `steps[snapshot.stepId]` for the current step. */
|
|
157
|
+
steps: Record<string, ReactNode>;
|
|
158
|
+
/** Initial data passed to `engine.start()`. */
|
|
159
|
+
initialData?: PathData;
|
|
160
|
+
/** If true, the path is started automatically on mount. Defaults to `true`. */
|
|
161
|
+
autoStart?: boolean;
|
|
162
|
+
/** Called when the path completes. Receives the final data. */
|
|
163
|
+
onComplete?: (data: PathData) => void;
|
|
164
|
+
/** Called when the path is cancelled. Receives the data at time of cancellation. */
|
|
165
|
+
onCancel?: (data: PathData) => void;
|
|
166
|
+
/** Called for every engine event. */
|
|
167
|
+
onEvent?: (event: PathEvent) => void;
|
|
168
|
+
/** Label for the Back button. Defaults to `"Back"`. */
|
|
169
|
+
backLabel?: string;
|
|
170
|
+
/** Label for the Next button. Defaults to `"Next"`. */
|
|
171
|
+
nextLabel?: string;
|
|
172
|
+
/** Label for the Finish button (shown on the last step). Defaults to `"Finish"`. */
|
|
173
|
+
finishLabel?: string;
|
|
174
|
+
/** Label for the Cancel button. Defaults to `"Cancel"`. */
|
|
175
|
+
cancelLabel?: string;
|
|
176
|
+
/** If true, hide the Cancel button. Defaults to `false`. */
|
|
177
|
+
hideCancel?: boolean;
|
|
178
|
+
/** If true, hide the progress indicator. Defaults to `false`. */
|
|
179
|
+
hideProgress?: boolean;
|
|
180
|
+
/** Optional extra CSS class on the root element. */
|
|
181
|
+
className?: string;
|
|
182
|
+
/** Render prop to replace the entire header (progress area). Receives the snapshot. */
|
|
183
|
+
renderHeader?: (snapshot: PathSnapshot) => ReactNode;
|
|
184
|
+
/** Render prop to replace the entire footer (navigation area). Receives the snapshot and actions. */
|
|
185
|
+
renderFooter?: (snapshot: PathSnapshot, actions: PathShellActions) => ReactNode;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface PathShellActions {
|
|
189
|
+
next: () => void;
|
|
190
|
+
previous: () => void;
|
|
191
|
+
cancel: () => void;
|
|
192
|
+
goToStep: (stepId: string) => void;
|
|
193
|
+
setData: (key: string, value: unknown) => void;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Default UI shell that renders a progress indicator, step content, and navigation
|
|
198
|
+
* buttons. Pass a `steps` map to define per-step content.
|
|
199
|
+
*
|
|
200
|
+
* ```tsx
|
|
201
|
+
* <PathShell
|
|
202
|
+
* path={myPath}
|
|
203
|
+
* initialData={{ name: "" }}
|
|
204
|
+
* onComplete={handleDone}
|
|
205
|
+
* steps={{
|
|
206
|
+
* details: <DetailsForm />,
|
|
207
|
+
* review: <ReviewPanel />,
|
|
208
|
+
* }}
|
|
209
|
+
* />
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
export function PathShell({
|
|
213
|
+
path: pathDef,
|
|
214
|
+
steps,
|
|
215
|
+
initialData = {},
|
|
216
|
+
autoStart = true,
|
|
217
|
+
onComplete,
|
|
218
|
+
onCancel,
|
|
219
|
+
onEvent,
|
|
220
|
+
backLabel = "Back",
|
|
221
|
+
nextLabel = "Next",
|
|
222
|
+
finishLabel = "Finish",
|
|
223
|
+
cancelLabel = "Cancel",
|
|
224
|
+
hideCancel = false,
|
|
225
|
+
hideProgress = false,
|
|
226
|
+
className,
|
|
227
|
+
renderHeader,
|
|
228
|
+
renderFooter,
|
|
229
|
+
}: PathShellProps): ReactElement {
|
|
230
|
+
const pathReturn = usePath({
|
|
231
|
+
onEvent(event) {
|
|
232
|
+
onEvent?.(event);
|
|
233
|
+
if (event.type === "completed") onComplete?.(event.data);
|
|
234
|
+
if (event.type === "cancelled") onCancel?.(event.data);
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
const { snapshot, start, next, previous, cancel, goToStep, setData } = pathReturn;
|
|
239
|
+
|
|
240
|
+
// Auto-start on mount
|
|
241
|
+
const startedRef = useRef(false);
|
|
242
|
+
useEffect(() => {
|
|
243
|
+
if (autoStart && !startedRef.current) {
|
|
244
|
+
startedRef.current = true;
|
|
245
|
+
start(pathDef, initialData);
|
|
246
|
+
}
|
|
247
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
248
|
+
}, []);
|
|
249
|
+
|
|
250
|
+
// Look up step content from the steps map
|
|
251
|
+
const stepContent = snapshot ? (steps[snapshot.stepId] ?? null) : null;
|
|
252
|
+
|
|
253
|
+
if (!snapshot) {
|
|
254
|
+
return createElement(PathContext.Provider, { value: pathReturn },
|
|
255
|
+
createElement("div", { className: cls("pw-shell", className) },
|
|
256
|
+
createElement("div", { className: "pw-shell__empty" },
|
|
257
|
+
createElement("p", null, "No active path."),
|
|
258
|
+
!autoStart && createElement("button", {
|
|
259
|
+
type: "button",
|
|
260
|
+
className: "pw-shell__start-btn",
|
|
261
|
+
onClick: () => start(pathDef, initialData)
|
|
262
|
+
}, "Start")
|
|
263
|
+
)
|
|
264
|
+
)
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const actions: PathShellActions = { next, previous, cancel, goToStep, setData };
|
|
269
|
+
|
|
270
|
+
return createElement(PathContext.Provider, { value: pathReturn },
|
|
271
|
+
createElement("div", { className: cls("pw-shell", className) },
|
|
272
|
+
// Header — progress indicator
|
|
273
|
+
!hideProgress && (renderHeader
|
|
274
|
+
? renderHeader(snapshot)
|
|
275
|
+
: defaultHeader(snapshot)),
|
|
276
|
+
// Body — step content
|
|
277
|
+
createElement("div", { className: "pw-shell__body" }, stepContent),
|
|
278
|
+
// Footer — navigation buttons
|
|
279
|
+
renderFooter
|
|
280
|
+
? renderFooter(snapshot, actions)
|
|
281
|
+
: defaultFooter(snapshot, actions, {
|
|
282
|
+
backLabel, nextLabel, finishLabel, cancelLabel, hideCancel
|
|
283
|
+
})
|
|
284
|
+
)
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// ---------------------------------------------------------------------------
|
|
289
|
+
// Default header (progress indicator)
|
|
290
|
+
// ---------------------------------------------------------------------------
|
|
291
|
+
|
|
292
|
+
function defaultHeader(snapshot: PathSnapshot): ReactElement {
|
|
293
|
+
return createElement("div", { className: "pw-shell__header" },
|
|
294
|
+
createElement("div", { className: "pw-shell__steps" },
|
|
295
|
+
...snapshot.steps.map((step, i) =>
|
|
296
|
+
createElement("div", {
|
|
297
|
+
key: step.id,
|
|
298
|
+
className: cls("pw-shell__step", `pw-shell__step--${step.status}`)
|
|
299
|
+
},
|
|
300
|
+
createElement("span", { className: "pw-shell__step-dot" },
|
|
301
|
+
step.status === "completed" ? "✓" : String(i + 1)
|
|
302
|
+
),
|
|
303
|
+
createElement("span", { className: "pw-shell__step-label" },
|
|
304
|
+
step.title ?? step.id
|
|
305
|
+
)
|
|
306
|
+
)
|
|
307
|
+
)
|
|
308
|
+
),
|
|
309
|
+
createElement("div", { className: "pw-shell__track" },
|
|
310
|
+
createElement("div", {
|
|
311
|
+
className: "pw-shell__track-fill",
|
|
312
|
+
style: { width: `${snapshot.progress * 100}%` }
|
|
313
|
+
})
|
|
314
|
+
)
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// ---------------------------------------------------------------------------
|
|
319
|
+
// Default footer (navigation buttons)
|
|
320
|
+
// ---------------------------------------------------------------------------
|
|
321
|
+
|
|
322
|
+
interface FooterLabels {
|
|
323
|
+
backLabel: string;
|
|
324
|
+
nextLabel: string;
|
|
325
|
+
finishLabel: string;
|
|
326
|
+
cancelLabel: string;
|
|
327
|
+
hideCancel: boolean;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function defaultFooter(
|
|
331
|
+
snapshot: PathSnapshot,
|
|
332
|
+
actions: PathShellActions,
|
|
333
|
+
labels: FooterLabels
|
|
334
|
+
): ReactElement {
|
|
335
|
+
return createElement("div", { className: "pw-shell__footer" },
|
|
336
|
+
createElement("div", { className: "pw-shell__footer-left" },
|
|
337
|
+
!snapshot.isFirstStep && createElement("button", {
|
|
338
|
+
type: "button",
|
|
339
|
+
className: "pw-shell__btn pw-shell__btn--back",
|
|
340
|
+
disabled: snapshot.isNavigating || !snapshot.canMovePrevious,
|
|
341
|
+
onClick: actions.previous
|
|
342
|
+
}, labels.backLabel)
|
|
343
|
+
),
|
|
344
|
+
createElement("div", { className: "pw-shell__footer-right" },
|
|
345
|
+
!labels.hideCancel && createElement("button", {
|
|
346
|
+
type: "button",
|
|
347
|
+
className: "pw-shell__btn pw-shell__btn--cancel",
|
|
348
|
+
disabled: snapshot.isNavigating,
|
|
349
|
+
onClick: actions.cancel
|
|
350
|
+
}, labels.cancelLabel),
|
|
351
|
+
createElement("button", {
|
|
352
|
+
type: "button",
|
|
353
|
+
className: "pw-shell__btn pw-shell__btn--next",
|
|
354
|
+
disabled: snapshot.isNavigating || !snapshot.canMoveNext,
|
|
355
|
+
onClick: actions.next
|
|
356
|
+
}, snapshot.isLastStep ? labels.finishLabel : labels.nextLabel)
|
|
357
|
+
)
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// ---------------------------------------------------------------------------
|
|
362
|
+
// Helpers
|
|
363
|
+
// ---------------------------------------------------------------------------
|
|
364
|
+
|
|
365
|
+
|
|
366
|
+
function cls(...parts: (string | undefined | false | null)[]): string {
|
|
367
|
+
return parts.filter(Boolean).join(" ");
|
|
368
|
+
}
|