Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dialog): allow for a config object to be passed on init #1679

Merged
merged 1 commit into from
Nov 9, 2016

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented Nov 1, 2016

Adds the ability to pass in a configuration object when creating a new MdDialogConfig. This should make working with a lot of dialog options a little easier.

@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Nov 1, 2016
role: DialogRole = 'dialog';

constructor(config?: MdDialogConfigObject) {
Object.assign(this, config);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessary- TypeScript uses structural typing, so you can pass an object literal with the appropriate properties everything should work. What's missing is that the properties should be marked as optional, e.g.:

role?: DialogRole = 'dialog';

(in typescript playground)

We can mark everything in here as optional this way except for viewContainerRef, which is required until angular/angular#9293 is resolved.

Copy link
Member Author

@crisbeto crisbeto Nov 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been trying this approach out, but it seems like the role stays undefined in this case. E.g. check the generated JS here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see what you mean. I want to keep it such the preferred API is

dialog.open(SomeComponent, {
  someConfig: true
});

And all the defaults are applied.

How about just having a method in MdDialog like _applyConfigDefaults that takes the config and applies the default values for things that are unset?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good. If we remove the MdDialogConfig class, it would be a breaking change, right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to remove the class; it will still be the type of the argument, people can just additionally use a matching literal.

@crisbeto crisbeto force-pushed the dialog-config-object branch 2 times, most recently from cd8b25e to e6fe109 Compare November 3, 2016 17:25
@crisbeto
Copy link
Member Author

crisbeto commented Nov 3, 2016

Updated with your feedback @jelbourn.

/**
* Applies default options to the dialog config.
* @param {MdDialogConfig} config Config to be modified.
* @returns The new configuration object.
Copy link
Member

@devversion devversion Nov 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the JSDoc types necessary here? I think the TS types should be enough.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Removed the types and renamed the argument to match the other methods.

@crisbeto crisbeto force-pushed the dialog-config-object branch 2 times, most recently from ca8ec47 to b4bd730 Compare November 4, 2016 16:22
private _applyConfigDefaults(dialogConfig: MdDialogConfig): MdDialogConfig {
let defaults: MdDialogConfig = {
role: 'dialog',
disableClose: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not leave the defaults in MdDialogConfig and simply say new MdDialogConfig here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I'll update it.

Adds the ability to pass in an object literal, that matches the signature of `MdDialogConfig`, when opening a dialog.
@crisbeto
Copy link
Member Author

crisbeto commented Nov 8, 2016

Updated again.

@jelbourn
Copy link
Member

jelbourn commented Nov 9, 2016

LGTM

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker and removed pr: needs review labels Nov 9, 2016
@jelbourn jelbourn merged commit f525db1 into angular:master Nov 9, 2016
rolandjitsu pushed a commit to rolandjitsu/material2 that referenced this pull request Nov 10, 2016
…ar#1679)

Adds the ability to pass in an object literal, that matches the signature of `MdDialogConfig`, when opening a dialog.
@Nogostradamus
Copy link

Nogostradamus commented Nov 28, 2016

I have following error:
Argument of type '{ disableClose: boolean; }' is not assignable to parameter of type 'MdDialogConfig'.
Object literal may only specify known properties, and 'disableClose' does not exist in type 'MdDialogConfig'.

this is using example from MdDialog readme:
this.dialogRef = this.dialog.open(PizzaDialog, { disableClose: false });

I'm using Angular 2.2.3 and material 2.0.0-alpha.10
(angular CLI)

@crisbeto
Copy link
Member Author

It works for me @Nogostradamus, can you retry checking your Material version?

@Nogostradamus
Copy link

Sorry, my bad. Now it works. I was still on experimental pizza v9 after an update. I had to removed caret from package.json "@angular/material": "^2.0.0-alpha.10" in order to update successfully.

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants