Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexible multiInput #6 #7

Merged
merged 7 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 59 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Custom Electron Prompt

[![NPM Version](https://img.shields.io/npm/v/custom-electron-prompt)](https://www.npmjs.com/package/custom-electron-prompt)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/Araxeus/custom-electron-prompt/blob/main/LICENSE)
[![NPM Version](https://img.shields.io/npm/v/custom-electron-prompt)](https://www.npmjs.com/package/custom-electron-prompt)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/Araxeus/custom-electron-prompt/blob/main/LICENSE)
[![Maintenance](https://img.shields.io/badge/Maintained%3F-yes-green.svg)](https://github.com/Araxeus/custom-electron-prompt)
[![Website shields.io](https://img.shields.io/website-up-down-green-red/http/shields.io.svg)](https://araxeus.github.io/custom-electron-prompt)

Expand All @@ -19,7 +19,7 @@ There is also an option for a button with user-defined `onclick` function.

## Usage

* 1: Install the npm package to your project directory with
* 1: Install the npm package to your project directory with
```bash
npm install custom-electron-prompt
```
Expand All @@ -29,19 +29,19 @@ There is also an option for a button with user-defined `onclick` function.
```

* 2: Import prompt

```javascript
const prompt = require('custom-electron-prompt')
```

* 3: Create a prompt

```javascript
prompt([options, parentBrowserWindow])
```

calling the prompt function returns a Promise

Promise resolve returns the input or returns null if prompt was canceled

       On error, Prompise reject returns custom error message
Expand Down Expand Up @@ -87,15 +87,15 @@ keybindOptions: [
]
```

Return an array made of objects in format
Return an array made of objects in format

`{value: "copyAccelerator", accelerator: "Ctrl+Shift+Insert"}`
`{value: "copyAccelerator", accelerator: "Ctrl+Shift+Insert"}`

where `accelerator` is the input for the `value` you registered

<details>
<summary> Code Example </summary>

```javascript
const kb = ($value, $label, $default) => { return { value: $value, label: $label, default: $default } };
prompt({
Expand All @@ -119,7 +119,7 @@ prompt({
.catch(console.error)
```
</details>

<details>
<summary> Screenshots </summary>

Expand Down Expand Up @@ -150,7 +150,7 @@ counterOptions: {

<details>
<summary> Code Example </summary>

```javascript
prompt({
title: "Counter",
Expand All @@ -165,7 +165,7 @@ prompt({
}, win).then(input => console.log(`input == ${input}`)).catch(console.error)
```
</details>

<details>
<summary> Screenshots </summary>

Expand Down Expand Up @@ -193,7 +193,7 @@ Must specify selectOptions with valid entries in **one** of the following format

<details>
<summary> Code Example </summary>

```javascript
prompt({
title: "Select",
Expand All @@ -209,7 +209,7 @@ prompt({
}, win).then(input => console.log(`input == ${input}`)).catch(console.error)
```
</details>

<details>
<summary> Screenshots </summary>

Expand All @@ -224,7 +224,7 @@ prompt({

### multiInput

Create a prompt with multiple inputs.
Create a prompt with multiple inputs. Select inputs can also be used.

Returns an array with with input in same order that was given to the options, for example:
multiInputOptions: [{usernameOptions}, {passwordOptions}] could return ["Jack", "61523"]
Expand All @@ -237,43 +237,54 @@ Must specify multiInputOptions with valid entries in the following format:

<details>
<summary> Code Example </summary>

```javascript
prompt({
title: "credentials",
label: "Please enter username and password",
type: "multiInput",
multiInputOptions:
[{
inputAttrs:
{
type: "email",
required: true,
placeholder: "email"
}
},
{
inputAttrs:
{
type: "password",
placeholder: "password"
}
}],
resizable: true,
height: 150,
width: 300,
title: "credentials",
label: "Login Info:",
type: "multiInput",
multiInputOptions:

lysdexic-audio marked this conversation as resolved.
Show resolved Hide resolved
{
inputAttrs:
{
type: "email",
required: true,
placeholder: "email"
}
},
{
inputAttrs:
{
type: "password",
placeholder: "password"
}
},
{
selectOptions: { na: "North America", eu: "Europe", other: "Other" },
value: "2"
}
],
resizable: true,
width: 300,
height: 225,
}, win).then(input => console.log(`input == ${input}`)).catch(console.error)
```
</details>

<details>
<summary> Screenshots </summary>

![](https://github.com/amunim/custom-electron-prompt/blob/main/screenshots/multiInput/button.PNG)
With `selectOptions`:

![](screenshots/multiInput/multiInputSelect.PNG)


Without `selectOptions`:
*This screenshot also contains a custom button.*

![](screenshots/multiInput/button.PNG)

This screenshot also contains a custom button.

</details>

----
Expand Down Expand Up @@ -340,8 +351,8 @@ Adds an extra/custom button with special functionalities other than success or e

<details>
<summary> Code Example </summary>


```javascript
await prompt({
title: 'Login credentials',
Expand Down Expand Up @@ -373,7 +384,7 @@ await prompt({
button:
{
label: "Autofill",
click: () =>
click: () =>
{
document.querySelectorAll("#data")[0].value = "mama@young.com";
document.querySelectorAll("#data")[1].value = "mysecretrecipe";
Expand All @@ -390,8 +401,8 @@ await prompt({

<details>
<summary> Screenshots </summary>


![](https://github.com/amunim/custom-electron-prompt/blob/main/screenshots/multiInput/button.PNG)
</details>

Expand Down
43 changes: 25 additions & 18 deletions lib/page/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function promptRegister() {
dataElement = promptCreateInput(promptOptions);
break;
case "select":
dataElement = promptCreateSelect();
dataElement = promptCreateSelect(promptOptions);
break;
case "keybind":
dataElement = require("./keybind")(promptOptions.keybindOptions, dataContainerElement);
Expand All @@ -80,18 +80,20 @@ function promptRegister() {
return promptError(`Unhandled input type '${promptOptions.type}'`);
}

if (promptOptions.type != "keybind" && promptOptions.type != "multiInput") {
dataElement.setAttribute("id", "data");
if (promptOptions.type !== "multiInput") {
if (promptOptions.type !== "keybind") {
dataElement.setAttribute("id", "data");

if (promptOptions.type !== "counter") {
dataContainerElement.append(dataElement);
}
}
if (promptOptions.type !== "counter") {
dataContainerElement.append(dataElement);
}

dataElement.focus();
if (promptOptions.type !== "select") {
dataElement.select();
}
}

if (promptOptions.type !== "select" && promptOptions.type !== "keybind") {
dataElement.select();
dataElement.focus?.();
}

//load custom script from options
Expand Down Expand Up @@ -213,29 +215,34 @@ function promptCreateInput(options) {
function promptCreateMultiInput(parentElement) {
let el;
for (const option of promptOptions.multiInputOptions) {
el = promptCreateInput(option);
el.setAttribute("id", "data");

if (option.inputAttrs) {
el = promptCreateInput(option);
el.setAttribute("id", "data");
}
if (option.selectOptions) {
el = promptCreateSelect(option);
el.setAttribute("id", "data");
}
parentElement.append(el);
}

return el;
}

//create multiple select
function promptCreateSelect() {
function promptCreateSelect(options) {
const dataElement = document.createElement("select");
let optionElement;

for (const k in promptOptions.selectOptions) {
if (!Object.prototype.hasOwnProperty.call(promptOptions.selectOptions, k)) {
for (const k in options.selectOptions) {
if (!Object.prototype.hasOwnProperty.call(options.selectOptions, k)) {
continue;
}

optionElement = document.createElement("option");
optionElement.setAttribute("value", k);
optionElement.textContent = promptOptions.selectOptions[k];
if (k === promptOptions.value) {
optionElement.textContent = options.selectOptions[k];
if (k === options.value) {
optionElement.setAttribute("selected", "selected");
}

Expand Down
Binary file added screenshots/multiInput/multiInputSelect.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.