Skip to content

feat: allow entering non-default values in multi-select #15935

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

Merged
merged 4 commits into from
Dec 27, 2024

Conversation

joobisb
Copy link
Contributor

@joobisb joobisb commented Dec 19, 2024

Closes #15488

cliui.Multiselect currently does not allow users to input custom options if zero choices are provided.

Proposed Solution

cliui.MultiSelect should always allow users to specify another option.

For example, when running coder exp prompt-example multi-select:

PS: I have modified the text a bit from the original issue

? Select some things:  [Use arrows to move, space to select, <right> to all, <left> to none, type to filter]
> [x] Code
  [ ] Chair
  [ ] Whale
  [ ] Diamond
  [ ] Carrot
  ... Other
+ Add custom value


? Select some things:  [Use arrows to move, space to select, <right> to all, <left> to none, type to filter]
? Enter custom value: potato⏎


? Select some things:  [Use arrows to move, space to select, <right> to all, <left> to none, type to filter]
  [x] Code
  [ ] Chair
  [ ] Whale
  [ ] Diamond
  [ ] Carrot
  [x] potato
>  ... Other
+ Add custom value

Attaching a recording for the same

Screen.Recording.2024-12-19.at.4.25.12.PM.mov

@cdr-bot cdr-bot bot added the community Pull Requests and issues created by the community. label Dec 19, 2024
@@ -101,6 +101,39 @@ func TestMultiSelect(t *testing.T) {
}()
require.Equal(t, items, <-msgChan)
})

t.Run("MultiSelectWithCustomInput", func(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couldn't add tests to check the custom input flow, we had this check here and not sure if its safe to remove and add interactive tests, please do let me know if there's a better way to incorporate those tests

if flag.Lookup("test.v") != nil {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think historically we've manually tested this using prompt-example. I agree that it's not ideal :-(

@joobisb
Copy link
Contributor Author

joobisb commented Dec 19, 2024

Hi @johnstcn ,

could you please have a look at this PR?

I do have a question regarding duplicates, if users enters the same custom value twice should we treat it as a single option ? right now it will create 2 duplicate options, what do you think will be the better way to handle that scenario?

@johnstcn
Copy link
Member

I do have a question regarding duplicates, if users enters the same custom value twice should we treat it as a single option ? right now it will create 2 duplicate options, what do you think will be the better way to handle that scenario?

I think it would make more sense to either de-duplicate or validate that the custom option isn't already present.

message string
canceled bool
selected bool
isInputMode bool // New field to track if we're adding a custom option
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: isCustomInputMode for consistency

Comment on lines -420 to +435
m.cursor = len(options) - 1
m.cursor = maxIndex
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this logic needs to handle when EnableCustomInput is false. Currently it lets you go over the last valid option.

cli/prompts.go Outdated
},
Defaults: []string{"Code"},
Defaults: []string{"Code"},
EnableCustomInput: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we expose this as a flag so we can easily test both with and without custom input?

@joobisb
Copy link
Contributor Author

joobisb commented Dec 20, 2024

I do have a question regarding duplicates, if users enters the same custom value twice should we treat it as a single option ? right now it will create 2 duplicate options, what do you think will be the better way to handle that scenario?

I think it would make more sense to either de-duplicate or validate that the custom option isn't already present.

@johnstcn @DanielleMaywood

I've addressed the comments and handled duplicates as shown in the recording

  • case insensitive comparison - if it exists, it selects and cursor points to the already existing option
  • if not add a new option
Screen.Recording.2024-12-20.at.11.39.55.AM.mov


// Check for duplicates
for i, opt := range m.options {
if strings.EqualFold(opt.option, m.customInput) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure about the case-insensitive matching. On the surface, it seems innocuous enough but I'm not sure if there's going to be a case where someone is going to want to differentiate A from a. Let's keep matching case-sensitive for now.
We can add an option for case-insensitive matching later if someone requests it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the PR!

@matifali matifali merged commit 638247c into coder:main Dec 27, 2024
29 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Dec 27, 2024
@joobisb joobisb deleted the issue#15488 branch December 27, 2024 20:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
community Pull Requests and issues created by the community.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cliui: Allow entering non-default values in MultiSelect
4 participants