|
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 |
|
7 | | -import React, { useState, useCallback, useRef } from "react"; |
| 7 | +import { ConfirmInput, Select, TextInput } from "@inkjs/ui"; |
8 | 8 | import { Box, Text, useInput } from "ink"; |
9 | | -import { TextInput, Select, ConfirmInput } from "@inkjs/ui"; |
| 9 | +import React, { useCallback, useRef, useState } from "react"; |
10 | 10 | import type { PromptFieldDescriptor } from "../input/prompt"; |
| 11 | +import { setNestedValue } from "../util"; |
11 | 12 |
|
12 | 13 | interface SchemaPromptAppProps { |
13 | 14 | readonly fields: readonly PromptFieldDescriptor[]; |
14 | 15 | readonly onComplete: (values: Record<string, unknown>) => void; |
15 | 16 | readonly onCancel: () => void; |
16 | 17 | } |
17 | 18 |
|
18 | | -function setNestedValue(obj: Record<string, unknown>, key: string, value: unknown): void { |
19 | | - const parts = key.split("."); |
20 | | - let current: Record<string, unknown> = obj; |
21 | | - |
22 | | - for (let i = 0; i < parts.length - 1; i++) { |
23 | | - if (!(parts[i] in current) || typeof current[parts[i]] !== "object") { |
24 | | - current[parts[i]] = {}; |
25 | | - } |
26 | | - current = current[parts[i]] as Record<string, unknown>; |
27 | | - } |
28 | | - |
29 | | - current[parts[parts.length - 1]] = value; |
30 | | -} |
31 | | - |
32 | 19 | function coercePromptValue(raw: string, field: PromptFieldDescriptor): unknown { |
33 | 20 | switch (field.type) { |
34 | 21 | case "number": |
@@ -110,7 +97,11 @@ function isTextField(field: PromptFieldDescriptor): boolean { |
110 | 97 | return field.type !== "enum" && field.type !== "boolean"; |
111 | 98 | } |
112 | 99 |
|
113 | | -export function SchemaPromptApp({ fields, onComplete, onCancel }: SchemaPromptAppProps): React.ReactElement { |
| 100 | +export function SchemaPromptApp({ |
| 101 | + fields, |
| 102 | + onComplete, |
| 103 | + onCancel, |
| 104 | +}: SchemaPromptAppProps): React.ReactElement { |
114 | 105 | const [focusedIndex, setFocusedIndex] = useState(0); |
115 | 106 | const valuesRef = useRef<Record<string, unknown>>({}); |
116 | 107 | const rawValuesRef = useRef<Record<string, string>>({}); |
@@ -290,7 +281,11 @@ function FieldWidget({ |
290 | 281 | if (field.type === "enum" && field.enumValues) { |
291 | 282 | const options = field.enumValues.map((v) => ({ label: v, value: v })); |
292 | 283 | return ( |
293 | | - <Select options={options} defaultValue={previousValue} onChange={(value) => onSubmit(value)} /> |
| 284 | + <Select |
| 285 | + options={options} |
| 286 | + defaultValue={previousValue} |
| 287 | + onChange={(value) => onSubmit(value)} |
| 288 | + /> |
294 | 289 | ); |
295 | 290 | } |
296 | 291 |
|
|
0 commit comments