2.3.7 • Published 11 months ago
@jarboer/electron-prompt v2.3.7
electron-prompt
Electron helper to prompt for a value via input or select
Installation
npm i @jarboer/electron-prompt
Examples
See basic-prompt-example for more an example program.
Example 1 - Using betterPrompt()
import { app, BrowserWindow } from 'electron';
import { betterPrompt } from 'electron-prompt';
let mainWindow: BrowserWindow | null;
// Electron window setup code...
// Wait for the prompt to close before continuing execution of this code section (block)
const result = await betterPrompt({
title: 'Select an Option',
subtitle: 'Please select an option',
type: 'select',
selectOptions: {
'1': 'Option 1',
'2': 'Option 2',
'3': 'Option 3',
'4': 'Option 4',
'5': 'Option 5',
'6': 'Option 6',
'7': 'Option 7',
'8': 'Option 8',
'9': 'Option 9',
'10': 'Option 10'
}
}, mainWindow);
if (result) {
console.log("Result:", result);
}
Example 2 - Using prompt()
import { prompt } from 'electron-prompt';
prompt({
title: 'Prompt example',
subtitle: 'URL:',
type: 'input'
inputTextOptions: [
{
value: 'http://example.org',
inputAttrs: {
type: "url",
required: false
}
}
],
})
.then((r: any) => {
if(r === null) {
console.log('user cancelled');
} else {
console.log('result', r);
}
})
.catch(console.error);
Documentation
Primary method:
const result = betterPrompt(promptOptions, window);
Old primary method:
prompt(options, parentBrowserWindow).then(...).catch(...);
Options object (optional)
If not supplied, it uses the defaults listed in the table below.
ElectronPromptOptions
Used to define the prompt's information object
InputElement
Used to define an input element's type, the options are: 'button', 'checkbox', 'color', 'date', 'datetime-local', 'email', 'file', 'hidden', 'image', 'month', 'number', 'password', 'radio', 'range', 'reset', 'search', 'submit', 'tel', 'text', 'time', 'url', 'week'.
StringDictionary
Used to define a string dictionary where the key and value are strings.
Type def: [key: string]: string;
Example:
{
'value1': 'hi',
'value2': 'hey',
'value3': 'hello'
}
LabelData
Key | Optional? | Type | Explanation |
---|---|---|---|
content | true | string | The label which appears on the prompt for the input field. Defaults to nothing/undefined. |
htmlFor | true | string | Defines which html element the label is for. Defaults to nothing/undefined. |
useHtmlLabel | true | boolean | Whether the label should be interpreted as HTML or not. Defaults to false. |
InputAttrs
Key | Optional? | Type | Explanation |
---|---|---|---|
type | false | InputElement | Define the input element's type. |
required | false | boolean | Define if the input element is required to be completed in a form. |
InputData
Key | Optional? | Type | Explanation |
---|---|---|---|
id | true | string | The id for the input element which appears on the prompt for the input field. The name will also be set to this (for labels). Defaults to nothing/undefined. |
placeholder | true | string | The placeholder which appears on the prompt for the input field. Defaults to being blank/undefined. |
value | true | string | The default value for the input field. Defaults to being blank/undefined. |
inputAttrs | true | InputAttrs | The attributes of the input field, analogous to the HTML attributes: {type: 'text', required: true} -> <input type="text" required> . Used if the type is 'input'. |
ButtonLabels
Key | Optional? | Type | Explanation |
---|---|---|---|
submit | true | string | Define the display text for the submit button. |
cancel | true | string | Define the display text for the cancel button. |
parentBrowserWindow (optional)
The window in which to display the prompt on. If not supplied, the parent window of the prompt will be null.