Skip to content

Commit c20da4c

Browse files
committed
prompt improvements
1 parent b03cdc5 commit c20da4c

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

jsEngine/api/PromptAPI.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,28 @@ export interface InputPromptOptions extends ModalPromptOptions {
6363
*/
6464
content?: string;
6565
/**
66-
* The placeholder text for the input field.
66+
* The placeholder text for the input field. This will show when the input field is empty.
6767
*/
6868
placeholder?: string;
69+
/**
70+
* The initial value of the input field that is pre-filled when the modal is opened.
71+
*/
72+
initialValue?: string;
73+
}
74+
75+
export interface NumberInputPromptOptions extends ModalPromptOptions {
76+
/**
77+
* Text content to display in the modal.
78+
*/
79+
content?: string;
80+
/**
81+
* The placeholder text for the input field. This will show when the input field is empty.
82+
*/
83+
placeholder?: string;
84+
/**
85+
* The initial value of the input field that is pre-filled when the modal is opened.
86+
*/
87+
initialValue?: number;
6988
}
7089

7190
export class PromptAPI {
@@ -225,6 +244,7 @@ export class PromptAPI {
225244
/**
226245
* Prompts the user with a text input dialog.
227246
* Returns the value of the input field, or undefined if the user closes the modal.
247+
* While the input field is focused, the user can use `enter` to submit the value and `esc` to cancel and close the modal.
228248
*
229249
* @example
230250
* ```typescript
@@ -263,6 +283,7 @@ export class PromptAPI {
263283
/**
264284
* Prompts the user with a textarea input dialog.
265285
* Returns the value of the input field, or undefined if the user closes the modal.
286+
* While the input field is focused, the user can use `esc` to cancel and close the modal.
266287
*
267288
* @example
268289
* ```typescript
@@ -302,6 +323,7 @@ export class PromptAPI {
302323
/**
303324
* Prompts the user with a number input dialog.
304325
* Returns the value of the input field, or undefined if the user closes the modal.
326+
* While the input field is focused, the user can use `enter` to submit the value and `esc` to cancel and close the modal.
305327
*
306328
* @example
307329
* ```typescript
@@ -313,7 +335,7 @@ export class PromptAPI {
313335
* });
314336
* ```
315337
*/
316-
public number(options: InputPromptOptions): Promise<number | undefined> {
338+
public number(options: NumberInputPromptOptions): Promise<number | undefined> {
317339
return new Promise<number | undefined>((resolve, reject) => {
318340
try {
319341
new SvelteModal<AnySvelteComponent, unknown>(

jsEngine/api/prompts/InputModalComponent.svelte

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,46 @@
11
<script lang="ts">
22
import Button from 'jsEngine/utils/Button.svelte';
3-
import type { InputPromptOptions } from '../PromptAPI';
3+
import type { InputPromptOptions, NumberInputPromptOptions } from '../PromptAPI';
44
import type { SvelteModal } from './SvelteModal';
55
import { ButtonStyleType } from 'jsEngine/utils/Util';
66
77
const {
88
options,
99
modal,
1010
inputType,
11-
}: {
12-
options: InputPromptOptions;
13-
modal: SvelteModal<any, unknown>;
14-
inputType: 'text' | 'number' | 'textarea';
15-
} = $props();
11+
}:
12+
| {
13+
options: InputPromptOptions;
14+
modal: SvelteModal<any, unknown>;
15+
inputType: 'text' | 'textarea';
16+
}
17+
| {
18+
options: NumberInputPromptOptions;
19+
modal: SvelteModal<any, unknown>;
20+
inputType: 'number';
21+
} = $props();
1622
17-
let value = $state();
23+
let value = $state(options.initialValue);
24+
25+
function onKeydown(event: KeyboardEvent) {
26+
if (event.key === 'Enter' && inputType !== 'textarea') {
27+
modal.submit(value);
28+
}
29+
if (event.key === 'Escape') {
30+
modal.submit(undefined);
31+
}
32+
}
1833
</script>
1934

2035
<p>{options.content}</p>
2136

2237
{#if inputType === 'textarea'}
23-
<textarea style="width: 100%; resize: vertical; height: 200px" bind:value={value} placeholder={options.placeholder ?? 'Text here...'}></textarea>
38+
<textarea style="width: 100%; resize: vertical; height: 200px" bind:value={value} placeholder={options.placeholder ?? 'Text here...'} onkeydown={onKeydown}
39+
></textarea>
2440
{:else if inputType === 'text'}
25-
<input style="width: 100%;" type="text" bind:value={value} placeholder={options.placeholder ?? 'Text here...'} />
41+
<input style="width: 100%;" type="text" bind:value={value} placeholder={options.placeholder ?? 'Text here...'} onkeydown={onKeydown} />
2642
{:else}
27-
<input style="width: 100%;" type="number" bind:value={value} placeholder={options.placeholder ?? 'Number here...'} />
43+
<input style="width: 100%;" type="number" bind:value={value} placeholder={options.placeholder ?? 'Number here...'} onkeydown={onKeydown} />
2844
{/if}
2945

3046
<div class="modal-button-container">

0 commit comments

Comments
 (0)