Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Created September 19, 2023 22:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnlindquist/f2d312274812670eaf687e29eb531004 to your computer and use it in GitHub Desktop.
Save johnlindquist/f2d312274812670eaf687e29eb531004 to your computer and use it in GitHub Desktop.
// 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