-
-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathform.ts
More file actions
35 lines (28 loc) · 864 Bytes
/
form.ts
File metadata and controls
35 lines (28 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { isCancel } from '@clack/prompts';
import type { JSX } from '../types.js';
import { resolveChildren } from '../utils.js';
export interface FormProps {
children?: JSX.Element | JSX.Element[] | string;
}
function isChildLike(child: unknown): child is { name: PropertyKey; value: unknown } {
return typeof child === 'object' && child !== null && 'name' in child && 'value' in child;
}
export function Form(props: FormProps): JSX.Element {
return {
render: async (options) => {
const results: Record<PropertyKey, unknown> = {};
if (props.children) {
const resolvedChildren = await resolveChildren(props.children, options);
for (const child of resolvedChildren) {
if (isCancel(child)) {
continue;
}
if (isChildLike(child)) {
results[child.name] = child.value;
}
}
}
return results;
},
};
}