From 774747801b47c99dc55e7b9fa11e0926a0e48436 Mon Sep 17 00:00:00 2001 From: Christoph Hinssen Date: Wed, 6 Oct 2021 12:12:52 +0200 Subject: [PATCH] GH-13939 Skeleton of read-only attribute component --- ...configurator-textfield-form.component.html | 38 +++++++---- .../configurator-textfield-form.component.ts | 12 +++- .../textfield/components/index.ts | 1 + ...tfield-input-field-readonly.component.html | 15 ++++ ...eld-input-field-readonly.component.spec.ts | 59 ++++++++++++++++ ...extfield-input-field-readonly.component.ts | 68 +++++++++++++++++++ ...extfield-configurator-components.module.ts | 3 + 7 files changed, 183 insertions(+), 13 deletions(-) create mode 100644 feature-libs/product-configurator/textfield/components/input-field-readonly/configurator-textfield-input-field-readonly.component.html create mode 100644 feature-libs/product-configurator/textfield/components/input-field-readonly/configurator-textfield-input-field-readonly.component.spec.ts create mode 100644 feature-libs/product-configurator/textfield/components/input-field-readonly/configurator-textfield-input-field-readonly.component.ts diff --git a/feature-libs/product-configurator/textfield/components/form/configurator-textfield-form.component.html b/feature-libs/product-configurator/textfield/components/form/configurator-textfield-form.component.html index b4b464fd3eb..b1b9c2c8424 100644 --- a/feature-libs/product-configurator/textfield/components/form/configurator-textfield-form.component.html +++ b/feature-libs/product-configurator/textfield/components/form/configurator-textfield-form.component.html @@ -1,14 +1,28 @@ -
- -
- + +
+ +
+ + +
+ +
+ +
+
diff --git a/feature-libs/product-configurator/textfield/components/form/configurator-textfield-form.component.ts b/feature-libs/product-configurator/textfield/components/form/configurator-textfield-form.component.ts index eb362878e3a..93c6d4f8c00 100644 --- a/feature-libs/product-configurator/textfield/components/form/configurator-textfield-form.component.ts +++ b/feature-libs/product-configurator/textfield/components/form/configurator-textfield-form.component.ts @@ -1,10 +1,11 @@ import { Component } from '@angular/core'; import { CommonConfigurator, + ConfiguratorRouter, ConfiguratorRouterExtractorService, } from '@spartacus/product-configurator/common'; import { Observable } from 'rxjs'; -import { switchMap } from 'rxjs/operators'; +import { map, switchMap } from 'rxjs/operators'; import { ConfiguratorTextfieldService } from '../../core/facade/configurator-textfield.service'; import { ConfiguratorTextfield } from '../../core/model/configurator-textfield.model'; @@ -34,6 +35,15 @@ export class ConfiguratorTextfieldFormComponent { }) ); + isEditable$: Observable = this.configRouterExtractorService + .extractRouterData() + .pipe( + map( + (routerData) => + routerData.pageType === ConfiguratorRouter.PageType.CONFIGURATION + ) + ); + constructor( protected configuratorTextfieldService: ConfiguratorTextfieldService, protected configRouterExtractorService: ConfiguratorRouterExtractorService diff --git a/feature-libs/product-configurator/textfield/components/index.ts b/feature-libs/product-configurator/textfield/components/index.ts index 5922a10edb2..cc828fa448f 100644 --- a/feature-libs/product-configurator/textfield/components/index.ts +++ b/feature-libs/product-configurator/textfield/components/index.ts @@ -1,4 +1,5 @@ export * from './add-to-cart-button/configurator-textfield-add-to-cart-button.component'; export * from './form/configurator-textfield-form.component'; export * from './input-field/configurator-textfield-input-field.component'; +export * from './input-field-readonly/configurator-textfield-input-field-readonly.component'; export * from './textfield-configurator-components.module'; diff --git a/feature-libs/product-configurator/textfield/components/input-field-readonly/configurator-textfield-input-field-readonly.component.html b/feature-libs/product-configurator/textfield/components/input-field-readonly/configurator-textfield-input-field-readonly.component.html new file mode 100644 index 00000000000..0431b06d12f --- /dev/null +++ b/feature-libs/product-configurator/textfield/components/input-field-readonly/configurator-textfield-input-field-readonly.component.html @@ -0,0 +1,15 @@ +readonly + +
+ +
diff --git a/feature-libs/product-configurator/textfield/components/input-field-readonly/configurator-textfield-input-field-readonly.component.spec.ts b/feature-libs/product-configurator/textfield/components/input-field-readonly/configurator-textfield-input-field-readonly.component.spec.ts new file mode 100644 index 00000000000..64eef1f0d23 --- /dev/null +++ b/feature-libs/product-configurator/textfield/components/input-field-readonly/configurator-textfield-input-field-readonly.component.spec.ts @@ -0,0 +1,59 @@ +import { ChangeDetectionStrategy } from '@angular/core'; +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; +import { ReactiveFormsModule } from '@angular/forms'; +import { ConfiguratorTextfieldInputFieldReadonlyComponent } from './configurator-textfield-input-field-readonly.component'; + +describe('TextfieldInputFieldComponent', () => { + let component: ConfiguratorTextfieldInputFieldReadonlyComponent; + + let fixture: ComponentFixture; + + beforeEach( + waitForAsync(() => { + TestBed.configureTestingModule({ + declarations: [ConfiguratorTextfieldInputFieldReadonlyComponent], + imports: [ReactiveFormsModule], + }) + .overrideComponent(ConfiguratorTextfieldInputFieldReadonlyComponent, { + set: { + changeDetection: ChangeDetectionStrategy.Default, + }, + }) + .compileComponents(); + }) + ); + + beforeEach(() => { + fixture = TestBed.createComponent( + ConfiguratorTextfieldInputFieldReadonlyComponent + ); + component = fixture.componentInstance; + component.attribute = { + configurationLabel: 'attributeName', + configurationValue: 'input123', + }; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); + + it('should set value on init', () => { + expect(component.attributeInputForm.value).toEqual('input123'); + }); + + it('should emit a change event on change ', () => { + spyOn(component.inputChange, 'emit').and.callThrough(); + component.onInputChange(); + expect(component.inputChange.emit).toHaveBeenCalledWith( + component.attribute + ); + }); + + it('should generate id with prefixt', () => { + expect(component.getId(component.attribute)).toEqual( + 'cx-configurator-textfieldattributeName' + ); + }); +}); diff --git a/feature-libs/product-configurator/textfield/components/input-field-readonly/configurator-textfield-input-field-readonly.component.ts b/feature-libs/product-configurator/textfield/components/input-field-readonly/configurator-textfield-input-field-readonly.component.ts new file mode 100644 index 00000000000..eb50d402b91 --- /dev/null +++ b/feature-libs/product-configurator/textfield/components/input-field-readonly/configurator-textfield-input-field-readonly.component.ts @@ -0,0 +1,68 @@ +import { + ChangeDetectionStrategy, + Component, + EventEmitter, + Input, + OnInit, + Output, +} from '@angular/core'; +import { FormControl } from '@angular/forms'; +import { ConfiguratorTextfield } from '../../core/model/configurator-textfield.model'; + +@Component({ + selector: 'cx-configurator-textfield-input-field-readonly', + templateUrl: './configurator-textfield-input-field-readonly.component.html', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class ConfiguratorTextfieldInputFieldReadonlyComponent + implements OnInit { + PREFIX_TEXTFIELD = 'cx-configurator-textfield'; + attributeInputForm = new FormControl(''); + + @Input() attribute: ConfiguratorTextfield.ConfigurationInfo; + @Output() + inputChange = new EventEmitter(); + + constructor() {} + + ngOnInit() { + this.attributeInputForm.setValue(this.attribute.configurationValue); + } + /** + * Triggered if an attribute value is changed. Triggers the emission of the inputChange event emitter that is + * in turn received in the form component + */ + onInputChange(): void { + const attribute: ConfiguratorTextfield.ConfigurationInfo = { + configurationLabel: this.attribute.configurationLabel, + configurationValue: this.attributeInputForm.value, + }; + + this.inputChange.emit(attribute); + } + /** + * Compiles an ID for the attribute label by using the label from the backend and a prefix 'label' + * @param attribute Textfield configurator attribute. Carries the attribute label information from the backend + * @returns ID + */ + getIdLabel(attribute: ConfiguratorTextfield.ConfigurationInfo): string { + return ( + this.PREFIX_TEXTFIELD + 'label' + this.getLabelForIdGeneration(attribute) + ); + } + /** + * Compiles an ID for the attribute value by using the label from the backend + * @param attribute Textfield configurator attribute. Carries the attribute label information from the backend + * @returns ID + */ + getId(attribute: ConfiguratorTextfield.ConfigurationInfo): string { + return this.PREFIX_TEXTFIELD + this.getLabelForIdGeneration(attribute); + } + + protected getLabelForIdGeneration( + attribute: ConfiguratorTextfield.ConfigurationInfo + ): string { + //replace white spaces with an empty string + return attribute.configurationLabel.replace(/\s/g, ''); + } +} diff --git a/feature-libs/product-configurator/textfield/components/textfield-configurator-components.module.ts b/feature-libs/product-configurator/textfield/components/textfield-configurator-components.module.ts index ca226899824..e4ce1d09e7b 100644 --- a/feature-libs/product-configurator/textfield/components/textfield-configurator-components.module.ts +++ b/feature-libs/product-configurator/textfield/components/textfield-configurator-components.module.ts @@ -12,6 +12,7 @@ import { import { ConfiguratorTextfieldAddToCartButtonComponent } from './add-to-cart-button/configurator-textfield-add-to-cart-button.component'; import { ConfiguratorTextfieldFormComponent } from './form/configurator-textfield-form.component'; import { ConfiguratorTextfieldInputFieldComponent } from './input-field/configurator-textfield-input-field.component'; +import { ConfiguratorTextfieldInputFieldReadonlyComponent } from './input-field-readonly/configurator-textfield-input-field-readonly.component'; @NgModule({ imports: [ @@ -35,11 +36,13 @@ import { ConfiguratorTextfieldInputFieldComponent } from './input-field/configur declarations: [ ConfiguratorTextfieldFormComponent, ConfiguratorTextfieldInputFieldComponent, + ConfiguratorTextfieldInputFieldReadonlyComponent, ConfiguratorTextfieldAddToCartButtonComponent, ], exports: [ ConfiguratorTextfieldFormComponent, ConfiguratorTextfieldInputFieldComponent, + ConfiguratorTextfieldInputFieldReadonlyComponent, ConfiguratorTextfieldAddToCartButtonComponent, ], })