Skip to content

Commit

Permalink
feat(viewer): make hex code display optional for color value (DSP-1667)…
Browse files Browse the repository at this point in the history
… (#303)

* feat(viewer): make hex code display optional for color value

* feat(viewer): make hex code display optional for color value
  • Loading branch information
tobiasschweizer committed Jun 15, 2021
1 parent e124aa3 commit 0b03781
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
@@ -1,8 +1,7 @@
<span *ngIf="mode === 'read'; else showForm" class="read-mode-view">
<div class="color-preview"
[style.color]="textColor"
<div class="rm-value"
[style.background-color]="valueFormControl.value">
<span class="rm-value">{{ valueFormControl.value }}</span>
<span *ngIf="showHexCode" class="hexcode" [style.color]="textColor">{{ valueFormControl.value }}</span>
</div>
<span class="rm-comment" *ngIf="shouldShowComment">
{{ commentFormControl.value }}
Expand Down
Expand Up @@ -15,10 +15,11 @@
}
}

.color-preview {
.rm-value {
border: 1px solid rgba(33,33,33,.5);
border-radius: 4px;
padding: 3px 12px;
margin: 6px 0;
margin: 6px 0 10px 0 !important;
width: 6em;
min-height: 8px;
}
Expand Up @@ -70,7 +70,7 @@ class TestColorPickerComponent implements ControlValueAccessor, MatFormFieldCont
*/
@Component({
template: `
<dsp-color-value #colorVal [displayValue]="displayColorVal" [mode]="mode"></dsp-color-value>`
<dsp-color-value #colorVal [displayValue]="displayColorVal" [mode]="mode" [showHexCode]="showColorHex"></dsp-color-value>`
})
class TestHostDisplayValueComponent implements OnInit {

Expand All @@ -80,6 +80,8 @@ class TestHostDisplayValueComponent implements OnInit {

mode: 'read' | 'update' | 'create' | 'search';

showColorHex = false;

ngOnInit() {

MockResource.getTestThing().subscribe(res => {
Expand Down Expand Up @@ -159,15 +161,35 @@ describe('ColorValueComponent', () => {
valueReadModeNativeElement = valueReadModeDebugElement.nativeElement;
});

it('should display an existing value', () => {
it('should display an existing value without a hex color code', () => {

expect(testHostComponent.colorValueComponent.displayValue.color).toEqual('#ff3333');

expect(testHostComponent.colorValueComponent.form.valid).toBeTruthy();

expect(testHostComponent.colorValueComponent.mode).toEqual('read');

expect(valueReadModeNativeElement.innerText).toEqual('#ff3333');
expect(valueReadModeNativeElement.style.backgroundColor).not.toBeUndefined();

expect(valueReadModeNativeElement.innerText).toEqual('');

});

it('should display an existing value with a hex color code', () => {

testHostComponent.showColorHex = true;

testHostFixture.detectChanges();

expect(testHostComponent.colorValueComponent.displayValue.color).toEqual('#ff3333');

expect(testHostComponent.colorValueComponent.form.valid).toBeTruthy();

expect(testHostComponent.colorValueComponent.mode).toEqual('read');

expect(valueReadModeNativeElement.style.backgroundColor).not.toBeUndefined();

expect(valueReadModeNativeElement.innerText).toEqual('#ff3333');

});

Expand Down Expand Up @@ -301,12 +323,54 @@ describe('ColorValueComponent', () => {

testHostFixture.detectChanges();

expect(valueReadModeNativeElement.innerText).toEqual('#d8d8d8');
expect(valueReadModeNativeElement.style.backgroundColor).not.toBeUndefined();

expect(valueReadModeNativeElement.innerText).toEqual('');

expect(testHostComponent.colorValueComponent.form.valid).toBeTruthy();

});

it('should set a new display value not showing the hex color code', () => {

const newColor = new ReadColorValue();

newColor.color = '#d8d8d8';
newColor.id = 'updatedId';

testHostComponent.displayColorVal = newColor;

testHostFixture.detectChanges();

expect(valueReadModeNativeElement.style.backgroundColor).not.toBeUndefined();

expect(valueReadModeNativeElement.innerText).toEqual('');

expect(testHostComponent.colorValueComponent.form.valid).toBeTruthy();

});

it('should set a new display value showing the hex color code', () => {

testHostComponent.showColorHex = true;

const newColor = new ReadColorValue();

newColor.color = '#d8d8d8';
newColor.id = 'updatedId';

testHostComponent.displayColorVal = newColor;

testHostFixture.detectChanges();

expect(valueReadModeNativeElement.style.backgroundColor).not.toBeUndefined();

expect(valueReadModeNativeElement.innerText).toEqual('#d8d8d8');

expect(testHostComponent.colorValueComponent.form.valid).toBeTruthy();

});

it('should unsubscribe when destroyed', () => {
expect(testHostComponent.colorValueComponent.valueChangesSubscription.closed).toBeFalsy();

Expand Down
Expand Up @@ -21,6 +21,8 @@ export class ColorValueComponent extends BaseValueComponent implements OnInit, O

@Input() displayValue?: ReadColorValue;

@Input() showHexCode = false;

valueFormControl: FormControl;
commentFormControl: FormControl;
form: FormGroup;
Expand Down Expand Up @@ -71,6 +73,10 @@ export class ColorValueComponent extends BaseValueComponent implements OnInit, O

ngOnChanges(changes: SimpleChanges): void {
this.resetFormControl();

if (this.showHexCode && this.valueFormControl !== undefined) {
this.textColor = this.getTextColor(this.valueFormControl.value);
}
}

// unsubscribe when the object is destroyed to prevent memory leaks
Expand Down

0 comments on commit 0b03781

Please sign in to comment.