vaadin / hilla

Build better business applications, faster. No more juggling REST endpoints or deciphering GraphQL queries. Hilla seamlessly connects Spring Boot and React to accelerate application development.
https://hilla.dev
Apache License 2.0
922 stars 57 forks source link

@vaadin/form binder should support binding id-referenced entities to combo-box fields #251

Open vlukashov opened 3 years ago

vlukashov commented 3 years ago

When creating a form for an entity with an N-to-one relatated entity that is represented by its id, I want the field() directive to support this case so that creating a form is as simple as when the related entity is referenced by an object reference.

Example (of the hack that is needed with Vaadin 19):

@Data
public class SnippetProjectDto {
  @Nullable
  private UUID snippetId;

  @Nullable
  private UUID projectId;
}
<vaadin-combo-box
        label="Project"
        id="project"
        ...="${field(this.snippetProjectBinder.model.projectId)}"
        .items="${store.projectNames}"
></vaadin-combo-box>

...

await this.snippetProjectBinder.submitTo( entity => {
    // hack: because object-comboboxes seem not to work yet, we must map the
    // caption to the id:
    if(entity.projectId) {
        const project = store.projectByName(entity.projectId);
        if(project) {
            entity.projectId = project.id;
            entity.snippetId = snippet.id;
            SnippetProjectEndpoint.update(entity);
        }
    }
    return new Promise((resolve) => { resolve(entity); });
} );

Expected Note: the TBD part in the field() directive. There should be some API to tell the binder whether it should use the selectedItem or the value property of the combo-box. The default in V19 is selectedItem (see ComboBoxFieldStrategy)

<vaadin-combo-box
        label="Project"
        id="project"
        ...="${field(this.snippetProjectBinder.model.projectId, TBD)}"
        .items="${store.projects}"
        item-value-path="id"
        item-label-path="name"
></vaadin-combo-box>
haijian-vaadin commented 3 years ago

Related to https://github.com/vaadin/flow/issues/9675