-
-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathselect.ts
More file actions
35 lines (30 loc) · 996 Bytes
/
select.ts
File metadata and controls
35 lines (30 loc) · 996 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 type { Option, SelectOptions } from '@clack/prompts';
import { select } from '@clack/prompts';
import type { JSX } from '../types.js';
import { resolveChildren } from '../utils.js';
export interface SelectProps extends Omit<SelectOptions<unknown>, 'options'> {
children: JSX.Element[] | JSX.Element | string;
}
const isOptionLike = (obj: unknown): obj is Option<unknown> => {
return obj !== null && typeof obj === 'object' && Object.hasOwnProperty.call(obj, 'value');
};
export function Select(props: SelectProps): JSX.Element {
return {
render: async (renderOptions) => {
const { children, ...opts } = props;
const options: Option<unknown>[] = [];
const resolvedChildren = await resolveChildren(props.children, renderOptions);
for (const child of resolvedChildren) {
if (isOptionLike(child)) {
options.push(child);
}
}
return select({
input: renderOptions?.input,
output: renderOptions?.output,
...opts,
options,
});
},
};
}