Skip to content

Commit 3289fee

Browse files
committed
release(0.1.9): release 🎉
Added the required validator to the control. Now <ti-angular-tags-input required='true'> can be used
1 parent 86c6fc0 commit 3289fee

File tree

8 files changed

+85
-8
lines changed

8 files changed

+85
-8
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2019 IOMechs
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"build:lib": "ng build angular-tags-input",
99
"test": "ng test",
1010
"lint": "ng lint",
11-
"publish:lib": "npm run build:lib && cd ./dist/angular-tags-input && npm publish --access public",
11+
"copy:readme": "cp ./README.md ./projects/angular-tags-input/",
12+
"copy:license": "cp ./LICENSE ./projects/angular-tags-input/",
13+
"release": "npm run copy:readme && npm run build:lib && cd ./dist/angular-tags-input && npm publish --access public",
1214
"e2e": "ng e2e"
1315
},
1416
"private": true,

projects/angular-tags-input/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License
2+
3+
Copyright (c) 2019 IOMechs
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

projects/angular-tags-input/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@iomechs/angular-tags-input",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"peerDependencies": {
55
"@angular/common": "^8.2.11",
66
"@angular/core": "^8.2.11"

projects/angular-tags-input/src/lib/angular-tags-input.component.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
ViewEncapsulation,
1515
HostListener,
1616
} from '@angular/core';
17-
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
17+
import { ControlValueAccessor, NG_VALUE_ACCESSOR, NG_VALIDATORS, Validator, FormControl } from '@angular/forms';
1818

1919
import { AngularTagsInputService } from './angular-tags-input.service';
2020
import { TagInputComponent } from './tag-input/tag-input.component';
@@ -28,18 +28,20 @@ import { KEY_CODES } from './constants';
2828
templateUrl: './angular-tags-input.component.html',
2929
styleUrls: ['./angular-tags-input.component.scss'],
3030
providers: [
31-
getAngularTagsInputValueAccessor()
31+
getAngularTagsInputValueAccessor(),
32+
getAngularTagsInputValidatorsProvider()
3233
],
3334
encapsulation: ViewEncapsulation.None
3435
})
35-
export class AngularTagsInputComponent implements OnInit, AfterViewInit, ControlValueAccessor, OnChanges {
36+
export class AngularTagsInputComponent implements OnInit, AfterViewInit, ControlValueAccessor, OnChanges, Validator {
3637
@ViewChild(DropdownComponent, { static: false }) dropdown: DropdownComponent;
3738
@Input() config: AngularTagsInputConfig;
3839
@Input() tagsData: Array<any> = [];
3940
@Input() disabled = false;
4041
@Input() tagsLoading: boolean;
4142
@Input() dropDownTemplate: TemplateRef<any> = null;
4243
@Input() tagItemTemplate: TemplateRef<any> = null;
44+
@Input() required = false;
4345
@Output() tagRemoved = new EventEmitter();
4446
@Output() tagAdded = new EventEmitter();
4547
@Output() valueChanged = new EventEmitter();
@@ -181,6 +183,20 @@ export class AngularTagsInputComponent implements OnInit, AfterViewInit, Control
181183
this.onChange = fn;
182184
}
183185

186+
/**
187+
* Validator function for the form control
188+
* Doesn't do anything if the control is not required
189+
* If it is required, checks if the control contains value
190+
*/
191+
validate(control: FormControl) {
192+
if (this.required === false) {
193+
return null;
194+
}
195+
return (!!control.value && control.value.length) ? null : {
196+
required: true,
197+
};
198+
}
199+
184200
/**
185201
* @author Ahsan Ayaz
186202
* @desc Triggers when the tag input is focused
@@ -463,3 +479,11 @@ export function getAngularTagsInputValueAccessor() {
463479
multi: true,
464480
};
465481
}
482+
483+
export function getAngularTagsInputValidatorsProvider() {
484+
return {
485+
provide: NG_VALIDATORS,
486+
useExisting: forwardRef(() => AngularTagsInputComponent),
487+
multi: true,
488+
};
489+
}

projects/tags-input-demo/src/app/app.component.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,21 +304,25 @@
304304

305305
<div class="content" role="main">
306306
<form [formGroup]="simpleForm" class="main__form">
307-
<ti-angular-tags-input formControlName="users" class="main__form__ti"
307+
<ti-angular-tags-input formControlName="nestedData" class="main__form__ti"
308308
[tagsData]="sampleDataNested"
309309
(tagAdded)="onTagAdded($event)"
310310
(valueChanged)="onValChanged($event)"
311311
[config]="nestedTagsInputConfig"
312312
[dropDownTemplate]="ddNestedTemplate"
313313
>
314314
</ti-angular-tags-input>
315-
<ti-angular-tags-input formControlName="users" class="main__form__ti"
315+
<ti-angular-tags-input formControlName="simpleData" class="main__form__ti"
316316
[tagsData]="sampleDataSimple"
317317
(tagAdded)="onTagAdded($event)"
318318
(valueChanged)="onValChanged($event)"
319319
[config]="simpleTagsInputConfig"
320+
required="true"
320321
>
321322
</ti-angular-tags-input>
323+
<div class="main__form__error" *ngIf="simpleForm.get('simpleData').dirty && simpleForm.get('simpleData').hasError('required')">
324+
Value for simple data is required
325+
</div>
322326
</form>
323327
</div>
324328
<!-- Footer -->

projects/tags-input-demo/src/app/app.component.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
&__ti {
55
width: 100%;
66
}
7+
&__error {
8+
color: red;
9+
margin-top: 10px;
10+
}
711
}
812
}
913

projects/tags-input-demo/src/app/app.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ export class AppComponent {
5151

5252
constructor(private fb: FormBuilder, private tagsInputService: AngularTagsInputService) {
5353
this.simpleForm = this.fb.group({
54-
users: [[], []]
54+
simpleData: [[], []],
55+
nestedData: [[], []]
5556
});
5657
this.tagsInputService.setDebugMode(true);
5758
}

0 commit comments

Comments
 (0)