Skip to content

fix(lookup): corrige seleção de registros no modo múltiplo #2414

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

Merged
merged 1 commit into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -723,8 +723,8 @@ describe('PoLookupModalBaseComponent:', () => {
expect(component.selecteds).toEqual(expectSelecteds);
});

it('setDisclaimersItems: should set selecteds with [{ value: component.selectedItems }] if selectedItems isnt array', () => {
const expectSelecteds = [{ value: 123456789 }];
it('setDisclaimersItems: should set selecteds with [component.selectedItems] if selectedItems isnt array', () => {
const expectSelecteds = [123456789];
component.multiple = true;
component.selectedItems = 123456789;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,12 @@ export abstract class PoLookupModalBaseComponent implements OnDestroy, OnInit {

//Método responsável por criar os disclaimers quando abre o modal.
setDisclaimersItems() {
if (this.selectedItems && !Array.isArray(this.selectedItems)) {
this.multiple ? (this.selecteds = [{ value: this.selectedItems }]) : (this.selecteds = [this.selectedItems]);
return;
}

if (this.selecteds.length === 0 && this.selectedItems && this.selectedItems.length) {
if (this.selectedItems && Array.isArray(this.selectedItems) && this.selectedItems.length > 0) {
this.selecteds = [...this.selectedItems];
} else if (this.selectedItems && !Array.isArray(this.selectedItems)) {
this.selecteds = [this.selectedItems];
} else {
this.selecteds = [];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,14 +210,15 @@ describe('PoLookupModalComponent', () => {

it('onSelect: should concat table item in selecteds', () => {
component.multiple = true;
component.selecteds = [{ value: 'Doe', label: 'Jane' }];
component.selectedItems = [{ value: 'Doe', label: 'Jane' }];
component.selecteds = [...component.selectedItems];

component.fieldLabel = 'name';
component.fieldValue = 'value';
const item = {
name: 'John',
value: 'Lenon'
value: 'Lenon',
label: 'John'
};

component.onSelect(item);

expect(component.selecteds).toEqual([
Expand All @@ -228,17 +229,32 @@ describe('PoLookupModalComponent', () => {

it('onSelect: should override table item in selecteds', () => {
component.multiple = false;
component.selecteds = [{ value: 'Doe', label: 'Jane' }];

component.fieldLabel = 'name';
component.fieldValue = 'value';
component.selectedItems = [{ value: 'Doe', label: 'Jane' }];
component.selecteds = [...component.selectedItems];

const item = {
name: 'John',
value: 'Lenon'
value: 'Lenon',
label: 'John'
};

component.onSelect(item);

expect(component.selecteds).toEqual([{ value: 'Lenon', label: 'John', name: 'John' }]);
expect(component.selecteds).toEqual([{ name: 'John', value: 'Lenon', label: 'John' }]);
});

it('onSelect: should initialize selectedItems with [selectedItem] when selectedItems is null or undefined', () => {
component.multiple = true;
component.selectedItems = null;

const selectedItem = { label: 'John', value: 1 };

component.onSelect(selectedItem);

expect(component.selectedItems).toEqual([selectedItem]);

expect(component.selecteds).toEqual([selectedItem]);
});

it('onAllUnselected: should be called and clean all items on table', () => {
Expand All @@ -251,6 +267,13 @@ describe('PoLookupModalComponent', () => {
});

it('onUnselectFromDisclaimer: should be called and remove disclaimer', () => {
component.selectedItems = [
{ label: 'John', value: 1 },
{ label: 'Paul', value: 2 }
];

component.selecteds = [...component.selectedItems];

spyOn(component['poTable'], 'unselectRowItem').and.callThrough();

component.fieldValue = 'value';
Expand All @@ -263,24 +286,63 @@ describe('PoLookupModalComponent', () => {
expect(component['poTable'].unselectRowItem).toHaveBeenCalled();
});

it('onUnselect: should be called and unselect item', () => {
it('onUnselectFromDisclaimer: should be called and remove disclaimer, setting selecteds to an empty array when selectedItems becomes empty', () => {
component.selectedItems = [{ label: 'John', value: 1 }];
component.selecteds = [...component.selectedItems];

spyOn(component['poTable'], 'unselectRowItem').and.callThrough();

component.fieldValue = 'value';
component.selecteds = [
fixture.detectChanges();

const removedDisclaimer = { label: 'John', value: 1 };

component.onUnselectFromDisclaimer(removedDisclaimer);

expect(component.selectedItems).toEqual([]);
expect(component.selecteds).toEqual([]);
expect(component['poTable'].unselectRowItem).toHaveBeenCalled();
});

it('onUnselect: should be called and unselect item', () => {
component.multiple = true;
component.selectedItems = [
{ label: 'John', value: 1 },
{ label: 'Paul', value: 2 },
{ label: 'George', value: 3 },
{ label: 'Ringo', value: 4 }
];
component.fieldValue = 'value';

const unselectedItem = { label: 'Paul', value: 2 };
const expected = [
component.onUnselect(unselectedItem);

expect(component.selectedItems.length).toBe(3);
expect(component.selectedItems).toEqual([
{ label: 'John', value: 1 },
{ label: 'George', value: 3 },
{ label: 'Ringo', value: 4 }
]);
expect(component.selecteds).toEqual(component.selectedItems);
});

it('onUnselect: should set selectedItems to an empty array when multiple is false', () => {
component.multiple = false;

component.selectedItems = [
{ label: 'John', value: 1 },
{ label: 'Paul', value: 2 }
];

component.selecteds = [...component.selectedItems];

const unselectedItem = { label: 'John', value: 1 };

component.onUnselect(unselectedItem);
expect(component.selecteds).toEqual(expected);

expect(component.selectedItems).toEqual([]);

expect(component.selecteds).toEqual([]);
});

it('onAllSelected: should be called and select all visible itens', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,49 @@ export class PoLookupModalComponent extends PoLookupModalBaseComponent implement

// Seleciona um item na tabela
onSelect(item) {
const formattedItem = {
value: item[this.fieldValue],
label: item[this.fieldLabel],
...item
};
if (this.multiple) {
this.selecteds = [...this.selecteds, { value: item[this.fieldValue], label: item[this.fieldLabel], ...item }];
this.selectedItems = this.selectedItems ? [...this.selectedItems, formattedItem] : [formattedItem];
} else {
this.selecteds = [{ value: item[this.fieldValue], label: item[this.fieldLabel], ...item }];
this.selectedItems = [formattedItem];
}
this.selecteds = [...this.selectedItems];
}

// Remove a seleção de um item na tabela
onUnselect(unselectedItem) {
this.selecteds = this.selecteds.filter(itemSelected => itemSelected.value !== unselectedItem[this.fieldValue]);
if (this.multiple) {
this.selectedItems = this.selectedItems.filter(item => item.value !== unselectedItem[this.fieldValue]);
} else {
this.selectedItems = [];
}
this.selecteds = [...this.selectedItems];
}

onUnselectFromDisclaimer(removedDisclaimer) {
this.selectedItems = this.selectedItems.filter(item => item.value !== removedDisclaimer.value);
if (this.selectedItems.length === 0) {
this.selecteds = [];
} else {
this.selecteds = [...this.selectedItems];
}
this.poTable.unselectRowItem(item => item[this.fieldValue] === removedDisclaimer.value);
}

// Seleciona todos os itens visíveis na tabela
onAllSelected(items) {
this.selecteds = items.map(item => ({ value: item[this.fieldValue], label: item[this.fieldLabel], ...item }));
this.selectedItems = items.map(item => ({ value: item[this.fieldValue], label: item[this.fieldLabel], ...item }));
this.selecteds = [...this.selectedItems];
}

// Remove a seleção de todos os itens visíveis na tabela
onAllUnselected(items) {
this.poTable.unselectRows();
this.selectedItems = [];
this.selecteds = [];
}

Expand Down