From f91621051bb6ec95dfe490e40167d8aa8c7d5d66 Mon Sep 17 00:00:00 2001 From: Madalena Campos Date: Tue, 11 Jul 2023 17:09:31 -0300 Subject: [PATCH] feat: add required to chip-group #734 --- .../lib/chip-group/chip-group.component.html | 1 + .../src/lib/chip-group/chip-group.component.ts | 18 +++++++++++------- projects/ion/src/lib/chip/chip.component.ts | 2 +- stories/ChipGroup.stories.ts | 6 ++++++ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/projects/ion/src/lib/chip-group/chip-group.component.html b/projects/ion/src/lib/chip-group/chip-group.component.html index f6db7e5ab..22e51e6b0 100644 --- a/projects/ion/src/lib/chip-group/chip-group.component.html +++ b/projects/ion/src/lib/chip-group/chip-group.component.html @@ -3,6 +3,7 @@ *ngFor="let chip of chips; index as i" [label]="chip.label" [disabled]="chip.disabled || disabled" + [required]="chip.required || required" [selected]="chip.selected" [showDropdown]="chip.selected && chip.options" [size]="chip.size || size" diff --git a/projects/ion/src/lib/chip-group/chip-group.component.ts b/projects/ion/src/lib/chip-group/chip-group.component.ts index 996168ad6..7cbba4db3 100644 --- a/projects/ion/src/lib/chip-group/chip-group.component.ts +++ b/projects/ion/src/lib/chip-group/chip-group.component.ts @@ -38,17 +38,21 @@ export class IonChipGroupComponent { chipSelected.selected = !isChipSelected; } - // const chipsSelecteds = this.chips.filter((chip) => chip.selected); - // if (this.required && !this.multiple) { - // chipSelected.selected = true; - // chipSelected.label = 'teste'; - // this.selected.emit(chipSelected); - // return; - // } + if (this.required) { + this.checkRequired(chipSelected); + return; + } this.selected.emit(chipSelected); } + checkRequired(chipSelected: ChipInGroup) { + const selectedChips = this.chips.filter((chip) => chip.selected); + if (!selectedChips.length) { + chipSelected.selected = true; + } + } + dropdownEvents(options: DropdownItem[]): void { if (options) { this.dropdown.emit(options); diff --git a/projects/ion/src/lib/chip/chip.component.ts b/projects/ion/src/lib/chip/chip.component.ts index 7f39158b7..4fc8bdb81 100644 --- a/projects/ion/src/lib/chip/chip.component.ts +++ b/projects/ion/src/lib/chip/chip.component.ts @@ -89,7 +89,7 @@ export class ChipComponent implements OnInit, AfterViewInit, DoCheck { select(): void { this.toggleDropdown(); if (!this.options) { - this.selected = !this.selected; + !this.selected; } this.events.emit({ selected: this.selected, diff --git a/stories/ChipGroup.stories.ts b/stories/ChipGroup.stories.ts index d41dc1d96..d6ad4914c 100644 --- a/stories/ChipGroup.stories.ts +++ b/stories/ChipGroup.stories.ts @@ -129,3 +129,9 @@ export const WithDropdown = Template.bind({}); WithDropdown.args = { chips: chipsWithOptions, }; + +export const WithRequired = Template.bind({}); +WithRequired.args = { + chips, + required: true, +};