Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add required to chip group #734 #743

Merged
merged 19 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
bdc01f8
feat: add required param to chip-group and test #734
iurynogueira Jul 7, 2023
f916210
feat: add required to chip-group #734
MadalenaCampos Jul 11, 2023
b23d8de
feat: add required to chip-group #734
MadalenaCampos Jul 11, 2023
fce5878
Merge branch 'main' into 734-add-required-to-chip-group
MadalenaCampos Jul 11, 2023
07edbb4
feat: add required to chip-group #734
MadalenaCampos Jul 12, 2023
eb7ea5f
fix: testing adjustments and validating options in the dropdown #734
MadalenaCampos Jul 12, 2023
a4f1563
fix: function adjustments #734
MadalenaCampos Jul 12, 2023
10c97f0
Merge branch 'main' into 734-add-required-to-chip-group
AlyssonMascarenhas Jul 12, 2023
f956a26
fix: function adjustments #734
MadalenaCampos Jul 12, 2023
ae8b75c
Merge branch '734-add-required-to-chip-group' of https://github.com/B…
MadalenaCampos Jul 12, 2023
c9a60a3
fix: function adjustments #734
MadalenaCampos Jul 12, 2023
5de86f8
fix: function adjustments #734
MadalenaCampos Jul 12, 2023
afb04ec
Merge branch 'main' into 734-add-required-to-chip-group
iurynogueira Jul 20, 2023
0ea5993
Merge branch 'main' into 734-add-required-to-chip-group
iurynogueira Jul 24, 2023
d77c27a
Merge branch 'main' into 734-add-required-to-chip-group
iurynogueira Aug 17, 2023
f2a328c
Merge branch 'main' into 734-add-required-to-chip-group
iurynogueira Sep 1, 2023
985be33
Merge branch 'main' into 734-add-required-to-chip-group
MadalenaCampos Sep 19, 2023
5d09387
Merge branch 'main' into 734-add-required-to-chip-group
iurynogueira Sep 19, 2023
8353102
fix: suggestions from pr
MadalenaCampos Sep 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions projects/ion/src/lib/chip-group/chip-group.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div data-testid="ion-chip-group" class="chip-group">
<ion-chip
*ngFor="let chip of chips; index as i"
[chipGroupRequired]="required"
[label]="chip.label"
[disabled]="chip.disabled || disabled"
[selected]="chip.selected"
Expand All @@ -14,5 +15,7 @@
[rightBadge]="chip.rightBadge || rightBadge"
(click)="selectChip(chip)"
(dropdownEvents)="dropdownEvents($event)"
[attr.data-testid]="'chip-group-' + chip.label"
[chipGroup]="true"
></ion-chip>
</div>
19 changes: 19 additions & 0 deletions projects/ion/src/lib/chip-group/chip-group.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@ describe('ChipGroupComponent', () => {
const element = screen.getByTestId('ion-chip');
expect(element).toHaveAttribute('disabled');
});

it('should render chip selected and not unselect when group is required', async () => {
const chips = [
{ label: 'Option 1', selected: true },
{ label: 'Option 2', selected: false },
];

await sut({
chips,
required: true,
selected: {
emit: selectEvent,
} as SafeAny,
});
const optionElement = screen.getByTestId(`chip-group-${chips[0].label}`);
expect(optionElement).toBeInTheDocument();
MadalenaCampos marked this conversation as resolved.
Show resolved Hide resolved
fireEvent.click(optionElement);
expect(optionElement).toHaveAttribute('ng-reflect-selected', 'true');
});
});

describe('With Dropdown', () => {
Expand Down
34 changes: 30 additions & 4 deletions projects/ion/src/lib/chip-group/chip-group.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,55 @@ import { DropdownItem } from '../core/types/dropdown';
styleUrls: ['./chip-group.component.scss'],
})
export class IonChipGroupComponent {
@Input() chips: ChipInGroup[];
@Input() chips: ChipInGroup[] = [];
@Input() size?: ChipSize = 'sm';
@Input() infoBadge?: IonChipProps['infoBadge'];
@Input() iconPosition?: IconDirection = 'left';
@Input() rightBadge?: RightBadge;
@Input() disabled = false;
@Input() multiple = false;
@Input() required = false;

@Output() selected? = new EventEmitter<ChipInGroup>();
@Output() dropdown? = new EventEmitter<DropdownItem[]>();

selectChip(chipSelected: ChipInGroup): void {
if (chipSelected.multiple && chipSelected.selected) {
const isChipSelectedOrMultiple =
chipSelected.multiple && chipSelected.selected;
if (isChipSelectedOrMultiple) {
return;
}

chipSelected = this.setChip(chipSelected);

this.selected.emit(chipSelected);
}

setChip(chipSelected: ChipInGroup): ChipInGroup {
const isChipSelected = chipSelected.selected;

if (!this.multiple) {
this.clearChips();
}
if (!chipSelected.multiple || !isChipSelected) {

const isSingleOrSelectedChip = !chipSelected.multiple || !isChipSelected;

if (isSingleOrSelectedChip) {
chipSelected.selected = !isChipSelected;
}
this.selected.emit(chipSelected);

if (this.required) {
this.checkRequired(chipSelected);
}

return chipSelected;
}

checkRequired(chipSelected: ChipInGroup): void {
const selectedChips = this.chips.filter((chip) => chip.selected);
if (!selectedChips.length) {
chipSelected.selected = true;
}
}

dropdownEvents(options: DropdownItem[]): void {
Expand Down
10 changes: 9 additions & 1 deletion projects/ion/src/lib/chip/chip.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ export class ChipComponent implements OnInit, AfterViewInit, DoCheck {
@Input() showToggle = false;
@Input() required = false;

@Input() chipGroup = false;
@Input() chipGroupRequired = false;

@Output() events = new EventEmitter<ChipEvent>();
@Output() dropdownEvents = new EventEmitter<DropdownItem[]>();
@Output() dropdownSearchEvents = new EventEmitter<string>();
Expand All @@ -88,9 +91,14 @@ export class ChipComponent implements OnInit, AfterViewInit, DoCheck {

select(): void {
this.toggleDropdown();
if (!this.options) {

if (
(!this.options && this.chipGroup && !this.chipGroupRequired) ||
!this.chipGroup
) {
this.selected = !this.selected;
}

this.events.emit({
selected: this.selected,
disabled: this.disabled,
Expand Down
1 change: 1 addition & 0 deletions projects/ion/src/lib/core/types/chip-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ChipGroupProps {
disabled?: boolean;
size?: ChipSize;
multiple?: boolean;
required?: boolean;
events?: EventEmitter<ChipEvent>;
selected?: EventEmitter<ChipInGroup>;
}
Expand Down
16 changes: 16 additions & 0 deletions stories/ChipGroup.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ SelectedByDefault.args = {
],
};

export const RequiredSelected = Template.bind({});
RequiredSelected.args = {
chips: [
{
label: 'Chip 1',
selected: true,
},
{
label: 'Chip 2',
selected: false,
},
],
required: true,
multiple: false,
};

export const Multiple = Template.bind({});
Multiple.args = {
chips,
Expand Down
Loading