From 0eacc9647ed7b12fac8db23cb711bb6c38a8c31a Mon Sep 17 00:00:00 2001 From: Michael Benford Date: Sat, 12 Apr 2014 14:59:53 -0300 Subject: [PATCH] fix(autosize): Re-size input when placeholder changes Fix the autosize directive so it correctly re-sizes the input element when it's empty and its placeholder changes due to interpolation. Closes #110 --- src/autosize.js | 6 ++++++ test/autosize.spec.js | 15 +++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/autosize.js b/src/autosize.js index ff6c1c46..e88ff536 100644 --- a/src/autosize.js +++ b/src/autosize.js @@ -44,6 +44,12 @@ tagsInput.directive('tiAutosize', function() { ctrl.$parsers.unshift(resize); ctrl.$formatters.unshift(resize); + + attrs.$observe('placeholder', function(value) { + if (!ctrl.$modelValue) { + resize(value); + } + }); } }; }); \ No newline at end of file diff --git a/test/autosize.spec.js b/test/autosize.spec.js index 5d4965ac..51f53441 100644 --- a/test/autosize.spec.js +++ b/test/autosize.spec.js @@ -83,6 +83,21 @@ describe('autosize directive', function() { expect(element.css('width')).toBe(getTextWidth('Some placeholder')); }); + it('sets the input width as the placeholder width when the input is empty and the placeholder changes', function() { + // Arrange + $scope.placeholder = 'Some placeholder'; + compile('placeholder="{{placeholder}}"'); + $scope.model = ''; + $scope.$digest(); + + // Act + $scope.placeholder = 'Some very lengthy placeholder'; + $scope.$digest(); + + // Assert + expect(element.css('width')).toBe(getTextWidth('Some very lengthy placeholder')); + }); + it('clears the input width when it cannot be calculated', function() { // Arrange container.hide();