Skip to content

Commit

Permalink
[Issue-79] Angular directive: use $formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
nosir committed Aug 16, 2016
1 parent 6704e3e commit 35cdebe
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 49 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
module.exports = {
"parser": "babel-eslint",
"globals": {
"angular": 1,
"Cleave": 1
"angular": 1
},
"rules": {
"quotes": [
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ First include the directive module:
<script src="cleave-phone.{country}.js"></script>
```

And in the model:
And in your model:

```js
angular.module('app', ['cleave.js'])
Expand All @@ -165,7 +165,7 @@ angular.module('app', ['cleave.js'])
};

$scope.model = {
value: ''
rawValue: ''
};

$scope.options = {
Expand All @@ -177,11 +177,11 @@ angular.module('app', ['cleave.js'])
});
```

Then easily you can apply `cleave` directive with `input` field:
Then easily you can apply `cleave` directive to `input` field:

```html
<div ng-controller="AppController">
<input ng-model="model.value" ng-whatever="..." type="text" placeholder="Enter credit card number"
<input ng-model="model.rawValue" ng-whatever="..." type="text" placeholder="Enter credit card number"
cleave options="options.creditCard"/>
</div>
```
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"form",
"input"
],
"version": "0.6.9",
"version": "0.6.10",
"author": {
"name": "Max Huang",
"email": "[email protected]",
Expand Down
31 changes: 19 additions & 12 deletions dist/cleave-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -823,19 +823,26 @@ angular.module('cleave.js', [])
onValueChange: '&?'
},

controller: function ($scope, $element) {
$scope.cleave = new Cleave($element[0], $scope.options);
$scope.onValueChange = $scope.onValueChange || null;
},
compile: function () {
return {
pre: function ($scope, $element, attrs, ngModelCtrl) {
$scope.cleave = new window.Cleave($element[0], $scope.options);

link: function ($scope, $element, attrs, ngModel) {
if ($scope.onValueChange) {
$scope.$watch(function () {
return ngModel.$modelValue;
}, function () {
$scope.onValueChange()($scope.cleave.getFormattedValue(), $scope.cleave.getRawValue());
});
}
ngModelCtrl.$formatters.push(function (val) {
$scope.cleave.setRawValue(val);

return $scope.cleave.getFormattedValue();
});

ngModelCtrl.$parsers.push(function (newFormattedValue) {
if ($scope.onValueChange) {
$scope.onValueChange()(newFormattedValue);
}

return $scope.cleave.getRawValue();
});
}
};
}
};
});
4 changes: 2 additions & 2 deletions dist/cleave-angular.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/cleave-react.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/cleave.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 12 additions & 12 deletions doc/angularjs-directive-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ angular.module('app', ['cleave.js'])
};

$scope.model = {
value: ''
rawValue: ''
};

$scope.options = {
Expand All @@ -38,36 +38,38 @@ angular.module('app', ['cleave.js'])
});
```

Then you can just use `cleave` directive with `input` field:
Then easily you can apply `cleave` directive to `input` field:

```html
<div ng-controller="AppController">
<input ng-model="model.value" ng-whatever="..." type="text" placeholder="Enter credit card number"
<input ng-model="model.rawValue" ng-whatever="..." type="text" placeholder="Enter credit card number"
cleave options="options.creditCard"/>
</div>
```

## Advanced usage

Sometimes you might want to get the raw value. Here is the way to deal with it:
By using `Cleave.js`, angular renders the input field with the formatted value, but keeps `ng-model` value as the raw value.

If you are looking to obtain the formatted value, here is the way:

First in you model:

```js
angular.module('app', ['cleave.js'])

.controller('AppController', function($scope) {
$scope.onCleaveValueChange = function(formattedValue, rawValue) {
$scope.onCleaveValueChange = function(formattedValue) {
$scope.model.formattedValue = formattedValue;
$scope.model.rawValue = rawValue;
};

$scope.onCreditCardTypeChanged = function(type) {
$scope.model.creditCardType = type;
};

$scope.model = {
value: ''
rawValue: '',
formattedValue: ''
};

$scope.options = {
Expand All @@ -83,18 +85,16 @@ Then in your html:

```html
<div ng-controller="AppController">
<input ng-model="model.value" ng-whatever="..." type="text" placeholder="Enter credit card number"
<input ng-model="model.rawValue" ng-whatever="..." type="text" placeholder="Enter credit card number"
cleave options="options.creditCard" on-value-change="onCleaveValueChange"/>

<p>raw value: {{model.rawValue}}</p>
<p>raw (ng-model) value: {{model.rawValue}}</p>
<p>formatted value: {{model.formattedValue}}</p>

<p>ng-model value: {{model.value}}</p>

<p>type: {{model.creditCardType}}</p>
</div>
```

As you can see, by passing the function (without `()`) to `on-value-change`, you register a callback from `cleave.js` directive.

Then in the callback, it returns `formattedValue` and `rawValue`. `formattedValue` here is same as your `ng-model` value.
Then in the callback, it returns `formattedValue` as the only parameter.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"form",
"input"
],
"version": "0.6.9",
"version": "0.6.10",
"author": {
"name": "Max Huang",
"url": "http://github.com/nosir",
Expand Down
31 changes: 19 additions & 12 deletions src/Cleave.angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,26 @@ angular.module('cleave.js', [])
onValueChange: '&?'
},

controller: function ($scope, $element) {
$scope.cleave = new Cleave($element[0], $scope.options);
$scope.onValueChange = $scope.onValueChange || null;
},
compile: function () {
return {
pre: function ($scope, $element, attrs, ngModelCtrl) {
$scope.cleave = new window.Cleave($element[0], $scope.options);

ngModelCtrl.$formatters.push(function (val) {
$scope.cleave.setRawValue(val);

return $scope.cleave.getFormattedValue();
});

ngModelCtrl.$parsers.push(function (newFormattedValue) {
if ($scope.onValueChange) {
$scope.onValueChange()(newFormattedValue);
}

link: function ($scope, $element, attrs, ngModel) {
if ($scope.onValueChange) {
$scope.$watch(function () {
return ngModel.$modelValue;
}, function () {
$scope.onValueChange()($scope.cleave.getFormattedValue(), $scope.cleave.getRawValue());
});
}
return $scope.cleave.getRawValue();
});
}
};
}
};
});
2 changes: 1 addition & 1 deletion test/NumeralFormatter_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('NumeralFormatter', function () {
title.push('Thousands Group Style: ' + numeral.thousandsGroupStyle);
}

if (numeral.numeralDecimalScale !== undefined) {
if (numeral.numeralDecimalScale || numeral.numeralDecimalScale === 0) {
title.push('Decimal Scale: ' + numeral.numeralDecimalScale);
}

Expand Down

0 comments on commit 35cdebe

Please sign in to comment.