Argument suggestions (#252)

* feature: Suggestions for inputs

* feature: #103 Completed suggestions support
This commit is contained in:
James Read 2024-03-23 21:52:53 +00:00 committed by GitHub
parent 781abaaf40
commit 1319f314ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 31 additions and 0 deletions

View File

@ -23,6 +23,7 @@ message ActionArgument {
repeated ActionArgumentChoice choices = 5; repeated ActionArgumentChoice choices = 5;
string description = 6; string description = 6;
map<string, string> suggestions = 7;
} }
message ActionArgumentChoice { message ActionArgumentChoice {

View File

@ -33,6 +33,7 @@ type ActionArgument struct {
Default string Default string
Choices []ActionArgumentChoice Choices []ActionArgumentChoice
Entity string Entity string
Suggestions map[string]string
} }
// ActionArgumentChoice represents a predefined choice for an argument. // ActionArgumentChoice represents a predefined choice for an argument.

View File

@ -94,6 +94,7 @@ func actionCfgToPb(action *config.Action, user *acl.AuthenticatedUser) *pb.Actio
Description: cfgArg.Description, Description: cfgArg.Description,
DefaultValue: cfgArg.Default, DefaultValue: cfgArg.Default,
Choices: buildChoices(cfgArg), Choices: buildChoices(cfgArg),
Suggestions: cfgArg.Suggestions,
} }
btn.Arguments = append(btn.Arguments, &pbArg) btn.Arguments = append(btn.Arguments, &pbArg)

View File

@ -64,6 +64,7 @@ class ArgumentForm extends window.HTMLElement {
domArgumentWrapper.classList.add('argument-wrapper') domArgumentWrapper.classList.add('argument-wrapper')
domArgumentWrapper.appendChild(this.createDomLabel(arg)) domArgumentWrapper.appendChild(this.createDomLabel(arg))
domArgumentWrapper.appendChild(this.createDomSuggestions(arg))
domArgumentWrapper.appendChild(this.createDomInput(arg)) domArgumentWrapper.appendChild(this.createDomInput(arg))
domArgumentWrapper.appendChild(this.createDomDescription(arg)) domArgumentWrapper.appendChild(this.createDomDescription(arg))
@ -79,6 +80,29 @@ class ArgumentForm extends window.HTMLElement {
return domLbl return domLbl
} }
createDomSuggestions (arg) {
if (typeof arg.suggestions !== 'object' || arg.suggestions.length === 0) {
return document.createElement('span')
}
const ret = document.createElement('datalist')
ret.setAttribute('id', arg.name + '-choices')
for (const suggestion of Object.keys(arg.suggestions)) {
const opt = document.createElement('option')
opt.setAttribute('value', suggestion)
if (typeof arg.suggestions[suggestion] !== 'undefined' && arg.suggestions[suggestion].length > 0) {
opt.innerText = arg.suggestions[suggestion]
}
ret.appendChild(opt)
}
return ret
}
createDomInput (arg) { createDomInput (arg) {
let domEl = null let domEl = null
@ -144,6 +168,10 @@ class ArgumentForm extends window.HTMLElement {
domEl.name = arg.name domEl.name = arg.name
domEl.value = arg.defaultValue domEl.value = arg.defaultValue
if (typeof arg.suggestions === 'object' && Object.keys(arg.suggestions).length > 0) {
domEl.setAttribute('list', arg.name + '-choices')
}
this.argInputs.push(domEl) this.argInputs.push(domEl)
return domEl return domEl