From 33443966c8e8cac85a863bb181d4a4aff00baab4 Mon Sep 17 00:00:00 2001 From: Yves Brissaud Date: Tue, 10 Dec 2013 21:49:31 +0100 Subject: feat($animate): animate dirty, pristine, valid, invalid for form/fields Add css animations when form or field status change to/from dirty, pristine, valid or invalid. This works like animation system present with ngClass, ngShow, etc. Closes #5378 --- src/ng/directive/form.js | 57 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 48 insertions(+), 9 deletions(-) (limited to 'src/ng/directive/form.js') diff --git a/src/ng/directive/form.js b/src/ng/directive/form.js index c5e39cde..4e6ec20d 100644 --- a/src/ng/directive/form.js +++ b/src/ng/directive/form.js @@ -46,8 +46,8 @@ var nullFormCtrl = { * */ //asks for $scope to fool the BC controller module -FormController.$inject = ['$element', '$attrs', '$scope']; -function FormController(element, attrs) { +FormController.$inject = ['$element', '$attrs', '$scope', '$animate']; +function FormController(element, attrs, $scope, $animate) { var form = this, parentForm = element.parent().controller('form') || nullFormCtrl, invalidCount = 0, // used to easily determine if we are valid @@ -70,9 +70,8 @@ function FormController(element, attrs) { // convenience method for easy toggling of classes function toggleValidCss(isValid, validationErrorKey) { validationErrorKey = validationErrorKey ? '-' + snake_case(validationErrorKey, '-') : ''; - element. - removeClass((isValid ? INVALID_CLASS : VALID_CLASS) + validationErrorKey). - addClass((isValid ? VALID_CLASS : INVALID_CLASS) + validationErrorKey); + $animate.removeClass(element, (isValid ? INVALID_CLASS : VALID_CLASS) + validationErrorKey); + $animate.addClass(element, (isValid ? VALID_CLASS : INVALID_CLASS) + validationErrorKey); } /** @@ -173,7 +172,8 @@ function FormController(element, attrs) { * state (ng-dirty class). This method will also propagate to parent forms. */ form.$setDirty = function() { - element.removeClass(PRISTINE_CLASS).addClass(DIRTY_CLASS); + $animate.removeClass(element, PRISTINE_CLASS); + $animate.addClass(element, DIRTY_CLASS); form.$dirty = true; form.$pristine = false; parentForm.$setDirty(); @@ -194,7 +194,8 @@ function FormController(element, attrs) { * saving or resetting it. */ form.$setPristine = function () { - element.removeClass(DIRTY_CLASS).addClass(PRISTINE_CLASS); + $animate.removeClass(element, DIRTY_CLASS); + $animate.addClass(element, PRISTINE_CLASS); form.$dirty = false; form.$pristine = true; forEach(controls, function(control) { @@ -249,6 +250,8 @@ function FormController(element, attrs) { * - `ng-pristine` is set if the form is pristine. * - `ng-dirty` is set if the form is dirty. * + * Keep in mind that ngAnimate can detect each of these classes when added and removed. + * * * # Submitting a form and preventing the default action * @@ -282,15 +285,48 @@ function FormController(element, attrs) { * @param {string=} name Name of the form. If specified, the form controller will be published into * related scope, under this name. * + * ## Animation Hooks + * + * Animations in ngForm are triggered when any of the associated CSS classes are added and removed. These + * classes are: `.pristine`, `.dirty`, `.invalid` and `.valid` as well as any other validations that + * are performed within the form. Animations in ngForm are similar to how they work in ngClass and + * animations can be hooked into using CSS transitions, keyframes as well as JS animations. + * + * The following example shows a simple way to utilize CSS transitions to style a form element + * that has been rendered as invalid after it has been validated: + * + *
+ * //be sure to include ngAnimate as a module to hook into more
+ * //advanced animations
+ * .my-form {
+ *   transition:0.5s linear all;
+ *   background: white;
+ * }
+ * .my-form.ng-invalid {
+ *   background: red;
+ *   color:white;
+ * }
+ * 
+ * * @example - + -
+ + userType: Required!
userType = {{userType}}
@@ -322,6 +358,9 @@ function FormController(element, attrs) { }); + * + * @param {string=} name Name of the form. If specified, the form controller will be published into + * related scope, under this name. */ var formDirectiveFactory = function(isNgForm) { return ['$timeout', function($timeout) { -- cgit v1.2.3