Skip to content

Commit fa74077

Browse files
committed
fix(segment): animate the highlight with value changes
1 parent 221bb42 commit fa74077

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

core/src/components/segment/segment.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,18 @@ export class Segment implements ComponentInterface {
7878
@Prop({ mutable: true }) value?: SegmentValue;
7979

8080
@Watch('value')
81-
protected valueChanged(value: SegmentValue | undefined) {
82-
/**
81+
protected valueChanged(value: SegmentValue | undefined, oldValue?: SegmentValue | undefined) {
82+
if (oldValue !== undefined && value !== undefined) {
83+
const buttons = this.getButtons();
84+
const previous = buttons.find(button => button.value === oldValue);
85+
const current = buttons.find(button => button.value === value);
86+
87+
if (previous && current) {
88+
this.checkButton(previous, current);
89+
}
90+
}
91+
92+
/**
8393
* `ionSelect` is emitted every time the value changes (internal or external changes).
8494
* Used by `ion-segment-button` to determine if the button should be checked.
8595
*/
@@ -208,7 +218,7 @@ export class Segment implements ComponentInterface {
208218
this.ionChange.emit({ value });
209219
}
210220

211-
private getButtons() {
221+
private getButtons(): HTMLIonSegmentButtonElement[] {
212222
return Array.from(this.el.querySelectorAll('ion-segment-button'));
213223
}
214224

0 commit comments

Comments
 (0)