Skip to content

Commit

Permalink
fix(core/forms): show labels for select fields when readonly
Browse files Browse the repository at this point in the history
approve api changes in core/forms
  • Loading branch information
sara-re authored and trik committed Nov 27, 2020
1 parent a3ee25f commit ddac4e6
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 11 deletions.
9 changes: 9 additions & 0 deletions src/core/forms/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ng_module(
":read-only-file-field.css",
":read-only-image-field.css",
":read-only-video-url-field.css",
":read-only-select-field.css",
":read-only-table-field.css",
] + glob(["**/*.html"]),
module_name = "@ajf/core/forms",
Expand All @@ -35,6 +36,7 @@ ng_module(
"@npm//@angular/core",
"@npm//@angular/forms",
"@npm//@angular/platform-browser",
"@npm//@ngx-translate/core",
"@npm//@types/esprima",
"@npm//date-fns",
],
Expand Down Expand Up @@ -76,6 +78,12 @@ sass_binary(
deps = [],
)

sass_binary(
name = "read_only_select_field_scss",
src = "read-only-select-field.scss",
deps = [],
)

sass_binary(
name = "read_only_table_field_scss",
src = "read-only-table-field.scss",
Expand All @@ -98,6 +106,7 @@ ng_test_library(
":forms",
"//src/core/models",
"@npm//@angular/platform-browser",
"@npm//@ngx-translate/core",
"@npm//rxjs",
],
)
Expand Down
21 changes: 16 additions & 5 deletions src/core/forms/forms-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {CommonModule} from '@angular/common';
import {HttpClientModule} from '@angular/common/http';
import {NgModule} from '@angular/core';
import {ReactiveFormsModule} from '@angular/forms';
import {TranslateModule} from '@ngx-translate/core';

import {AjfAsFieldInstancePipe} from './as-field-instance';
import {AjfAsRepeatingSlideInstancePipe} from './as-repeating-slide-instance';
Expand All @@ -48,6 +49,7 @@ import {AjfRangePipe} from './range';
import {AjfReadOnlyFieldComponent} from './read-only-field';
import {AjfReadOnlyFileFieldComponent} from './read-only-file-field';
import {AjfReadOnlyImageFieldComponent} from './read-only-image-field';
import {AjfReadOnlySelectFieldComponent} from './read-only-select-field';
import {AjfReadOnlyTableFieldComponent} from './read-only-table-field';
import {AjfReadOnlyVideoUrlFieldComponent} from './read-only-video-url-field';
import {AjfTableRowClass} from './table-row-class';
Expand Down Expand Up @@ -77,18 +79,16 @@ import {AjfValidationService} from './validation-service';
AjfReadOnlyFieldComponent,
AjfReadOnlyFileFieldComponent,
AjfReadOnlyImageFieldComponent,
AjfReadOnlySelectFieldComponent,
AjfReadOnlyTableFieldComponent,
AjfReadOnlyVideoUrlFieldComponent,
AjfTableRowClass,
AjfTableVisibleColumnsPipe,
AjfValidSlidePipe,
],
imports: [
AjfCommonModule,
AjfFileInputModule,
CommonModule,
HttpClientModule,
ReactiveFormsModule,
AjfCommonModule, AjfFileInputModule, CommonModule, HttpClientModule, ReactiveFormsModule,
TranslateModule
],
exports: [
AjfAsFieldInstancePipe,
Expand All @@ -111,12 +111,23 @@ import {AjfValidationService} from './validation-service';
AjfReadOnlyFieldComponent,
AjfReadOnlyFileFieldComponent,
AjfReadOnlyImageFieldComponent,
AjfReadOnlySelectFieldComponent,
AjfReadOnlyTableFieldComponent,
AjfReadOnlyVideoUrlFieldComponent,
AjfTableRowClass,
AjfTableVisibleColumnsPipe,
AjfValidSlidePipe,
],
entryComponents: [
AjfFileFieldComponent,
AjfImageFieldComponent,
AjfReadOnlyFieldComponent,
AjfReadOnlyFileFieldComponent,
AjfReadOnlyImageFieldComponent,
AjfReadOnlySelectFieldComponent,
AjfReadOnlyTableFieldComponent,
AjfReadOnlyVideoUrlFieldComponent,
],
providers: [
AjfDateValueStringPipe,
AjfFormRendererService,
Expand Down
1 change: 1 addition & 0 deletions src/core/forms/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export * from './node-complete-name';
export * from './range';
export * from './read-only-file-field';
export * from './read-only-image-field';
export * from './read-only-select-field';
export * from './read-only-video-url-field';
export * from './search-alert-threshold';
export * from './serializers/attachments-origin-serializer';
Expand Down
12 changes: 12 additions & 0 deletions src/core/forms/read-only-select-field.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<ng-container *ngIf="control|async as ctrl">
<ng-container *ngFor="let choice of instance.filteredChoices; let idx = index">
<ng-container *ngIf="multiple|async; else singleChoice">
<span *ngIf="ctrl.value && ctrl.value?.indexOf(choice.value) > -1">
{{choice.label|translate}}{{ctrl.value[ctrl.value.length - 1] !== choice.value ? ', ': ''}}
</span>
</ng-container>
<ng-template #singleChoice>
<span *ngIf="ctrl.value === choice.value">{{choice.label|translate}}</span>
</ng-template>
</ng-container>
</ng-container>
Empty file.
60 changes: 60 additions & 0 deletions src/core/forms/read-only-select-field.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @license
* Copyright (C) Gnucoop soc. coop.
*
* This file is part of the Advanced JSON forms (ajf).
*
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Advanced JSON forms (ajf).
* If not, see http://www.gnu.org/licenses/.
*
*/
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Inject,
ViewEncapsulation
} from '@angular/core';
import {Observable} from 'rxjs';
import {filter, map} from 'rxjs/operators';

