GetTerminus / terminus-oss

A collection of open source libraries.
MIT License
11 stars 10 forks source link

New component: dialog #505

Open benjamincharity opened 3 years ago

benjamincharity commented 3 years ago

This will offer 2 basic tools:

a) The dialog service b) a base dialog component

Requirements


Example implementations

Service-based examples
// The consumer defined data structure:
export interface MyDialogData {
  animal: string;
  name: string;
}

// The consumer component that needs a dialog:
@Component({
  selector: 'consumer-component',
  templateUrl: 'consumer-component.html',
})
export class ConsumerComponent {
  animal: string;
  name: string;

  constructor(public modal: TsModalService) {}

  open(): void {
    const modalRef = this.modal.open(ConsumerExampleModal, {
      data: {name: this.name, animal: this.animal}
    });

    modalRef.afterClosed().subscribe(result => {
      console.log('The modal was closed');
      this.animal = result;
    });
  }

}

// The actual dialog component that will be shown.
// The consumer can rely on a `<ts-dialog>|<ts-dialog-header>|<ts-dialog-footer>` components to get basic layout and styles.
@Component({
  selector: 'consumer-dialog-content',
  templateUrl: 'consumer-dialog-content.html',
})
export class ConsumerDialogContent {
  constructor(
    public dialogRef: TsDialogRef<ConsumerDialogContent>,
    @Inject(TS_DIALOG_DATA) public data: MyDialogData,
  ) {}

  onCancelClick(): void {
    this.dialogRef.close();
  }
}
Component-based examples

NOTE: I think that long-term, this approach is too simplistic; but I am leaving the examples here for discussion.

<ts-dialog title="My Title" [isVisible]="true">
  Content
</ts-dialog>

<ts-dialog [isVisible]="true">
  <ts-dialog-header>
    <h2>My title</h2>
    <ts-chip badge>Read Only</ts-chip>
  </ts-dialog-header>

  <ts-dialog-footer>
    <button>Cancel</button>
    <button>Submit</button>
  </ts-dialog-footer>
</ts-dialog>

<ts-dialog maxHeight="86%" maxWidth="400px">
  Content
</ts-dialog>
<!-- standard api -->
<button (click)="dialog1.isVisible ? dialog1.hide() : dialog1.show()">Toggle dialog 1</button>
<button (click)="dialog2.isVisible ? dialog2.hide() : dialog2.show()">Toggle dialog 2</button>

<!-- shortcut -->
<button (click)="dialog2.toggle()">Toggle dialog 2</button>

<ts-dialog #tsDialog="dialog1">First modal</ts-dialog>
<ts-dialog #tsDialog="dialog2">Second modal</ts-dialog>

Resources

image.png

image.png

benjamincharity commented 3 years ago

What currently exists: https://github.com/GetTerminus/engage-gui/pull/1130/commits/28af7b4682a25457afff331eac10c85f3b3e8b30