Skip to content

Bug: MultiSelect accessible mode allows selecting more options than the specified limit #114

@anirudhaCodes

Description

@anirudhaCodes

Describe the bug
In the runAccessible method of the MultiSelect field, there is a missing check for the limit when selecting options. This causes the accessible mode to allow selecting more options than the specified limit. This limit checkis handle in update method.

To Reproduce
Steps to reproduce the behavior:

  1. Go to burger example of 'huh\examples\burger'
  2. Run the form with WithAccessible(true)
  3. Try to select more option than specified limit in multiselect field.
  4. The accessible mode allows you to select more options than the specified limit.

Expected behavior
The accessible mode should enforce the specified limit on the number of options that can be selected, just like in the regular Run method (in update function).

Screenshots
In below image you can see that i can able to select more options than specified limit.
image

Desktop :

Device : Laptop
OS : WIN 11
go Version : go 1.21.6 windows/amd64

Proposed solution

Option 1:

func (m *MultiSelect[T]) runAccessible() error {
	m.printOptions()

	var choice int
	for {
		fmt.Printf("Select up to %d options. 0 to continue.\n", m.limit)

		choice = accessibility.PromptInt("Select: ", 0, len(m.options))
		if choice == 0 {
			m.finalize()
			err := m.validate(*m.value)
			if err != nil {
				fmt.Println(err)
				continue
			}
			break
		}
+		if !m.options[choice-1].selected && m.limit > 0 && m.numSelected() >= m.limit {
+			fmt.Printf("You can't select above %d options. 0 to continue.\n", m.limit)
+			continue
+		}
		m.options[choice-1].selected = !m.options[choice-1].selected
		if m.options[choice-1].selected {
			fmt.Printf("Selected: %s\n\n", m.options[choice-1].Key)
		} else {
			fmt.Printf("Deselected: %s\n\n", m.options[choice-1].Key)
		}

		m.printOptions()
	}

	var values []string

	for _, option := range m.options {
		if option.selected {
			*m.value = append(*m.value, option.Value)
			values = append(values, option.Key)
		}
	}

	fmt.Println(m.theme.Focused.SelectedOption.Render("Selected:", strings.Join(values, ", ")+"\n"))
	return nil
}

Option 2:
Alternatively, a separate checker function can be introduced to handle the all the limit check. This function can be called before toggling the selected state of an option in the runAccessible method and in Run method (update function).

Assign this to me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions