Created
September 19, 2023 22:25
-
-
Save johnlindquist/f2d312274812670eaf687e29eb531004 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name: Testing Regenerate List | |
import "@johnlindquist/kit" | |
let choices = ["one", "two", "three"].map(name => ({ | |
name, | |
value: name, | |
id: uuid(), // This happens automatically behind the scenes if you don't do it manually | |
})) | |
let value = "" | |
let defaultChoiceId = "" | |
let done = false | |
/* | |
The "defaultChoiceId" is used to keep the current choice focused after regenerating the list. | |
If this doesn't matter to you, you can avoid the `while` loop and use `setChoices()` inside of the shortcut instead | |
*/ | |
while (!done) { | |
done = true | |
if (defaultChoiceId) { | |
// Update choices | |
choices = choices.map(c => { | |
if (c.id === defaultChoiceId) { | |
let name = Math.random().toString().slice(2, 6) | |
return { | |
...c, | |
name, | |
value: name, | |
} | |
} | |
return c | |
}) | |
} | |
value = await arg( | |
{ | |
defaultChoiceId, | |
shortcuts: [ | |
{ | |
name: "Regenerate List", | |
key: `${cmd}+l`, | |
bar: "right", | |
onPress: (input, state) => { | |
done = false | |
defaultChoiceId = state.focused.id | |
submit("") | |
}, | |
}, | |
], | |
}, | |
choices | |
) | |
} | |
await div(value) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment