Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

feat(dialog): add default value to prompt preset. #7090

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/components/dialog/demoBasicUsage/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ angular.module('dialogDemo1', ['ngMaterial'])
$scope.showPrompt = function(ev) {
// Appending dialog to document.body to cover sidenav in docs app
var confirm = $mdDialog.prompt()
.title('What would you name your dog?')
.textContent('Bowser is a common name.')
.placeholder('dog name')
.ariaLabel('Dog name')
.targetEvent(ev)
.ok('Okay!')
.cancel('I\'m a cat person');
.title('What would you name your dog?')
.textContent('Bowser is a common name.')
.placeholder('Dog name')
.ariaLabel('Dog name')
.initialValue('Buddy')
.targetEvent(ev)
.ok('Okay!')
.cancel('I\'m a cat person');

$mdDialog.show(confirm).then(function(result) {
$scope.status = 'You decided to name your dog ' + result + '.';
Expand Down
14 changes: 11 additions & 3 deletions src/components/dialog/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ function MdDialogDirective($$rAF, $mdTheming, $mdDialog) {
* - $mdDialogPreset#htmlContent(string) - Sets the prompt message as HTML. Requires ngSanitize
* module to be loaded. HTML is not run through Angular's compiler.
* - $mdDialogPreset#placeholder(string) - Sets the placeholder text for the input.
* - $mdDialogPreset#initialValue(string) - Sets the initial value for the prompt input.
* - $mdDialogPreset#ok(string) - Sets the prompt "Okay" button text.
* - $mdDialogPreset#cancel(string) - Sets the prompt "Cancel" button text.
* - $mdDialogPreset#theme(string) - Sets the theme of the prompt dialog.
Expand Down Expand Up @@ -544,7 +545,7 @@ function MdDialogProvider($$interimElementProvider) {
options: advancedDialogOptions
})
.addPreset('prompt', {
methods: ['title', 'htmlContent', 'textContent', 'content', 'placeholder', 'ariaLabel',
methods: ['title', 'htmlContent', 'textContent', 'initialValue', 'content', 'placeholder', 'ariaLabel',
'ok', 'cancel', 'theme', 'css'],
options: advancedDialogOptions
});
Expand All @@ -562,7 +563,8 @@ function MdDialogProvider($$interimElementProvider) {
' <p>{{::dialog.mdTextContent}}</p>',
' </div>',
' <md-input-container md-no-float ng-if="::dialog.$type == \'prompt\'" class="md-prompt-input-container">',

Choose a reason for hiding this comment

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

@devversion should this be changed to === instead of == while you're in the area?

PR looks good, thanks for doing this.

Copy link
Member Author

Choose a reason for hiding this comment

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

@zbjornson The IDE's and Linters are throwing a warning when using that.

' <input ng-keypress="dialog.keypress($event)" md-autofocus ng-model="dialog.result" placeholder="{{::dialog.placeholder}}">',
' <input ng-keypress="dialog.keypress($event)" md-autofocus ng-model="dialog.result" ' +
' placeholder="{{::dialog.placeholder}}">',
' </md-input-container>',
' </md-dialog-content>',
' <md-dialog-actions>',
Expand All @@ -577,8 +579,14 @@ function MdDialogProvider($$interimElementProvider) {
'</md-dialog>'
].join('').replace(/\s\s+/g, ''),
controller: function mdDialogCtrl() {
var isPrompt = this.$type == 'prompt';

if (isPrompt && this.initialValue) {
this.result = this.initialValue;
}

this.hide = function() {
$mdDialog.hide(this.$type === 'prompt' ? this.result : true);
$mdDialog.hide(isPrompt ? this.result : true);
};
this.abort = function() {
$mdDialog.cancel();
Expand Down
4 changes: 3 additions & 1 deletion src/components/dialog/dialog.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ describe('$mdDialog', function() {
.title('Title')
.textContent('Hello world')
.placeholder('placeholder text')
.initialValue('initial value')
.theme('some-theme')
.css('someClass anotherClass')
.ok('Next')
Expand All @@ -604,6 +605,7 @@ describe('$mdDialog', function() {
expect(title.text()).toBe('Title');
expect(contentBody.textContent).toBe('Hello world');
expect(inputElement[0].placeholder).toBe('placeholder text');
expect(inputElement.val()).toBe('initial value');
expect(buttons.length).toBe(2);
expect(buttons.eq(0).text()).toBe('Next');
expect(theme).toBe('some-theme');
Expand Down Expand Up @@ -634,7 +636,7 @@ describe('$mdDialog', function() {
.parent(parent)
.textContent('Hello world')
.placeholder('placeholder text')
)
);

runAnimation(parent.find('md-dialog'));

Expand Down