import {AjfBaseFieldComponent} from './base-field';
import {AjfFormRendererService} from './form-renderer';
import {
AjfFieldWithChoicesInstance
} from './interface/fields-instances/field-with-choices-instance';
import {AJF_WARNING_ALERT_SERVICE, AjfWarningAlertService} from './warning-alert-service';
import {AjfFieldType} from './interface/fields/field-type';

@Component({
templateUrl: 'read-only-select-field.html',
styleUrls: ['read-only-select-field.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
})
export class AjfReadOnlySelectFieldComponent extends
AjfBaseFieldComponent<AjfFieldWithChoicesInstance<String|number>> {
readonly multiple: Observable<boolean>;

constructor(
cdr: ChangeDetectorRef, service: AjfFormRendererService,
@Inject(AJF_WARNING_ALERT_SERVICE) was: AjfWarningAlertService) {
super(cdr, service, was);

this.multiple = this.control.pipe(
filter(control => control != null),
map(() => this.instance.node.fieldType === AjfFieldType.MultipleChoice)
);
}
}
6 changes: 5 additions & 1 deletion src/dev-app/mat-forms/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ export const formSchema: any = {
type: 'fixed',
name: 'animals',
choicesType: 'string',
choices: [{value: 'dog', label: 'Dog'}, {value: 'cat', label: 'Cat'}]
choices: [
{value: 'dog', label: 'Dog'},
{value: 'cat', label: 'Cat'},
{value: 'blackcat', label: 'Black Cat'}
]
},
{
type: 'fixed',
Expand Down
5 changes: 3 additions & 2 deletions src/ionic/forms/field-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
AjfReadOnlyFieldComponent,
AjfReadOnlyFileFieldComponent,
AjfReadOnlyImageFieldComponent,
AjfReadOnlySelectFieldComponent,
AjfReadOnlyTableFieldComponent,
AjfReadOnlyVideoUrlFieldComponent,
} from '@ajf/core/forms';
Expand Down Expand Up @@ -90,11 +91,11 @@ export class AjfFieldService extends CoreService {
};
this.componentsMap[AjfFieldType.SingleChoice] = {
component: AjfSingleChoiceFieldComponent,
readOnlyComponent: AjfReadOnlyFieldComponent
readOnlyComponent: AjfReadOnlySelectFieldComponent
};
this.componentsMap[AjfFieldType.MultipleChoice] = {
component: AjfMultipleChoiceFieldComponent,
readOnlyComponent: AjfReadOnlyFieldComponent
readOnlyComponent: AjfReadOnlySelectFieldComponent
};
this.componentsMap[AjfFieldType.Time] = {
component: AjfTimeFieldComponent,
Expand Down
101 changes: 101 additions & 0 deletions src/ionic/forms/form-read-only.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @license
* Copyright (C) Gnucoop soc. coop.
*
* This file is part of the Advanced JSON forms (ajf).
*
* Advanced JSON forms (ajf) is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Advanced JSON forms (ajf) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
* General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Advanced JSON forms (ajf).
* If not, see http://www.gnu.org/licenses/.
*
*/

import {AjfFieldType, AjfFieldWithChoices, AjfFormSerializer, AjfNodeType} from '@ajf/core/forms';
import {Component} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
import {TranslateModule} from '@ngx-translate/core';

import {AjfFormsModule} from './public-api';

describe('AjfFormRenderer', () => {
beforeEach(async () => {
TestBed.configureTestingModule({
imports: [
AjfFormsModule,
NoopAnimationsModule,
TranslateModule.forRoot(),
],
declarations: [
TestComponent,
],
});
await TestBed.compileComponents();
});

it('read only select value should equals to label', async () => {
const fixture = TestBed.createComponent(TestComponent);

fixture.detectChanges();
await fixture.whenStable();
fixture.detectChanges();
await fixture.whenStable();

let field = fixture.debugElement.query(By.css('.ajf-field-field')).nativeElement;
expect(field.textContent).toEqual('Black Cat');
});
});

const testForm = {
choicesOrigins: [
{
name: 'choices',
label: 'choices',
type: 'fixed',
choices: [
{value: 'dog', label: 'Dog'},
{value: 'cat', label: 'Cat'},
{value: 'blackcat', label: 'Black Cat'},
],
},
],
nodes: [{
id: 1,
parent: 0,
parentNode: 0,
nodeType: AjfNodeType.AjfSlide,
name: 'slide',
label: 'slide',
conditionalBranches: [{condition: 'true'}],
nodes: [{
id: 2,
parent: 1,
parentNode: 0,
nodeType: AjfNodeType.AjfField,
name: 'field',
label: 'field',
conditionalBranches: [{condition: 'true'}],
fieldType: AjfFieldType.SingleChoice,
choicesOriginRef: 'choices',
validation: {notEmpty: true} as any
} as unknown as AjfFieldWithChoices<string>]
}],
} as any;

@Component({
template: '<ajf-form readonly [form]="form"></ajf-form>',
})
class TestComponent {
form = AjfFormSerializer.fromJson(testForm, {field: 'blackcat'});
}
5 changes: 3 additions & 2 deletions src/material/forms/field-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
AjfReadOnlyFieldComponent,
AjfReadOnlyFileFieldComponent,
AjfReadOnlyImageFieldComponent,
AjfReadOnlySelectFieldComponent,
AjfReadOnlyTableFieldComponent,
AjfReadOnlyVideoUrlFieldComponent,
} from '@ajf/core/forms';
Expand Down Expand Up @@ -87,11 +88,11 @@ export class AjfFieldService extends CoreService {
this.componentsMap[AjfFieldType.Empty] = {component: AjfEmptyFieldComponent},
this.componentsMap[AjfFieldType.SingleChoice] = {
component: AjfSingleChoiceFieldComponent,
readOnlyComponent: AjfReadOnlyFieldComponent
readOnlyComponent: AjfReadOnlySelectFieldComponent
},
this.componentsMap[AjfFieldType.MultipleChoice] = {
component: AjfMultipleChoiceFieldComponent,
readOnlyComponent: AjfReadOnlyFieldComponent
readOnlyComponent: AjfReadOnlySelectFieldComponent
},
this.componentsMap[AjfFieldType.Time] = {
component: AjfTimeFieldComponent,
Expand Down

0 comments on commit ddac4e6

Please sign in to comment.