Skip to content
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

enhancement(Autocomplete) allow default selected option(s) #577

Open
wants to merge 7 commits into
base: v2-dev
Choose a base branch
from

Conversation

gselderslaghs
Copy link
Member

@gselderslaghs gselderslaghs commented Jan 1, 2025

Proposed changes

This enhancement allows default selected for single or multiselect, as requested per #479
I have implemented an additional option selected that allows an array of alphanumeric ids, given the items ids
This implementation maps the selected ids in the selectedValues object array and handles selection of single or multiple items

Example single select:

M.Autocomplete.init(document.querySelector('.autocomplete'), {
    data: [
        {id: 1, text: "Item 1"},
        {id: 2, text: "Item 2"},
        {id: 3, text: "Item 3"}
    ],
    selected: [3]
});

Example multi select:

M.Autocomplete.init(document.querySelector('.autocomplete'), {
    isMultiSelect: true,
    data: [
        {id: 1, text: "Item 1"},
        {id: 2, text: "Item 2"},
        {id: 3, text: "Item 3"}
    ],
    selected: [1, 2]
});

Types of changes

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to change).

Checklist:

  • I have read the CONTRIBUTING document.
  • My commit messages follows the conventional commit format
  • My change requires a change to the documentation, and updated it accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@gselderslaghs
Copy link
Member Author

Will add additional spec tests later

@gselderslaghs gselderslaghs force-pushed the autocomplete-default-option branch from 1f620aa to aa9c828 Compare January 2, 2025 10:38
@gselderslaghs gselderslaghs changed the base branch from main to v2-dev January 2, 2025 10:38
…g of toggling checkbox since list is empty if minLength option is greater then 0 materializecss#479
@gselderslaghs
Copy link
Member Author

Additionally changed the selectOption method since selecting values was depending on rendered html in multiSelect, if the minLength option is greater then 0, no results are rendered in the html element, as a result, no options where able to mark as selected, I had to remove the conditional check on the list item and updated selected items using the selectedValues property followed by re-rendering the dropdown component
This change also allows calling the selectOption multiple times as items get pushed in the array without overwriting the full selectedValues array

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant