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(segment): only emit ionChange when user releases pointer from screen #20495

Merged
merged 5 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5209,7 +5209,7 @@ declare namespace LocalJSX {
*/
'mode'?: "ios" | "md";
/**
* Emitted when the value property has changed.
* Emitted when the value property has changed and any dragging pointer has been released from `ion-segment`.
*/
'onIonChange'?: (event: CustomEvent<SegmentChangeEventDetail>) => void;
/**
Expand Down
4 changes: 2 additions & 2 deletions core/src/components/segment-button/segment-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ export class SegmentButton implements ComponentInterface, ButtonInterface {
const segmentEl = this.segmentEl = this.el.closest('ion-segment');
if (segmentEl) {
this.updateState();
segmentEl.addEventListener('ionChange', this.updateState);
segmentEl.addEventListener('ionSelect', this.updateState);
}
}

disconnectedCallback() {
const segmentEl = this.segmentEl;
if (segmentEl) {
segmentEl.removeEventListener('ionChange', this.updateState);
segmentEl.removeEventListener('ionSelect', this.updateState);
this.segmentEl = null;
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/components/segment/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -449,9 +449,9 @@ export const SegmentExample: React.FC = () => (

## Events

| Event | Description | Type |
| ----------- | -------------------------------------------- | --------------------------------------- |
| `ionChange` | Emitted when the value property has changed. | `CustomEvent<SegmentChangeEventDetail>` |
| Event | Description | Type |
| ----------- | ---------------------------------------------------------------------------------------------------------- | --------------------------------------- |
| `ionChange` | Emitted when the value property has changed and any dragging pointer has been released from `ion-segment`. | `CustomEvent<SegmentChangeEventDetail>` |


## CSS Custom Properties
Expand Down
28 changes: 26 additions & 2 deletions core/src/components/segment/segment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export class Segment implements ComponentInterface {
private gesture?: Gesture;
private didInit = false;
private checked?: HTMLIonSegmentButtonElement;
private pointerDown = false;
liamdebeasi marked this conversation as resolved.
Show resolved Hide resolved

// Value to be emitted when gesture ends
private valueAfterGesture?: any;

@Element() el!: HTMLIonSegmentElement;

Expand Down Expand Up @@ -53,15 +57,27 @@ export class Segment implements ComponentInterface {
@Watch('value')
protected valueChanged(value: string | undefined) {
if (this.didInit) {
this.ionChange.emit({ value });
this.ionSelect.emit({ value });
if (!this.pointerDown) {
liamdebeasi marked this conversation as resolved.
Show resolved Hide resolved
this.ionChange.emit({ value });
} else {
this.valueAfterGesture = value;
}
}
}

/**
* Emitted when the value property has changed.
* Emitted when the value property has changed and any
* dragging pointer has been released from `ion-segment`.
*/
@Event() ionChange!: EventEmitter<SegmentChangeEventDetail>;

/**
* Emitted when user has dragged over a new button
* @internal
*/
@Event() ionSelect!: EventEmitter<SegmentChangeEventDetail>;

/**
* Emitted when the styles change.
* @internal
Expand Down Expand Up @@ -115,6 +131,7 @@ export class Segment implements ComponentInterface {
}

onStart(detail: GestureDetail) {
this.pointerDown = true;
this.activate(detail);
}

Expand All @@ -123,6 +140,7 @@ export class Segment implements ComponentInterface {
}

onEnd(detail: GestureDetail) {
this.pointerDown = false;
this.setActivated(false);

const checkedValidButton = this.setNextIndex(detail, true);
Expand All @@ -133,6 +151,12 @@ export class Segment implements ComponentInterface {
if (checkedValidButton) {
this.addRipple(detail);
}

const value = this.valueAfterGesture;
if (value !== undefined) {
this.ionChange.emit({ value });
this.valueAfterGesture = undefined;
}
}

private getButtons() {
Expand Down