Gathering detailed insights and metrics for ngx-chips
Gathering detailed insights and metrics for ngx-chips
Gathering detailed insights and metrics for ngx-chips
Gathering detailed insights and metrics for ngx-chips
npm install ngx-chips
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
903 Stars
629 Commits
358 Forks
27 Watching
26 Branches
53 Contributors
Updated on 23 Nov 2024
TypeScript (81.2%)
SCSS (12.89%)
HTML (5.12%)
JavaScript (0.79%)
Cumulative downloads
Total Downloads
Last day
-5.3%
6,406
Compared to previous day
Last week
2.3%
35,055
Compared to previous week
Last month
3.4%
145,515
Compared to previous month
Last year
-4.7%
1,710,218
Compared to previous year
2
This is a component for Angular >= 4. Design and API are blandly inspired by Angular Material's md-chips. Formerly called ng2-tag-input.
Check out the live demo.
NB: This repository is currently unmaintained. Please fork or use Angular Material's chips module, it got better.
npm i ngx-chips // OR
yarn add ngx-chips
Notice: the latest version on NPM may not reflect the branch master
. Open an issue and tag me if you need it to be published.
Ensure you import the module and the dependencies:
1import { TagInputModule } from 'ngx-chips'; 2import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; // this is needed! 3import { FormsModule, ReactiveFormsModule } from '@angular/forms'; 4 5@NgModule({ 6 imports: [ 7 TagInputModule, 8 BrowserAnimationsModule, 9 FormsModule, 10 ReactiveFormsModule 11 ...OtherModules 12 ] // along with your other modules 13}) 14export class AppModule {}
ngModel
- [string[] | TagModel[]
] - Model of the component. Accepts an array of strings as input OR an array of objects.If you do use an array of objects, make sure you:
value
and display
. Value
will uniquely identify the items, display
will be the value displayed.identifyBy
and displayBy
Notice: the items provided to the model won't change, but the items added to the model will have the format { display, value }. If you do provide identifyBy
and displayBy
, these will be used as format for the user-entered tags.
placeholder
- [?string
]
String that sets the placeholder of the input for entering new terms.
secondaryPlaceholder
- [?string
]
String that sets the placeholder of the input for entering new terms when there are 0 items entered.
maxItems
- [?number
]
Sets the maximum number of items it is possible to enter.
- [readonly
?boolean
] [REMOVED]
Please add a readonly attribute to each tag model as a truthy value instead.
Example:
// TagModel
{
display: 'display',
value: 124242,
readonly: true
}
separatorKeyCodes
- [?number[]
]
Array of keyboard keys with which is possible to define the key for separating terms. By default, only Enter is the defined key.
separatorKeys
- [?string[]
]
Array of input characters with which is possible to define the key for separating terms. Default is empty. Can use with separatorKeyCodes
, either one method matched will trigger tag separation.
- [transform
?(item: string) => string
] [REMOVED]
Please use onAdding
instead. Just pass the value, transformed, to the Observable.
inputId
- [?string
]
Custom ID assigned to the input
inputClass
- [?string
]
Custom class assigned to the input
clearOnBlur
- [?boolean
]
If set to true, it will clear the form's text on blur events
hideForm
- [?boolean
]
If set to true, will remove the form from the component
onTextChangeDebounce
- [?number
]
Number of ms for debouncing the onTextChange
event (defaults to 250
)
addOnBlur
- [?boolean
]
If set to true
, will add an item when the form is blurred (defaults to false
)
addOnPaste
- [?boolean
]
If set to true
, will add items pasted into the form's input (defaults to false
)
pasteSplitPattern
- [?string | RegExp
]
Pattern used with the native method split() to separate patterns in the string pasted (defaults to ,
)
blinkIfDupe
- [?boolean
]
If a duplicate item gets added, this will blink - giving the user a visual cue of where it is located (defaults to true
)
removable
- [?boolean
]
If set to false
, it will not be possible to remove tags (defaults to true
)
editable
(experimental) - [?boolean
]
If set to true
, it will be possible to edit the display value of the tags (defaults to false
)
allowDupes
- [?boolean
]
If set to true
, it will be possible to add tags with the same value (defaults to false
)
modelAsStrings
- [?boolean
]
If set to true
, all values added will be strings, and not objects (defaults to false
)
trimTags
- [?boolean
]
If set to false
, the tags could contain leading and trailing spaces (defaults to true
)
inputText
- [?string
]
Property to bind text directly to the form's value. You can use it to change the text of the input at any time, or to just bind a value. Remember: use two-way data binding with this property.
ripple
- [?boolean
]
Specifies whether the ripple effect should be visible or not (defaults to true
)
disable
- [?boolean
]
If set to true
, the input will be disabled. Similar to readonly
but with a visual effect.
Notice*: this attribute was changed from 'disabled' to 'disable' in order to comply with Angular's compiler.
tabindex
- [?string
]
If set, passes the specified tabindex to the form's input.
dragZone
- [?string
]
If set, the input will be draggable. Also the input will be draggable to another form with the same dragZone value.
animationDuration
- [?{enter: string, leave: string}
]
This option overwrites the default timing values for the animation. If you don't like the animation at all, just set both values to '0ms'.
The default value is {enter: '250ms', leave: '150ms'}
validators
- [?ValidatorFn[]
]
An array of Validators (custom or Angular's) that will validate the tag before adding it to the list of items. It is possible to use multiple validators.
asyncValidators
- [?AsyncValidatorFn[]
]
An array of AsyncValidators that will validate the tag before adding it to the list of items. It is possible to use multiple async validators.
errorMessages
- [?Object{error: message}
]
An object whose key is the name of the error (ex. required) and the value is the message you want to display to your users
onAdding
- [?onAdding(tag: tagModel): Observable<TagModel>
]
Hook to intercept when an item is being added. Needs to return an Observable.
Example:
1 public onAdding(tag: TagModel): Observable<TagModel> { 2 const confirm = window.confirm('Do you really want to add this tag?'); 3 return Observable 4 .of(tag) 5 .filter(() => confirm); 6}
onRemoving
- [?onRemoving(tag: tagModel): Observable<TagModel>
]
Hook to intercept when an item is being removed. Needs to return an Observable. Example:
1public onRemoving(tag: TagModel): Observable<TagModel> { 2 const confirm = window.confirm('Do you really want to remove this tag?'); 3 return Observable 4 .of(tag) 5 .filter(() => confirm); 6 }
onlyFromAutocomplete
- [?boolean
]
If set to true
, it will be possible to add new items only from the autocomplete dropdown
identifyBy
- [?any
]
Any value you want your tag object to be defined by (defaults to value
)
displayBy
- [?string
]
The string displayed in a tag object (defaults to display
)
onAdd
- [?onAdd($event: string)
]
Event fired when an item has been added
onRemove
- [?onRemove($event: string)
]
Event fired when an item has been removed
onSelect
- [?onSelect($event: string)
]
Event fired when an item has been selected
onFocus
- [?onFocus($event: string)
]
Event fired when the input is focused - will return current input value
onBlur
- [?onBlur($event: string)
]
Event fired when the input is blurred - will return current input value
onTextChange
- [?onTextChange($event: string)
]
Event fired when the input value changes
onPaste
- [?onPaste($event: string)
]
Event fired when the text is pasted into the input (only if addOnPaste
is set to true
)
onValidationError
- [?onValidationError($event: string)
]
Event fired when the validation fails
onTagEdited
- [?onTagEdited($event: TagModel)
]
Event fired when a tag is edited
TagInputDropdownComponent is a proxy between ngx-chips
and ng2-material-dropdown
.
autocompleteObservable
- [(text: string) => Observable<Response>
]
A function that takes a string (current input value) and returns an Observable (ex. http.get()
) with an array of items wit the same structure as autocompleteItems
(see below). Make sure you retain the scope of your class or function when using this property.
It can be used to populate the autocomplete with items coming from an async request.
showDropdownIfEmpty
- [?boolean
]
If set to true
, the dropdown of the autocomplete will be shown as soon as the user focuses on the form
keepOpen
- [?boolean
]
To use in conjunction with showDropdownIfEmpty
. If set to false
, the dropdown will not reopen automatically after adding a new tag. (defaults to true
).
autocompleteItems
- [?string[] | AutoCompleteModel[]
]
An array of items to populate the autocomplete dropdown
offset
- [?string
]
Offset to adjust the position of the dropdown with absolute values (defaults to '50 0'
)
focusFirstElement
- [?boolean
]
If set to true
, the first item of the dropdown will be automatically focused (defaults to false
)
minimumTextLength
- [?number
]
Minimum text length in order to display the autocomplete dropdown (defaults to 1
)
limitItemsTo
- [?number
]
Number of items to display in the autocomplete dropdown
identifyBy
- [?string
]
Just like for tag-input
, this property defines the property of the value displayed of the object passed to the autocomplete
displayBy
- [?string
]
Just like for tag-input
, this property defines the property of the unique value of the object passed to the autocomplete
matchingFn
- [?matchingFn(value: string, target: TagModel): boolean
]
Use this property if you are not happy with the default matching and want to provide your own implementation. The first value is the value of the input text, the second value corresponds to the value of each autocomplete item passed to the component
appendToBody
- [?boolean
]
If set to false
, the dropdown will not be appended to the body, but will remain in its parent element. Useful when using the components inside popups or dropdowns. Defaults to true
.
dynamicUpdate
- [?boolean
]
If set to false
, the dropdown will not try to set the position according to its position in the page, but will be fixed. Defaults to true
.
zIndex
- [?number
]
Manually set the zIndex of the dropdown. Defaults to 100
.
The property autocompleteItems
can be an array of strings or objects. Interface for AutoCompleteModel
(just like TagModel)
is:
1interface AutoCompleteModel { 2 value: any; 3 display: string; 4}
The input text will be matched against both the properties.
More options to customise the dropdown will follow.
1@Component({ 2 selector: 'app', 3 template: `<tag-input [(ngModel)]='items'></tag-input>` 4}); 5export class App { 6 items = ['Pizza', 'Pasta', 'Parmesan']; 7}
1<tag-input [(ngModel)]='items'></tag-input>
1// itemsAsObjects = [{value: 0, display: 'Angular'}, {value: 1, display: 'React'}]; 2<tag-input [ngModel]="itemsAsObjects"></tag-input>
identifyBy
and displayBy
1// itemsAsObjects = [{id: 0, name: 'Angular'}, {id: 1, name: 'React'}]; 2<tag-input [ngModel]="itemsAsObjects" [identifyBy]="'id'" [displayBy]="'name'"></tag-input>
1<tag-input [(ngModel)]='items' [editable]='true' (onTagEdited)="onTagEdited($event)"></tag-input>
1<tag-input [(ngModel)]='items' [removable]='false'></tag-input>
1<tag-input [(ngModel)]='items' [maxItems]='5'></tag-input>
If the value of the model will contain more tags than maxItems
, maxItems
will be replaced with the current size of the model.
1<tag-input [ngModel]="['@item']"> 2 <tag-input-dropdown [autocompleteItems]="[{display: 'Item1', value: 0}, 'item2', 'item3']"> 3 </tag-input-dropdown> 4</tag-input>
This will accept items only from the autocomplete dropdown:
1<tag-input [ngModel]="['@item']" 2 [onlyFromAutocomplete]="true"> 3 <tag-input-dropdown [showDropdownIfEmpty]="true" 4 [autocompleteItems]="['iTem1', 'item2', 'item3']"> 5 </tag-input-dropdown> 6</tag-input>
1<tag-input [ngModel]="['@item']" 2 [onlyFromAutocomplete]="true"> 3 <tag-input-dropdown [showDropdownIfEmpty]="true" 4 [autocompleteItems]="['iTem1', 'item2', 'item3']"> 5 <ng-template let-item="item" let-index="index"> 6 {{ index }}: {{ item.display }} 7 </ng-template> 8 </tag-input-dropdown> 9</tag-input>
1public requestAutocompleteItems = (text: string): Observable<Response> => { 2 const url = `https://my.api.com/search?q=${text}`; 3 return this.http 4 .get(url) 5 .map(data => data.json()); 6};
1<tag-input [ngModel]="['@item']"> 2 <tag-input-dropdown [autocompleteObservable]='requestAutocompleteItems'></tag-input-dropdown> 3</tag-input>
If you want to use more keys to separate items, add them to separatorKeys as an array of keyboard key codes.
1<tag-input [(ngModel)]='items' [separatorKeyCodes]="[32]"></tag-input>
Create some validation methods in your component:
1class MyComponent { 2 private startsWithAt(control: FormControl) { 3 if (control.value.charAt(0) !== '@') { 4 return { 5 'startsWithAt@': true 6 }; 7 } 8 9 return null; 10 } 11 12 private endsWith$(control: FormControl) { 13 if (control.value.charAt(control.value.length - 1) !== '$') { 14 return { 15 'endsWith$': true 16 }; 17 } 18 19 return null; 20 } 21 22 public validators = [this.startsWithAt, this.endsWith$]; 23 24 public errorMessages = { 25 'startsWithAt@': 'Your items need to start with "@"', 26 'endsWith$': 'Your items need to end with "$"' 27 }; 28}
Pass them to the tag-input component:
1<tag-input [ngModel]="['@item']" 2 [errorMessages]="errorMessages" 3 [validators]="validators"> 4</tag-input>
Set up some methods that will run when its relative event is fired.
1<tag-input [(ngModel)]='items' 2 (onBlur)="onInputBlurred($event)" 3 (onFocus)="onInputFocused($event)" 4 (onSelect)="onSelected($event)" 5 (onRemove)="onItemRemoved($event)" 6 (onTextChange)="onTextChange($event)" 7 (onAdd)="onItemAdded($event)"> 8</tag-input>
#### Readonly
If readonly is passed to the tag-input, it won't be possible to select, add and remove items. [REMOVED]
1<tag-input [ngModel]="['Javascript', 'Typescript']" [readonly]="true"></tag-input>
Define your own template, but remember to set up the needed events using the input
reference.
1<tag-input [ngModel]="['@item']" [modelAsStrings]="true" #input> 2 <ng-template let-item="item" let-index="index"> <!-- DEFINE HERE YOUR TEMPLATE --> 3 <span> 4 <!-- YOU MAY ACTUALLY DISPLAY WHATEVER YOU WANT IF YOU PASS AN OBJECT AS ITEM --> 5 <!-- ex. item.myDisplayValue --> 6 7 item: {{ item }} 8 </span> 9 <delete-icon (click)="input.removeItem(item, index)"></delete-icon> 10 </ng-template> 11</tag-input>
If you use many instances of the component and want to set some values by default for all of them, import the module and use withDefaults
:
1import { TagInputModule } from 'ngx-chips'; 2 3TagInputModule.withDefaults({ 4 tagInput: { 5 placeholder: 'Add a new tag', 6 // add here other default values for tag-input 7 }, 8 dropdown: { 9 displayBy: 'my-display-value', 10 // add here other default values for tag-input-dropdown 11 } 12});
If you don't like how the default theme looks, or you just need it to fit in a different design, you can choose 4 new themes: bootstrap3-info
, bootstrap
, dark
and minimal
.
1<tag-input [(ngModel)]='items' theme='bootstrap3-info'></tag-input> 2<tag-input [(ngModel)]='items' theme='bootstrap'></tag-input> 3<tag-input [(ngModel)]='items' theme='minimal'></tag-input> 4<tag-input [(ngModel)]='items' theme='dark'></tag-input>
If you do not like these themes, define your own theme.
Yes.
Yes.
Yes.
This component is supposed to work with the latest Angular versions.
If you have any issues, please do make sure you're not running a different version (or check this repo's package.json). Otherwise, please do open a new issue.
Yes - check out how to create custom themes.
Please do open a new issue, but please check first that the same issue has not already been raised and that you are using the latest version :)
Please do not send private emails - Github Issues are supposed to help whoever might have your same issue, so it is the right place to help each other.
Issues not filled out with the provided templates are going to be closed. Please provide as much information as possible: do include a plunkr so that I can see what the problem is without having to replicate your environment on my laptop. The time I can spend on this is very limited.
No features requests will be considered, unless they are Pull Requests. I feel the component is already quite bloated, and I'd like on solving bugs and making this more reliable for everyone.
Contributions are highly welcome! No, there is no guideline on how to do it. Just make sure to lint and unit test your changes. We'll figure out the rest with a couple of messages...
Do please read this great post by Micheal Bromley: http://www.michaelbromley.co.uk/blog/529/why-i-havent-fixed-your-issue-yet. No, I don't have babies, but am not 24/7 coding :)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 5/11 approved changesets -- score normalized to 4
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
70 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More