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

Commit b49ebcf

Browse files
devversionThomasBurleson
authored andcommitted
feat(dialog): add initial value option to prompt preset.
Fixes #7046 Closes #7090
1 parent c6c5d48 commit b49ebcf

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

src/components/dialog/demoBasicUsage/script.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ angular.module('dialogDemo1', ['ngMaterial'])
4040
$scope.showPrompt = function(ev) {
4141
// Appending dialog to document.body to cover sidenav in docs app
4242
var confirm = $mdDialog.prompt()
43-
.title('What would you name your dog?')
44-
.textContent('Bowser is a common name.')
45-
.placeholder('dog name')
46-
.ariaLabel('Dog name')
47-
.targetEvent(ev)
48-
.ok('Okay!')
49-
.cancel('I\'m a cat person');
43+
.title('What would you name your dog?')
44+
.textContent('Bowser is a common name.')
45+
.placeholder('Dog name')
46+
.ariaLabel('Dog name')
47+
.initialValue('Buddy')
48+
.targetEvent(ev)
49+
.ok('Okay!')
50+
.cancel('I\'m a cat person');
5051

5152
$mdDialog.show(confirm).then(function(result) {
5253
$scope.status = 'You decided to name your dog ' + result + '.';

src/components/dialog/dialog.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ function MdDialogDirective($$rAF, $mdTheming, $mdDialog) {
421421
* - $mdDialogPreset#htmlContent(string) - Sets the prompt message as HTML. Requires ngSanitize
422422
* module to be loaded. HTML is not run through Angular's compiler.
423423
* - $mdDialogPreset#placeholder(string) - Sets the placeholder text for the input.
424+
* - $mdDialogPreset#initialValue(string) - Sets the initial value for the prompt input.
424425
* - $mdDialogPreset#ok(string) - Sets the prompt "Okay" button text.
425426
* - $mdDialogPreset#cancel(string) - Sets the prompt "Cancel" button text.
426427
* - $mdDialogPreset#theme(string) - Sets the theme of the prompt dialog.
@@ -544,7 +545,7 @@ function MdDialogProvider($$interimElementProvider) {
544545
options: advancedDialogOptions
545546
})
546547
.addPreset('prompt', {
547-
methods: ['title', 'htmlContent', 'textContent', 'content', 'placeholder', 'ariaLabel',
548+
methods: ['title', 'htmlContent', 'textContent', 'initialValue', 'content', 'placeholder', 'ariaLabel',
548549
'ok', 'cancel', 'theme', 'css'],
549550
options: advancedDialogOptions
550551
});
@@ -562,7 +563,8 @@ function MdDialogProvider($$interimElementProvider) {
562563
' <p>{{::dialog.mdTextContent}}</p>',
563564
' </div>',
564565
' <md-input-container md-no-float ng-if="::dialog.$type == \'prompt\'" class="md-prompt-input-container">',
565-
' <input ng-keypress="dialog.keypress($event)" md-autofocus ng-model="dialog.result" placeholder="{{::dialog.placeholder}}">',
566+
' <input ng-keypress="dialog.keypress($event)" md-autofocus ng-model="dialog.result" ' +
567+
' placeholder="{{::dialog.placeholder}}">',
566568
' </md-input-container>',
567569
' </md-dialog-content>',
568570
' <md-dialog-actions>',
@@ -577,8 +579,14 @@ function MdDialogProvider($$interimElementProvider) {
577579
'</md-dialog>'
578580
].join('').replace(/\s\s+/g, ''),
579581
controller: function mdDialogCtrl() {
582+
var isPrompt = this.$type == 'prompt';
583+
584+
if (isPrompt && this.initialValue) {
585+
this.result = this.initialValue;
586+
}
587+
580588
this.hide = function() {
581-
$mdDialog.hide(this.$type === 'prompt' ? this.result : true);
589+
$mdDialog.hide(isPrompt ? this.result : true);
582590
};
583591
this.abort = function() {
584592
$mdDialog.cancel();

src/components/dialog/dialog.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ describe('$mdDialog', function() {
579579
.title('Title')
580580
.textContent('Hello world')
581581
.placeholder('placeholder text')
582+
.initialValue('initial value')
582583
.theme('some-theme')
583584
.css('someClass anotherClass')
584585
.ok('Next')
@@ -604,6 +605,7 @@ describe('$mdDialog', function() {
604605
expect(title.text()).toBe('Title');
605606
expect(contentBody.textContent).toBe('Hello world');
606607
expect(inputElement[0].placeholder).toBe('placeholder text');
608+
expect(inputElement.val()).toBe('initial value');
607609
expect(buttons.length).toBe(2);
608610
expect(buttons.eq(0).text()).toBe('Next');
609611
expect(theme).toBe('some-theme');

0 commit comments

Comments
 (0)