From cad5eee3b4b8d3367b1f1e82bd271513d96e3cfa Mon Sep 17 00:00:00 2001 From: vmuresanu Date: Thu, 9 Nov 2023 15:46:17 +0200 Subject: [PATCH] fix: p-chips do not work with reactive forms --- src/app/components/chips/chips.ts | 14 ++---- src/app/showcase/doc/chips/chipsdoc.module.ts | 3 +- src/app/showcase/doc/chips/maxvaluesdoc.ts | 46 +++++++++++++++++++ src/app/showcase/pages/chips/chipsdemo.ts | 6 +++ 4 files changed, 59 insertions(+), 10 deletions(-) create mode 100644 src/app/showcase/doc/chips/maxvaluesdoc.ts diff --git a/src/app/components/chips/chips.ts b/src/app/components/chips/chips.ts index 918ef706cd2..9f7756f972c 100755 --- a/src/app/components/chips/chips.ts +++ b/src/app/components/chips/chips.ts @@ -84,7 +84,7 @@ export const CHIPS_VALUE_ACCESSOR: any = { (paste)="onPaste($event)" (focus)="onInputFocus($event)" (blur)="onInputBlur($event)" - [disabled]="disabled || maxedOut" + [disabled]="disabled || isMaxedOut" [ngStyle]="inputStyle" [class]="inputStyleClass" /> @@ -263,7 +263,7 @@ export class Chips implements AfterContentInit, ControlValueAccessor { return this.focusedIndex !== null ? `${this.id}_chips_item_${this.focusedIndex}` : null; } - private get isValueMaxLimited(): boolean { + get isMaxedOut(): boolean { return this.max && this.value && this.max === this.value.length; } @@ -459,7 +459,7 @@ export class Chips implements AfterContentInit, ControlValueAccessor { this.value = this.value || []; if (item && item.trim().length) { - if ((this.allowDuplicate || this.value.indexOf(item) === -1) && !this.isValueMaxLimited) { + if ((this.allowDuplicate || this.value.indexOf(item) === -1) && !this.isMaxedOut) { this.value = [...this.value, item]; this.onModelChange(this.value); this.onAdd.emit({ @@ -500,7 +500,7 @@ export class Chips implements AfterContentInit, ControlValueAccessor { break; case 'Enter': - if (inputValue && inputValue.trim().length && !this.maxedOut()) { + if (inputValue && inputValue.trim().length && !this.isMaxedOut) { this.addItem(event, inputValue, true); } @@ -530,7 +530,7 @@ export class Chips implements AfterContentInit, ControlValueAccessor { updateMaxedOut(): void { if (this.inputViewChild && this.inputViewChild.nativeElement) { - if (this.isValueMaxLimited) { + if (this.isMaxedOut) { // Calling `blur` is necessary because firefox does not call `onfocus` events // for disabled inputs, unlike chromium browsers. this.inputViewChild.nativeElement.blur(); @@ -544,10 +544,6 @@ export class Chips implements AfterContentInit, ControlValueAccessor { } } } - - maxedOut(): boolean { - return this.max && this.value && this.max === this.value.length; - } } @NgModule({ diff --git a/src/app/showcase/doc/chips/chipsdoc.module.ts b/src/app/showcase/doc/chips/chipsdoc.module.ts index dfd1557b3ff..2ca96a2bd9d 100644 --- a/src/app/showcase/doc/chips/chipsdoc.module.ts +++ b/src/app/showcase/doc/chips/chipsdoc.module.ts @@ -12,10 +12,11 @@ import { ReactiveFormsDoc } from './reactiveformsdoc'; import { RegexpSeperatorDoc } from './regexpseperator.doc'; import { StyleDoc } from './styledoc'; import { TemplateDoc } from './templatedoc'; +import { MaxValuesDoc } from './maxvaluesdoc'; @NgModule({ imports: [CommonModule, ChipsModule, FormsModule, ReactiveFormsModule, AppCodeModule, AppDocModule], exports: [AppDocModule], - declarations: [ImportDoc, BasicDoc, CommaSeperatorDoc, RegexpSeperatorDoc, TemplateDoc, StyleDoc, AccessibilityDoc, ReactiveFormsDoc] + declarations: [ImportDoc, BasicDoc, CommaSeperatorDoc, RegexpSeperatorDoc, TemplateDoc, StyleDoc, AccessibilityDoc, ReactiveFormsDoc, MaxValuesDoc] }) export class ChipsDocModule {} diff --git a/src/app/showcase/doc/chips/maxvaluesdoc.ts b/src/app/showcase/doc/chips/maxvaluesdoc.ts new file mode 100644 index 00000000000..8718e008bbd --- /dev/null +++ b/src/app/showcase/doc/chips/maxvaluesdoc.ts @@ -0,0 +1,46 @@ +import { Component, Input } from '@angular/core'; +import { FormControl } from '@angular/forms'; +import { Code } from '../../domain/code'; + +@Component({ + selector: 'reactive-forms-doc', + template: `
+ +

Chips can have a maximum number of entered items. In order to set the limit max property is used and accepts a numeric value which reflects the maximum number of items in the chip list

+
+
+ +
+ +
` +}) +export class MaxValuesDoc { + @Input() id: string; + + @Input() title: string; + + values = new FormControl(null); + + code: Code = { + basic: ``, + + html: ` +
+ +
`, + + typescript: ` +import { Component, OnInit } from '@angular/core'; +import { FormControl, FormGroup } from '@angular/forms'; + +@Component({ + selector: 'chips-max-values-demo', + templateUrl: './chips-max-values-demo.html', + styleUrls: ['./chips-max-values-demo.scss'] +}) +export class ChipsMaxValuesDemo { + values = new FormControl(null); + max = 2; +}` + }; +} diff --git a/src/app/showcase/pages/chips/chipsdemo.ts b/src/app/showcase/pages/chips/chipsdemo.ts index 399c9bed4db..dbdb33382f8 100755 --- a/src/app/showcase/pages/chips/chipsdemo.ts +++ b/src/app/showcase/pages/chips/chipsdemo.ts @@ -7,6 +7,7 @@ import { TemplateDoc } from '../../doc/chips/templatedoc'; import { StyleDoc } from '../../doc/chips/styledoc'; import { AccessibilityDoc } from '../../doc/chips/accessibilitydoc'; import { ReactiveFormsDoc } from '../../doc/chips/reactiveformsdoc'; +import { MaxValuesDoc } from '../../doc/chips/maxvaluesdoc'; @Component({ templateUrl: './chipsdemo.html' @@ -28,6 +29,11 @@ export class ChipsDemo { label: 'Reactive Forms', component: ReactiveFormsDoc }, + { + id: 'max-values', + label: 'Max Values', + component: MaxValuesDoc + }, { id: 'commaseperator', label: 'Comma Seperator',