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

Table: Editing row with calendar component causes it to terminate editing #15555

Closed
Closed
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
127 changes: 47 additions & 80 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"primeflex": "^3.3.1",
"primeicons": "^7.0.0",
"prismjs": "^1.29.0",
"quill": "1.3.7",
"quill": "2.0.2",
"rxjs": "~7.8.1",
"ts-node": "~10.9.1",
"tslib": "^2.5.0",
Expand Down
112 changes: 109 additions & 3 deletions src/app/components/table/table.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { RouterTestingModule } from '@angular/router/testing';
import { SharedModule } from 'primeng/api';
import { ContextMenu, ContextMenuModule } from 'primeng/contextmenu';
import { CalendarModule } from 'primeng/calendar';
import { DropdownModule } from 'primeng/dropdown';
import { EditableColumn, Table, TableModule } from './table';
import { CellEditor, EditableColumn, EditableRow, Table, TableModule } from './table';
import type { Paginator } from '../paginator/paginator';

@Component({
Expand Down Expand Up @@ -324,6 +325,58 @@ import type { Paginator } from '../paginator/paginator';
</tr>
</ng-template>
</p-table>

<p-table class="editableRowTable" [value]="carsEditRow" editMode="row" dataKey="vin">
<ng-template pTemplate="caption"> List of Cars </ng-template>
<ng-template pTemplate="header">
<tr>
<th>Brand</th>
<th>Color</th>
<th>Date Bought</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-editing="editing" let-ri="rowIndex" let-car>
<tr pEditableRow>
<td>
<p-cellEditor id="brandinput">
<ng-template pTemplate="input">
<input pInputText type="text" [(ngModel)]="car.brand" name="brand" required />
</ng-template>
<ng-template pTemplate="output">
{{ car.brand }}
</ng-template>
</p-cellEditor>
</td>
<td>
<p-cellEditor id="colorInput">
<ng-template pTemplate="input">
<input pInputText type="text" [(ngModel)]="car.color" name="color" required />
</ng-template>
<ng-template pTemplate="output">
{{ car.color }}
</ng-template>
</p-cellEditor>
</td>
<td>
<p-cellEditor id="calendar_input">
<ng-template pTemplate="input">
<p-calendar [(ngModel)]="car.datebought" appendTo="body" name="datebought"> </p-calendar>
</ng-template>
<ng-template pTemplate="output">
{{ car.datebought | date : 'dd/MM/yyyy' }}
</ng-template>
</p-cellEditor>
</td>
<td>
<div>
<button pButton type="button" pInitEditableRow icon="pi pi-pencil" (click)="onRowEditInit(car)" class="p-button-rounded p-button-text"></button>
<button pButton type="button" pSaveEditableRow icon="pi pi-check" (click)="onRowEditSave(car)" class="p-button-rounded p-button-text p-button-success mr-2"></button>
<button pButton type="button" pCancelEditableRow icon="pi pi-times" (click)="onRowEditCancel(car, ri)" class="p-button-rounded p-button-text p-button-danger"></button>
</div>
</td>
</tr>
</ng-template>
</p-table>
`
})
class TestBasicTableComponent {
Expand Down Expand Up @@ -367,6 +420,7 @@ class TestBasicTableComponent {
{ brand: 'Ford', year: 2000, color: 'Black', vin: 'h54hw5' },
{ brand: 'Fiat', year: 2013, color: 'Red', vin: '245t2s' }
];
carsEditRow = [{ brand: 'Renault', year: 2005, color: 'Gray', vin: 3, datebought: '2021-01-01' }];

customSort(event) {
event.data.sort((data1, data2) => {
Expand All @@ -383,6 +437,21 @@ class TestBasicTableComponent {
return event.order * result;
});
}

clonedProducts: { [s: string]: any } = {};

onRowEditInit(car) {
this.clonedProducts[car.vin] = { ...car };
}

onRowEditSave(car) {
delete this.clonedProducts[car.vin];
}

onRowEditCancel(car, index) {
this.cars[index] = this.clonedProducts[car.vin];
delete this.clonedProducts[car.vin];
}
}
describe('Table', () => {
let table: Table;
Expand All @@ -397,13 +466,14 @@ describe('Table', () => {
let reorderableTable: Table;
let contextMenuTable: Table;
let stateTable: Table;
let editableRowTable: Table;
let testComponent: TestBasicTableComponent;
let fixture: ComponentFixture<TestBasicTableComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [NoopAnimationsModule, FormsModule, SharedModule, DropdownModule, ContextMenuModule, TableModule, RouterTestingModule.withRoutes([{ path: 'test', component: ContextMenu }])],
declarations: [TestBasicTableComponent, EditableColumn]
imports: [NoopAnimationsModule, FormsModule, SharedModule, DropdownModule, ContextMenuModule, CalendarModule, TableModule, RouterTestingModule.withRoutes([{ path: 'test', component: ContextMenu }])],
declarations: [TestBasicTableComponent, EditableColumn, CellEditor, EditableRow]
});

fixture = TestBed.createComponent(TestBasicTableComponent);
Expand All @@ -420,6 +490,7 @@ describe('Table', () => {
reorderableTable = fixture.debugElement.children[9].componentInstance;
contextMenuTable = fixture.debugElement.children[10].componentInstance;
stateTable = fixture.debugElement.children[12].componentInstance;
editableRowTable = fixture.debugElement.children[13].componentInstance;
});

it('should display by default', () => {
Expand Down Expand Up @@ -1618,4 +1689,39 @@ describe('Table', () => {
state = JSON.parse(stateTable.getStorage().getItem(stateTable.stateKey));
expect(state).toBeNull();
});

it('should not end editing for a row if data changes uses p-calendar', () => {
fixture.detectChanges();
let editableRowTableEl = fixture.debugElement.query(By.css('.editableRowTable'));
const rowEls = editableRowTableEl.queryAll(By.css('tbody tr'));
const numberOfRows = rowEls.length;

expect(numberOfRows).toEqual(1);
expect(editableRowTable.isRowBeingEdited).toBeFalsy();

const actionButtons = editableRowTableEl.queryAll(By.css('button'));
expect(actionButtons.length).toEqual(3);
const editButton = actionButtons[0];
editButton.nativeElement.click();
fixture.detectChanges();

// Check if editing is true
expect(editableRowTable.isRowBeingEdited).toBeTruthy();

const calendarEl = rowEls[0].query(By.css('p-calendar'));
const calendarInputEl = calendarEl.query(By.css('input'));
calendarInputEl.nativeElement.value = '2021-01-03';
calendarInputEl.nativeElement.dispatchEvent(new Event('input'));
fixture.detectChanges();

expect(editableRowTable.isRowBeingEdited).toBeTruthy();
const saveButton = actionButtons[1];
saveButton.nativeElement.click();
fixture.detectChanges();

// Check if editing is false
const rowData = editableRowTable.value[0];
expect(rowData.datebought).toEqual('2021-01-03');
expect(editableRowTable.isRowBeingEdited).toBeFalsy();
});
});
Loading
Loading