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

fix(picker-column): dynamically change options #27359

Merged
merged 17 commits into from May 11, 2023
Merged

fix(picker-column): dynamically change options #27359

merged 17 commits into from May 11, 2023

Conversation

thetaPC
Copy link
Contributor

@thetaPC thetaPC commented May 3, 2023

Issue number: resolves #21763


What is the current behavior?

Picker options do not render correctly when dynamically changed. It throws an error when the original options' length is no longer the same as the updated options' length. This is due to refresh() being called with this.optsEl having a stale list of children. The list doesn't get updated until the upcoming render.

What is the new behavior?

The column will call refresh() when it detects the columns has changed. The call needs to be done after this.optsEl gets updated with the correct number of children.

componentShouldUpdate() will check if options has changed -> re-renders with the updated options -> componentDidUpdate() calls refresh() based on componentShouldUpdate()

The standalone test page has been updated to include a way to test this fix.

Does this introduce a breaking change?

  • Yes
  • No

Other information

It might be beneficial to review the component lifecycle.

Co-authored-by: liamdebeasi liamdebeasi@users.noreply.github.com

@stackblitz
Copy link

stackblitz bot commented May 3, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@github-actions github-actions bot added the package: core @ionic/core package label May 3, 2023
@thetaPC thetaPC marked this pull request as ready for review May 3, 2023 19:56
@thetaPC thetaPC requested a review from a team as a code owner May 3, 2023 19:56
@amandaejohnston amandaejohnston self-requested a review May 4, 2023 15:25
Copy link
Contributor

@amandaejohnston amandaejohnston left a comment

Choose a reason for hiding this comment

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

LGTM aside from one last tweak 👍 Thank you!

@thetaPC thetaPC requested a review from liamdebeasi May 10, 2023 18:17

this.refresh();
componentDidUpdate() {
// Options may have changed since last update.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Options may have changed since last update.
// Options have changed since last update.

If colDidChange is true then the columns definitely changed since the last render

Copy link
Member

@liamdebeasi liamdebeasi left a comment

Choose a reason for hiding this comment

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

A few small comment/style things, but otherwise this is good to go. Great job on this!

// Options may have changed since last update.
if (this.colDidChange) {
// Animation must be disabled through the `onDomChange` parameter.
// Otherwise, the recently added options will will render
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Otherwise, the recently added options will will render
// Otherwise, the recently added options will render

@@ -74,21 +80,32 @@ export class PickerColumnCmp implements ComponentInterface {
onEnd: (ev) => this.onEnd(ev),
});
this.gesture.enable();
// Options have not been initialized yet
// Animation must be disabled through the `noAnimate` flag
// Otherwise, the options will will render
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Otherwise, the options will will render
// Otherwise, the options will render

@@ -33,6 +33,12 @@ export class PickerColumnCmp implements ComponentInterface {
private rafId?: ReturnType<typeof requestAnimationFrame>;
private tmrId?: ReturnType<typeof setTimeout>;
private noAnimate = true;
// `colDidChange` is a flag that gets set when the column is changed
// dynamically. When this flag is set, the column will refresh
// to incorporate the new column data.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// to incorporate the new column data.
// after the component re-renders to incorporate the new column data.
// This is necessary because `this.refresh` queries for the option elements,
// so it needs to wait for the latest elements to be available in the DOM.

@@ -132,6 +149,7 @@ export class PickerColumnCmp implements ComponentInterface {
const scaleStr = `scale(${this.scaleFactor})`;

const children = this.optsEl.children;

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

@@ -185,6 +203,7 @@ export class PickerColumnCmp implements ComponentInterface {
button.classList.remove(PICKER_OPT_SELECTED);
}
}

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change

@thetaPC thetaPC added this pull request to the merge queue May 11, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 11, 2023
@thetaPC thetaPC added this pull request to the merge queue May 11, 2023
Merged via the queue into main with commit 7c7fb2b May 11, 2023
45 checks passed
@thetaPC thetaPC deleted the FW-4127 branch May 11, 2023 15:39
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.

bug: Error occurs when dynamically changing picker column options
4 participants