From a27fefd4a83bccc08094614c728f37c422dbbbdc Mon Sep 17 00:00:00 2001 From: Conrad Damon Date: Tue, 27 Mar 2018 09:22:08 -0700 Subject: [PATCH 1/2] Only show placeholder if no tags and no input --- src/tags-input.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tags-input.js b/src/tags-input.js index 8158a3d4..8277405b 100644 --- a/src/tags-input.js +++ b/src/tags-input.js @@ -302,7 +302,7 @@ export default function TagsInputDirective($timeout, $document, $window, $q, tag scope.$watch('tags.length', () => { setElementValidity(); - + input.attr('placeholder', tagList.items.length === 0 && input.val() === '' ? options.placeholder : ''); // ngModelController won't trigger validators when the model changes (because it's an array), // so we need to do it ourselves. Unfortunately this won't trigger any registered formatter. ngModelCtrl.$validate(); From e12430027a673afe27b3f285bf81ed38cf63e246 Mon Sep 17 00:00:00 2001 From: Conrad Damon Date: Tue, 27 Mar 2018 17:33:08 -0700 Subject: [PATCH 2/2] fix(tagsInput): Manage placeholder Show the placeholder only if there is no input and no tags. Closes #855. --- src/tags-input.js | 3 +++ test/tags-input.spec.js | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/tags-input.js b/src/tags-input.js index 8277405b..693d04d8 100644 --- a/src/tags-input.js +++ b/src/tags-input.js @@ -302,7 +302,10 @@ export default function TagsInputDirective($timeout, $document, $window, $q, tag scope.$watch('tags.length', () => { setElementValidity(); + + // show placeholder only if there are no tags and no input input.attr('placeholder', tagList.items.length === 0 && input.val() === '' ? options.placeholder : ''); + // ngModelController won't trigger validators when the model changes (because it's an array), // so we need to do it ourselves. Unfortunately this won't trigger any registered formatter. ngModelCtrl.$validate(); diff --git a/test/tags-input.spec.js b/test/tags-input.spec.js index af1582f1..fffb0ee0 100644 --- a/test/tags-input.spec.js +++ b/test/tags-input.spec.js @@ -847,6 +847,29 @@ describe('tags-input directive', () => { // Assert expect(isolateScope.options.placeholder).toBe('Add a tag'); }); + + it('clears the placeholder if there is at least one tag', () => { + // Arrange + compile(); + + // Act + newTag('foo'); + + // Assert + expect(getInput().attr('placeholder')).toBe(''); + }); + + it('restores the placeholder when there are no tags', () => { + // Arrange + compile(); + + // Act + newTag('foo'); + getRemoveButton(0).click(); + + // Assert + expect(getInput().attr('placeholder')).toBe('Add a tag'); + }); }); describe('remove-tag-symbol option', () => {