Skip to content

Comments

Fixed syncSelectValue() for data-display#100

Merged
Tsjippy merged 3 commits intobluzky:masterfrom
LuigiPulcini:fix/99
Aug 27, 2025
Merged

Fixed syncSelectValue() for data-display#100
Tsjippy merged 3 commits intobluzky:masterfrom
LuigiPulcini:fix/99

Conversation

@LuigiPulcini
Copy link
Contributor

@LuigiPulcini LuigiPulcini commented Aug 20, 2025

The changes in this PR address issue #99 by allowing syncSelectValue() to take data-display into account when searching for an option matching each item in the dropdown list.

This solution is fully tested in a local environment and confirmed to work regardless of data-display being used or not.

The name matchingOption was used for the variable in the outer scope, so that there is no confusion with the option variable used inside the find callback functions. While the previous naming doesn't create any functional problem, the new one makes the code easier to read.

Summary by Sourcery

Fix syncSelectValue to account for the data-display attribute when matching dropdown options and improve variable naming for clarity.

Bug Fixes:

  • Allow syncSelectValue to match options based on the data-display attribute (or text content) in a case-insensitive manner and fallback to matching by value if no display match is found

Enhancements:

  • Rename the inner variable from option to matchingOption to avoid confusion and improve code readability

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Aug 20, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates syncSelectValue to perform case-insensitive matching of dropdown options based on the data-display attribute (falling back to textContent) with an additional fallback to value, and renames the loop variable for clarity.

Class diagram for updated syncSelectValue logic in NiceSelect

classDiagram
    class NiceSelect {
        options: Array
        syncSelectValue()
    }
    class Option {
        dataset.display
        textContent
        value
        selected
    }
    NiceSelect --> Option : uses in syncSelectValue
Loading

Flow diagram for option matching in syncSelectValue

flowchart TD
    A[Start: For each item in options] --> B{Find option by data-display or textContent}
    B -- Found --> D[Set selected state]
    B -- Not Found --> C{Find option by value}
    C -- Found --> D[Set selected state]
    C -- Not Found --> E[Continue to next item]
    D --> F[Next item]
    E --> F[Next item]
    F --> G[End]
Loading

File-Level Changes

Change Details Files
Enhanced option matching logic to consider data-display and fallback to value
  • Use option.dataset.display or option.textContent for initial matching, trimmed and lowercased
  • Added secondary find by option.value matching item.data.value
  • Return early if no matching option is found
src/js/nice-select2.js
Renamed loop variable for improved readability
  • Changed outer variable name from 'option' to 'matchingOption'
  • Updated selected true/false assignments to reference matchingOption
src/js/nice-select2.js

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/js/nice-select2.js:442` </location>
<code_context>

     this.options.forEach(item =>{
-      let option   = Array.from(select.options).find(option => option.textContent === item.data.text);
+      let matchingOption = Array.from(select.options).find(option => {
+        return (option.dataset.display || option.textContent) === item.data.text;
+      });
+
</code_context>

<issue_to_address>
Consider normalizing text comparison for robustness.

Direct string comparison may fail if there are differences in whitespace or case. Normalizing both values before comparison will improve reliability, especially when handling user input or external data.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@LuigiPulcini
Copy link
Contributor Author

@sourcery-ai review

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `src/js/nice-select2.js:456` </location>
<code_context>

-      if(option == undefined){
-        option   = Array.from(select.options).find(option => option.textContent === item.data.value);
+      if (matchingOption == undefined) {
+        // no matching option was found, continue
+        return;
       }
</code_context>

<issue_to_address>
Silently skipping unmatched options may hide data issues.

Consider adding a log or warning when no matching option is found to help identify data mismatches during debugging.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
      if (matchingOption == undefined) {
        // no matching option was found, continue
        return;
=======
      if (matchingOption == undefined) {
        console.warn(`No matching option found for value: "${item.data.value}" in select element`, select);
        return;
>>>>>>> REPLACE

</suggested_fix>

### Comment 2
<location> `src/js/nice-select2.js:462` </location>
<code_context>

       if(item.attributes.selected){
-        option.selected = true;
+        matchingOption.selected = true;
       } else {
-        option.selected = false;
+        matchingOption.selected = false;
       }
     });
</code_context>

<issue_to_address>
Directly setting selected property may not trigger change events.

If your application relies on change events, dispatch one manually after updating the selection.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
      if(item.attributes.selected){
        matchingOption.selected = true;
      } else {
        matchingOption.selected = false;
      }
=======
      if(item.attributes.selected){
        matchingOption.selected = true;
      } else {
        matchingOption.selected = false;
      }
      // Manually dispatch change event after updating selection
      const event = new Event('change', { bubbles: true });
      select.dispatchEvent(event);
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@Tsjippy Tsjippy merged commit 413941b into bluzky:master Aug 27, 2025
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.

2 participants