ui5-webcomponents icon indicating copy to clipboard operation
ui5-webcomponents copied to clipboard

Two way data binding for ui5-select is not fully supported

Open esporysz opened this issue 5 years ago • 4 comments

Bug Description

I’m writing regarding PR https://github.com/SAP/ui5-webcomponents/pull/1583. I try to use ui5-select with Angular reactive forms and 2-way data binding using “@codebakery/origami”: “^3.2.0".

  1. Binding from view to model updates model with option HTMLElement and not its actual value. I suppose that in Select.js _afterClose method should fire event in that way this._fireChangeEvent(this.options[this._selectedIndex].value);
  2. Binding from model to view seems to not work (preloading data value). Ui5-select selects always first option from array and it doesn’t take into consideration actual value of model.

Example

app.component.ts

import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
  form: FormGroup;
  accounts = [
    {
      id: '1',
      name: 'Account 1',
    },
    {
      id: '2',
      name: 'Account 2',
    },
    {
      id: '3',
      name: 'Account 3',
    },
    {
      id: '4',
      name: 'Account 4',
    },
  ];

  constructor(private fb: FormBuilder) {}

  ngOnInit(): void {
    this.form = this.fb.group({
      name: ['test', Validators.required],
      accountId: ['3', Validators.required],
    });
  }

  save(): void {
    console.log(this.form.value);
  }
}

app.component.html

  <div>
    <ui5-label required>Name</ui5-label>
    <ui5-input [formControl]="form.get('name')" origami></ui5-input>
  </div>

  <div>
    <ui5-label required>Account</ui5-label>
    <ui5-select [formControl]="form.get('accountId')" origami name="accountId">
      <ui5-option *ngFor="let account of accounts" [value]="account.id">{{
        account.name
      }}</ui5-option>
    </ui5-select>
  </div>
  <ui5-button design="Emphasized" (click)="save()">Save</ui5-button>
</div>

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import {
  APP_INITIALIZER,
  CUSTOM_ELEMENTS_SCHEMA,
  NgModule,
} from '@angular/core';
import { OrigamiFormsModule } from '@codebakery/origami/forms';
import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import Input from '@ui5/webcomponents/dist/Input';
import '@ui5/webcomponents/dist/Button';
import '@ui5/webcomponents/dist/Label';
import Select from '@ui5/webcomponents/dist/Select';
import '@ui5/webcomponents/dist/Option';
import '@ui5/webcomponents/dist/features/InputElementsFormSupport.js';

function onAppInit(): () => Promise<any> {
  return (): Promise<any> => {
    return Input.define() && Select.define();
  };
}

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    FormsModule,
    ReactiveFormsModule,
    OrigamiFormsModule,
  ],
  providers: [
    {
      provide: APP_INITIALIZER,
      useFactory: onAppInit,
      multi: true,
    },
  ],
  schemas: [CUSTOM_ELEMENTS_SCHEMA],
  bootstrap: [AppComponent],
})
export class AppModule {}

Priority

  • [ ] Low
  • [x] Medium
  • [ ] High
  • [ ] Very High

esporysz avatar May 26 '21 08:05 esporysz

Hi @esporysz

Can you recreate the explained scenario in CodeSandbox?

fifoosid avatar May 27 '21 09:05 fifoosid

Hello, I recreated it here https://stackblitz.com/edit/angular-tnhncu

esporysz avatar May 27 '21 09:05 esporysz

The addressed PR should fix both issues that are explained in this issue ☺️

Note: In order to make point 2 work, you should set the default value to "Account 3" rather than the id("3")

fifoosid avatar May 28 '21 06:05 fifoosid

Thank you for your answer 👍

esporysz avatar May 28 '21 06:05 esporysz