SAP / fundamental-ngx

Fundamental Library for Angular is SAP Design System Angular component library
https://sap.github.io/fundamental-ngx
Apache License 2.0
264 stars 126 forks source link

Segmented button value conflict #10119

Open ZsDeak-SAP opened 1 year ago

ZsDeak-SAP commented 1 year ago

Is this a bug, enhancement, or feature request?

bug

Briefly describe your proposal.

Which versions of Angular and Fundamental Library for Angular are affected? (If this is a feature request, use current version.)

tried with Angular 15 and fundamental 40.3, but the code is the same on master right now

If this is a bug, please provide steps for reproducing it.

Use the component in toggle mode, starting with empty state. Assign the clicked button's value to the current value in a handler.

Expected: the value defined by the parent component through writeValue is presented by the component

Actual: the inner handler overrides the button's toggled state. what i saw in the code was a call first from writeValue that selected the button, but then the segmented-button component's own event handler deselected it. image

Please provide relevant source code if applicable.

<fd-segmented-button [toggle]="true" [ngModel]="currentValue">
  <button
    *ngFor="let value of values"
    fd-button
    [label]="value"
    [attr.value]="value"
    (click)="handleValueChange(value)"
  ></button>
</fd-segmented-button>

<br />
<small>value: {{ currentValue }}</small>
import { Component } from '@angular/core';

@Component({
  selector: 'fd-segmented-button-complex-example',
  templateUrl: './segmented-button-complex-example.component.html',
})
export class SegmentedButtonComplexExampleComponent {
  values: string[] = ['first', 'second', 'third'];
  currentValue = [];

  handleValueChange(value: string): void {
    this.currentValue = [value];
  }
}
khotcholava commented 1 day ago

@ZsDeak-SAP Hello, I did not understand well what is the problem, Can you please attack stackblitz example?

ZsDeak-SAP commented 1 day ago

@khotcholava it's the fd-segmented-button-complex-example from the fundamental examples, only with toggle mode and array value, but here's the same copy pasted to stackblitz😄 :

https://stackblitz.com/edit/tlhuhf?file=src%2Fapp%2Fsegmented-button-complex-example.component.ts

When you use toggle mode, value accepts an array. In this example I oversimplified handleValueChange, but it's still valid, it sets an array of values. As you can see the selected value is written under the buttons, but there's no highlight on the selected button.