Skip to content

Commit fc5593e

Browse files
georgmuGeorg Müller
authored andcommitted
make DataPassingModal story type-safe
1 parent 7ae4141 commit fc5593e

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/modal/stories/data-passing.component.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,33 @@ import { InputModal } from "./input-modal.component";
1212
selector: "app-data-passing-modal",
1313
template: `
1414
<button class="cds--btn cds--btn--primary" (click)="openModal()">Open Modal</button>
15-
<h3>Data passed from input modal on input change: </h3>{{ modalInputValue }}
15+
<h3>Data passed from input modal on close: </h3>{{ modalInputValue }}
1616
`
1717
})
18-
export class DataPassingModal implements AfterContentInit {
18+
export class DataPassingModal {
1919
@Input() modalText = "Hello, World";
2020
@Input() size = "md";
2121

2222
protected modalInputValue = "";
23-
protected data: Observable<string> = new Subject<string>();
2423

2524
constructor(protected modalService: ModalService) { }
2625

2726
openModal() {
28-
this.modalService.create({
27+
const componentRef = this.modalService.create({
2928
component: InputModal,
3029
inputs: {
3130
modalText: this.modalText,
3231
inputValue: this.modalInputValue,
3332
size: this.size,
34-
data: this.data
3533
}
3634
});
37-
}
3835

39-
ngAfterContentInit() {
40-
this.data.subscribe(value => this.modalInputValue = value);
36+
componentRef.instance.close.subscribe((result: string) => {
37+
if (result === undefined) {
38+
console.log("Modal closed without value");
39+
} else {
40+
this.modalInputValue = result;
41+
}
42+
});
4143
}
4244
}

src/modal/stories/input-modal.component.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,24 @@ import { ModalService, BaseModal } from "../";
2020
</section>
2121
<cds-modal-footer>
2222
<button class="cds--btn cds--btn--secondary" (click)="closeModal()">Cancel</button>
23-
<button class="cds--btn cds--btn--primary" modal-primary-focus (click)="closeModal()">Save</button>
23+
<button class="cds--btn cds--btn--primary" modal-primary-focus (click)="closeModal(outputValue)">Save</button>
2424
</cds-modal-footer>
2525
</cds-modal>
2626
`
2727
})
28-
export class InputModal extends BaseModal {
28+
export class InputModal extends BaseModal<string> {
29+
30+
protected outputValue = this.inputValue;
31+
2932
constructor(
3033
@Inject("modalText") public modalText,
3134
@Inject("size") public size,
32-
@Inject("data") public data,
3335
@Inject("inputValue") public inputValue,
3436
protected modalService: ModalService) {
3537
super();
3638
}
3739

3840
onChange(event) {
39-
this.data.next(event.target.value);
41+
this.outputValue = event.target.value;
4042
}
4143
}

0 commit comments

Comments
 (0)