Skip to content

fix(select): value is selected when given array - #24687

Merged
sean-perkins merged 2 commits into
ionic-team:mainfrom
adamduren:patch-2
Feb 9, 2022
Merged

sean-perkins merged 2 commits into
ionic-team:mainfrom
adamduren:patch-2

Conversation

@adamduren

@adamduren adamduren commented Feb 1, 2022 •

Copy link
Copy Markdown
Contributor

This commit fixes an issue that caused selected options to not be selected when using the multiple option.

Pull request checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Build (npm run build) was run locally and any changes were pushed
  • Lint (npm run lint) has passed locally and any fixes were made for failures

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

Matching options are not selected by default if an array value is passed with multiple set to true.

Issue Number: #24742

What is the new behavior?

Matching options are selected by default if an array value is passed with multiple set to true.

Does this introduce a breaking change?

  • Yes
  • No

Other information

The first two argument names and their argument types for isOptionSelected(currentValue: any | any[], compareValue: any, compareWith) was the main indicator aside from the apparent bug that the parameters were out of order.

This commit fixes an issue that caused selected options to not be selected when using the `multiple` option.
@adamduren
adamduren requested a review from a team February 1, 2022 21:46
@github-actions github-actions Bot added the package: core @ionic/core package label Feb 1, 2022
@sean-perkins

Copy link
Copy Markdown
Contributor

@adamduren thanks for the PR!

With all PRs, we require an issue and reproduction app. Could you please create an issue with a reproduction app that demonstrates the issue?

We require this for a few reasons:

  1. Attached issues are referenced in our CHANGELOG, so that developers understand what each release contains.
  2. Reproduction app allows us to verify your changes solve the issue

Thanks!

@adamduren

Copy link
Copy Markdown
Contributor Author

@sean-perkins here is a reproduction. Just need to npm i; npm start. Also attached a video of the reproduction and code side-by-side

ionic-select-bug.mov

@adamduren

Copy link
Copy Markdown
Contributor Author

@sean-perkins any updates on this one? Happy to help in any way I can.

@sean-perkins

Copy link
Copy Markdown
Contributor

@adamduren thanks for the reproduction. I can verify that this is an issue with the compareWith with behavior.

This issue is not directly resolving #24584, is it? If not, we can quickly split out a new issue to attach to this PR to help with future issues that may arise (having the ability to search for the issue when reporting new ones).

I'd like to write a test around this behavior. Let's run the Github actions and make sure all existing behavior passes and then I can follow-up with what a test may look like to cover this change (may be as simple as a new test with compareWith and capturing a screenshot when it's opened).

Appreciate the work here.

@adamduren

Copy link
Copy Markdown
Contributor Author

@sean-perkins Thanks for the follow-up.

This PR does not resolve #24584. I more digging into that one and commented on what I observed in that issue separately. Unfortunately it is beyond the scope of my expertise but believe it is related to React seeing IonSelect as a custom component and manually calling setAttribute on value with the stringified object.

@sean-perkins

Copy link
Copy Markdown
Contributor

@adamduren awesome, thanks for the follow-up. We'll create a new issue to track this specific issue then.

Can you please add the following test to your PR?

In core/src/components/select/test create a new folder called compare-with.

Inside that directory, create two files, index.html and e2e.ts.

The contents of the HTML file:

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="UTF-8">
  <title>Select - compareWith</title>
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <link href="../../../../../css/ionic.bundle.css" rel="stylesheet">
  <link href="../../../../../scripts/testing/styles.css" rel="stylesheet">
  <script src="../../../../../scripts/testing/scripts.js"></script>
  <script nomodule src="../../../../../dist/ionic/ionic.js"></script>
  <script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
</head>

<body>
  <ion-app>

    <ion-header>
      <ion-toolbar>
        <ion-title>Select - compareWith</ion-title>
      </ion-toolbar>
    </ion-header>

    <ion-content>
      <ion-list>
        <ion-item>
          <ion-label>Select multiple using objects and compareWith</ion-label>
          <ion-select id="multiple"></ion-select>
        </ion-item>
        <ion-item>
          <ion-label>Select using objects and compareWith</ion-label>
          <ion-select id="single" multiple="true"></ion-select>
        </ion-item>
      </ion-list>

    </ion-content>

    <script>
      function compareWithFn(a, b) {
        return a.value === b.value;
      }

      let objectOptions = [{
        label: 'selected by default', value: '1'
      }, {
        label: 'not selected by default', value: '2'
      }];

      document.querySelectorAll('ion-select').forEach(select => {
        select.compareWith = compareWithFn;

        objectOptions.forEach((option) => {
          let selectOption = document.createElement('ion-select-option');
          selectOption.value = option;
          selectOption.textContent = option.label;

          select.appendChild(selectOption)
        });

      });

      let objectSelectMultipleElement = document.getElementById('multiple');
      objectSelectMultipleElement.value = [objectOptions[0]];

      let objectSelectElement = document.getElementById('single');
      objectSelectElement.value = objectOptions[0];
    </script>
  </ion-app>
</body>

</html>

The contents of the e2e.ts:

import { newE2EPage } from '@stencil/core/testing';

test('select: compareWith', async () => {
  const page = await newE2EPage({
    url: '/src/components/select/test/compare-with?ionic:_testing=true'
  });

  const compares = [];
  compares.push(await page.compareScreenshot());

  const selectMultiple = await page.find('#multiple');
  await selectMultiple.click();

  let alert = await page.find('ion-alert');
  await alert.waitForVisible();
  await page.waitForTimeout(250);

  compares.push(await page.compareScreenshot('should open select[multiple] with option selected'));

  await alert.callMethod('dismiss');

  const selectSingle = await page.find('#single');
  await selectSingle.click();

  alert = await page.find('ion-alert');
  await alert.waitForVisible();
  await page.waitForTimeout(250);

  compares.push(await page.compareScreenshot('should open select with option selected'));

  await alert.callMethod('dismiss');

  for (const compare of compares) {
    expect(compare).toMatchScreenshot();
  }

});

@sean-perkins sean-perkins left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good comparing against the reproduction and locally with running the test file and verifying before/after behavior.

I'll add the team for review.

@sean-perkins
sean-perkins requested a review from a team February 8, 2022 18:22
@sean-perkins
sean-perkins merged commit 6ee7d15 into ionic-team:main Feb 9, 2022
@sean-perkins

Copy link
Copy Markdown
Contributor

Merged in, will be available in the 6.0.6 release. Thank you for the contribution!

@adamduren
adamduren deleted the patch-2 branch February 9, 2022 18:13
@adamduren

Copy link
Copy Markdown
Contributor Author

Thanks team and @sean-perkins!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: core @ionic/core package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants