Skip to content

Commit

Permalink
fix(picker-column): prevSelected is set to the correct value (#27458)
Browse files Browse the repository at this point in the history
Issue number: resolves #21764 

---------

<!-- Please do not submit updates to dependencies unless it fixes an
issue. -->

<!-- Please try to limit your pull request to one type (bugfix, feature,
etc). Submit multiple pull requests if needed. -->

## What is the current behavior?
<!-- Please describe the current behavior that you are modifying. -->

The `prevSelected` returns the same value as `selectedIndex`.

When a user has selected the first option then the second:
- `prevSelected` returns `1`
- `selectedIndex` returns `1`

## What is the new behavior?
<!-- Please describe the behavior or changes that are being added by
this PR. -->

The `prevSelected` returns the index of the last selected option.

When a user has selected the first option then the second:
- `prevSelected` returns `0`
- `selectedIndex` returns `1`

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

<!-- If this introduces a breaking change, please describe the impact
and migration path for existing applications below. -->


## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->

Read the comment below regarding `toHaveReceivedEventDetail`.
  • Loading branch information
thetaPC committed May 16, 2023
1 parent 13d2d11 commit 9dc126d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion core/src/components/picker-column/picker-column.tsx
Expand Up @@ -146,6 +146,7 @@ export class PickerColumnCmp implements ComponentInterface {
let translateY = 0;
let translateZ = 0;
const { col, rotateFactor } = this;
const prevSelected = col.selectedIndex;
const selectedIndex = (col.selectedIndex = this.indexForY(-y));
const durationStr = duration === 0 ? '' : duration + 'ms';
const scaleStr = `scale(${this.scaleFactor})`;
Expand Down Expand Up @@ -204,7 +205,7 @@ export class PickerColumnCmp implements ComponentInterface {
button.classList.remove(PICKER_OPT_SELECTED);
}
}
this.col.prevSelected = selectedIndex;
this.col.prevSelected = prevSelected;

if (saveY) {
this.y = y;
Expand Down
Expand Up @@ -3,9 +3,11 @@ import { configs, test } from '@utils/test/playwright';

configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) => {
test.describe(title('picker-column'), () => {
test('should present picker without ion-app', async ({ page }) => {
await page.goto('/src/components/picker-column/test/standalone', config);
test.beforeEach(async ({ page }) => {
await page.goto(`/src/components/picker-column/test/standalone`, config);
});

test('should present picker without ion-app', async ({ page }) => {
const ionPickerDidPresent = await page.spyOnEvent('ionPickerDidPresent');

const picker = page.locator('ion-picker');
Expand All @@ -16,5 +18,29 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>

await expect(picker).toBeVisible();
});

test('should have the correct selectedIndex and prevSelected', async ({ page }) => {
test.info().annotations.push({
type: 'issue',
description: 'https://github.com/ionic-team/ionic-framework/issues/21764',
});

const ionPickerDidPresent = await page.spyOnEvent('ionPickerDidPresent');
const ionPickerColChangeEvent = await page.spyOnEvent('ionPickerColChange');

const column = page.locator('ion-picker-column');
const secondOption = column.locator('.picker-opt').nth(1);

await page.click('#single-column-button');

await ionPickerDidPresent.next();

await secondOption.click();

await ionPickerColChangeEvent.next();

expect(ionPickerColChangeEvent.events[0].detail.prevSelected).toBe(0);
expect(ionPickerColChangeEvent.events[0].detail.selectedIndex).toBe(1);
});
});
});

0 comments on commit 9dc126d

Please sign in to comment.