PrimeNG Chips Multiple Separator while Pasting Multiple Values
September 20, 2024 · 2 min readLucian CiobanuPrimeNG offers the possibility to update it's chips component with different separator other than enter. The property accepts a string or a RegExp as such.
chips.component.html
<p-chips
formControlName="countries"
separator=","
></p-chips>
<p-chips
formControlName="capitals"
separator=" "
></p-chips>
The first p-chip allows for the usual "enter" key to mark as separator but also the "," character while the second allows also for space to be a separator but not for both. If you would want to enable users to have all of enter, space and "," as separators while still allowing them to paste long strings like "Germany, England, France", a regexp is needed as such:
chips.component.ts
//this matches spaces and commas but any regex can be used here
const spaceOrCommaRegex: Regex = /[\s,]/g;
chips.component.html
<p-chips
formControlName="countries"
[separator]="spaceOrCommaRegex"
></p-chips>
<p-chips
formControlName="capitals"
[separator]="spaceOrCommaRegex"
></p-chips>
Notice in the HTML file we are using [ ] around separator so the regexp can be passed.
This will allow the user to create a new chip when hitting enter, space, comma and will turn pasting strings like "Germany, England, France" with multiple values into the same number of beautiful PrimeNG chips.
Hope this helped!
Cheers,
Lucian

Web Development & Software Delivery