Skip to content

Commit 52728a9

Browse files
committed
fix(segmented-button): refactored event handler and adjusted code by merge request comments
1 parent 70f030a commit 52728a9

File tree

3 files changed

+32
-33
lines changed

3 files changed

+32
-33
lines changed

packages/components/src/components/segment/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
| `position` | `position` | (optional) position within group | `number` | `undefined` |
2121
| `segmentId` | `segment-id` | (optional) segment's id | `string` | `'segment-' + i++` |
2222
| `selected` | `selected` | (optional) If `true`, the segment is selected | `boolean` | `false` |
23-
| `selectedIndex` | `selected-index` | (optional) the index of the currently selected segment in the segmented-button | `string` | `undefined` |
23+
| `selectedIndex` | `selected-index` | (optional) the index of the currently selected segment in the segmented-button | `number` | `undefined` |
2424
| `size` | `size` | (optional) The size of the segment | `"large" \| "medium" \| "small"` | `'small'` |
2525
| `styles` | `styles` | (optional) Injected CSS styles | `string` | `undefined` |
2626
| `textOnly` | `text-only` | (optional) position within group | `boolean` | `undefined` |

packages/components/src/components/segment/segment.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,13 @@ export class Segment {
6666
/** (optional) position within group */
6767
@Prop({ mutable: true }) iconOnly?: boolean;
6868
/** (optional) the index of the currently selected segment in the segmented-button */
69-
@Prop({ mutable: true }) selectedIndex?: string;
69+
@Prop({ mutable: true }) selectedIndex?: number;
7070

7171
/** Emitted when button is clicked */
7272
@Event({ eventName: 'scale-click' }) scaleClick!: EventEmitter<{
7373
id: string;
7474
selected: boolean;
7575
}>;
76-
/** Emitted when selection has been changed. */
77-
@Event({ eventName: 'scaleSelectionChanged' }) scaleSelectionChanged!: EventEmitter;
7876
/** @deprecated in v3 in favor of kebab-case event names */
7977
@Event({ eventName: 'scaleClick' }) scaleClickLegacy!: EventEmitter<{
8078
id: string;
@@ -85,7 +83,13 @@ export class Segment {
8583

8684
@Watch('selected')
8785
selectionChanged() {
88-
emitEvent(this, 'scaleSelectionChanged');
86+
if (this.selectedIndex !== -1 && !this.selected) {
87+
return;
88+
}
89+
emitEvent(this, 'scaleClick', {
90+
id: this.segmentId,
91+
selected: this.selected,
92+
});
8993
}
9094

9195
@Method()
@@ -158,15 +162,11 @@ export class Segment {
158162
}
159163

160164
handleClick = (event: MouseEvent) => {
161-
if (parseInt(this.selectedIndex, 10) + 1 === this.position) {
165+
if (this.selectedIndex === this.position) {
162166
return;
163167
}
164168
event.preventDefault();
165169
this.selected = !this.selected;
166-
emitEvent(this, 'scaleClick', {
167-
id: this.segmentId,
168-
selected: this.selected,
169-
});
170170
};
171171

172172
render() {

packages/components/src/components/segmented-button/segmented-button.tsx

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,35 +79,30 @@ export class SegmentedButton {
7979
showHelperText = false;
8080
@Listen('scaleClick')
8181
scaleClickHandler(ev: { detail: { id: string; selected: boolean } }) {
82-
let tempState: SegmentStatus[];
82+
if (this.status.length === 0) {
83+
return;
84+
}
85+
let tempState = this.getAllSegments().map((segment) => {
86+
return {
87+
id: segment.getAttribute('segment-id') || segment.segmentId,
88+
selected:
89+
segment.hasAttribute('selected') && segment.selected ? true : false,
90+
};
91+
});
8392
if (!this.multiSelect) {
8493
if (!ev.detail.selected) {
85-
tempState = this.status.map((obj) =>
94+
tempState = tempState.map((obj) =>
8695
ev.detail.id === obj.id ? ev.detail : { ...obj }
8796
);
88-
/* clicked button has now selected state */
8997
} else {
90-
tempState = this.status.map((obj) =>
98+
tempState = tempState.map((obj) =>
9199
ev.detail.id === obj.id ? ev.detail : { ...obj, selected: false }
92100
);
93101
}
94-
} else {
95-
tempState = this.status.map((obj) =>
96-
ev.detail.id === obj.id ? ev.detail : { ...obj }
97-
);
98102
}
99103
this.setState(tempState);
100104
}
101105

102-
@Listen('scaleSelectionChanged')
103-
selectionChangedHandler() {
104-
this.selectedIndex = this.getSelectedIndex();
105-
if (typeof(this.selectedIndex) !== 'number') { return }
106-
this.getAllSegments().forEach((segment) => {
107-
segment.setAttribute('selected-index', this.selectedIndex.toString());
108-
})
109-
}
110-
111106
@Watch('disabled')
112107
@Watch('size')
113108
@Watch('selectedIndex')
@@ -135,15 +130,16 @@ export class SegmentedButton {
135130
segments.forEach((segment, i) => {
136131
tempState.push({
137132
id: segment.getAttribute('segment-id') || segment.segmentId,
138-
selected: segment.hasAttribute('selected') || segment.selected,
133+
selected:
134+
segment.hasAttribute('selected') && segment.selected ? true : false,
139135
});
140-
segment.setAttribute('position', `${i + 1}`);
136+
segment.setAttribute('position', `${i}`);
141137
segment.setAttribute(
142138
'aria-description-translation',
143139
'$position $selected'
144140
);
145141
});
146-
this.setState(tempState);
142+
this.setState(tempState, true);
147143
this.selectedIndex = this.getSelectedIndex();
148144
this.showHelperText = this.shouldShowHelperText();
149145
}
@@ -185,7 +181,8 @@ export class SegmentedButton {
185181
const selectedIndex = allSegments.findIndex(
186182
(el: HTMLScaleSegmentElement) => el.selected === true
187183
);
188-
return selectedIndex;
184+
// we need to return -2 if no segment is selected
185+
return selectedIndex >= 0 ? selectedIndex : -2;
189186
}
190187
}
191188

@@ -235,7 +232,7 @@ export class SegmentedButton {
235232
return tempWidth;
236233
}
237234

238-
setState(tempState: SegmentStatus[]) {
235+
setState(tempState: SegmentStatus[], isInitial: boolean = false) {
239236
const segments = Array.from(
240237
this.hostElement.querySelectorAll('scale-segment')
241238
);
@@ -250,7 +247,9 @@ export class SegmentedButton {
250247
);
251248
});
252249
this.status = tempState;
253-
emitEvent(this, 'scaleChange', this.status);
250+
if (!isInitial) {
251+
emitEvent(this, 'scaleChange', { status: this.status, segments });
252+
}
254253
}
255254

256255
getAllSegments() {

0 commit comments

Comments
 (0)