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

Commit f730527

Browse files
Merge pull request #133 from lukepfeiffer10/master
Add config option for default placeholder
2 parents 6c7b72d + 2162212 commit f730527

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,18 @@ Inside of `ui-options`, you can customize these four properties:
6565
* `clearOnBlur` - default: `true`,
6666
* `clearOnBlurPlaceholder` - default: `false`,
6767
* `eventsToHandle` - default: `['input', 'keyup', 'click', 'focus']`
68+
* `addDefaultPlaceholder` - default: `true`
6869

69-
When customizing `eventsToHandle` or `clearOnBlur`, the value you supply will replace the default. To customize `eventsToHandle`, be sure to replace the entire array.
70-
71-
When setting `clearOnBlurPlaceholder` to `true`, it will show the placeholder text instead of the empty mask. It requires the `ui-mask-placeholder` attribute to be set on the input to display properly.
70+
When customizing `eventsToHandle`, `clearOnBlur`, or `addDefaultPlaceholder`, the value you supply will replace the default. To customize `eventsToHandle`, be sure to replace the entire array.
7271

7372
Whereas, `maskDefinitions` is an object, so any custom object you supply will be merged together with the defaults using `angular.extend()`. This allows you to override the defaults selectively, if you wish.
7473

74+
When setting `clearOnBlurPlaceholder` to `true`, it will show the placeholder text instead of the empty mask. It requires the `ui-mask-placeholder` attribute to be set on the input to display properly.
75+
7576
#### Global customization
7677
In addition to customizing behaviors for a specific element, you can also customize the behaviors globally. To do this, simply use the `uiMaskConfig` provider in your app configuration. Example:
7778

78-
```sh
79+
```javascript
7980
app.config(['uiMask.ConfigProvider', function(uiMaskConfigProvider) {
8081
uiMaskConfigProvider.maskDefinitions({'A': /[a-z], '*': /[a-zA-Z0-9]/});
8182
uiMaskConfigProvider.clearOnBlur(false);
@@ -95,6 +96,9 @@ Allows customizing the mask placeholder when a user has focused the input elemen
9596
#### uiMaskPlaceholderChar
9697
Allows customizing the mask placeholder character. The default mask placeholder is `_`.
9798
99+
#### addDefaultPlaceholder
100+
The default placeholder is constructed from the `ui-mask` definition so a mask of `999-9999` would have a default placeholder of `___-____`; unless you have overridden the default placeholder character.
101+
98102
## Testing
99103
100104
Most of the testing is done using Karma to run the tests and SauceLabs to provide the different browser environments to test against.

src/mask.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ angular.module('ui.mask', [])
1010
},
1111
clearOnBlur: true,
1212
clearOnBlurPlaceholder: false,
13-
eventsToHandle: ['input', 'keyup', 'click', 'focus']
13+
eventsToHandle: ['input', 'keyup', 'click', 'focus'],
14+
addDefaultPlaceholder: true
1415
})
1516
.provider('uiMask.Config', function() {
1617
var options = {};
@@ -24,6 +25,9 @@ angular.module('ui.mask', [])
2425
this.eventsToHandle = function(eventsToHandle) {
2526
return options.eventsToHandle = eventsToHandle;
2627
};
28+
this.addDefaultPlaceholder = function(addDefaultPlaceholder) {
29+
return options.addDefaultPlaceholder = addDefaultPlaceholder;
30+
};
2731
this.$get = ['uiMaskConfig', function(uiMaskConfig) {
2832
var tempOptions = uiMaskConfig;
2933
for(var prop in options)
@@ -209,7 +213,7 @@ angular.module('ui.mask', [])
209213
if (iAttrs.maxlength) { // Double maxlength to allow pasting new val at end of mask
210214
iElement.attr('maxlength', maskCaretMap[maskCaretMap.length - 1] * 2);
211215
}
212-
if ( ! originalPlaceholder) {
216+
if ( ! originalPlaceholder && linkOptions.addDefaultPlaceholder) {
213217
iElement.attr('placeholder', maskPlaceholder);
214218
}
215219
var viewValue = controller.$modelValue;

test/maskSpec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,17 @@ describe("uiMask", function () {
584584
input.triggerHandler("blur");
585585
expect(input.val()).toBe("f111");
586586
});
587+
588+
it("should accept new addDefaultPlaceholder value set per element", function() {
589+
scope.options = {
590+
addDefaultPlaceholder: false
591+
};
592+
593+
var input = compileElement(inputHtml);
594+
scope.$apply("x = ''");
595+
scope.$apply("mask = '@999'");
596+
expect(input.attr('placeholder')).toBe(undefined);
597+
});
587598
});
588599

589600
describe("blurring", function () {
@@ -683,13 +694,16 @@ describe("uiMask", function () {
683694
var service = $injector.invoke(uiMaskConfigProvider.$get);
684695
expect(service.clearOnBlur).toEqual(true);
685696
expect(service.clearOnBlurPlaceholder).toEqual(false);
697+
expect(service.addDefaultPlaceholder).toEqual(true);
686698
}));
687699

688700
it("should merge default values with configured values", inject(function($injector) {
689701
uiMaskConfigProvider.clearOnBlur(false);
702+
uiMaskConfigProvider.addDefaultPlaceholder(false);
690703
var service = $injector.invoke(uiMaskConfigProvider.$get);
691704
expect(service.clearOnBlur).toEqual(false);
692705
expect(service.clearOnBlurPlaceholder).toEqual(false);
706+
expect(service.addDefaultPlaceholder).toEqual(false);
693707
}));
694708
});
695709

0 commit comments

Comments
 (0)