[ADF-3275] Select CardView item (#3530)

* Select card view item

* package json fix

* package json fix

* console.log removed

* Usage of generic
This commit is contained in:
Alex Bolboșenco
2018-06-27 16:14:13 +03:00
committed by Eugenio Romano
parent da8ffdfe5e
commit 7563f91665
13 changed files with 338 additions and 1 deletions

View File

@@ -79,6 +79,12 @@ Defining properties from Typescript:
value: [],
key: 'key-value-pairs'
}),
new CardViewSelectItemModel({
label: 'Select box',
value: 'one',
options$: of([{ key: 'one', label: 'One' }, { key: 'two', label: 'Two' }]),
key: 'select'
}),
...
]
```
@@ -105,6 +111,7 @@ You define the property list, the [`CardViewComponent`](../core/card-view.compon
- [**CardViewIntItemModel**](#card-int-item) - _for integer items_
- [**CardViewFloatItemModel**](#card-float-item) - _for float items_
- [**CardViewKeyValuePairsItemModel**](#card-key-values-pairs-item) - _for key-value-pairs items_
- [**CardViewSelectItemModel**](#card-select-item) - _for select items_
Each of these types implements the [Card View Item interface](card-view-item.interface.md):
@@ -309,6 +316,23 @@ const keyValuePairsItemProperty = new CardViewKeyValuePairsItemModel(options);
| value\* | `[{ name: '', value: '' }, ...]` | | The original data value for the item |
#### Card Select Item
[`CardViewSelectItemModel`](../../lib/core/card-view/models/card-view-selectitem.model.ts) is a property type for select properties.
```ts
const selectItemProperty = new CardViewSelectItemModel(options);
```
| Name | Type | Default | Description |
| ---- | ---- | ------- | ----------- |
| label\* | string | | Item label |
| key\* | string | | Identifying key (important when editing the item) |
| editable | boolean | false | Toggles whether the item is editable |
| value | string | | The original data value for the item |
| options$\* | Observable<CardViewSelectItemOption[]> | | The original data value for the item |
## See also

View File

@@ -25,7 +25,8 @@ import {
MatIconModule,
MatInputModule,
MatCheckboxModule,
MatNativeDateModule
MatNativeDateModule,
MatSelectModule
} from '@angular/material';
import { MatDatetimepickerModule, MatNativeDatetimeModule } from '@mat-datetimepicker/core';
import { FlexLayoutModule } from '@angular/flex-layout';
@@ -39,6 +40,7 @@ import { CardViewItemDispatcherComponent } from './components/card-view-item-dis
import { CardViewMapItemComponent } from './components/card-view-mapitem/card-view-mapitem.component';
import { CardViewTextItemComponent } from './components/card-view-textitem/card-view-textitem.component';
import { CardViewKeyValuePairsItemComponent } from './components/card-view-keyvaluepairsitem/card-view-keyvaluepairsitem.component';
import { CardViewSelectItemComponent } from './components/card-view-selectitem/card-view-selectitem.component';
@NgModule({
imports: [
@@ -52,6 +54,7 @@ import { CardViewKeyValuePairsItemComponent } from './components/card-view-keyva
MatInputModule,
MatTableModule,
MatIconModule,
MatSelectModule,
MatButtonModule,
MatDatetimepickerModule,
MatNativeDatetimeModule
@@ -63,6 +66,7 @@ import { CardViewKeyValuePairsItemComponent } from './components/card-view-keyva
CardViewMapItemComponent,
CardViewTextItemComponent,
CardViewKeyValuePairsItemComponent,
CardViewSelectItemComponent,
CardViewItemDispatcherComponent,
CardViewContentProxyDirective
],
@@ -71,6 +75,7 @@ import { CardViewKeyValuePairsItemComponent } from './components/card-view-keyva
CardViewDateItemComponent,
CardViewMapItemComponent,
CardViewTextItemComponent,
CardViewSelectItemComponent,
CardViewKeyValuePairsItemComponent
],
exports: [
@@ -79,6 +84,7 @@ import { CardViewKeyValuePairsItemComponent } from './components/card-view-keyva
CardViewDateItemComponent,
CardViewMapItemComponent,
CardViewTextItemComponent,
CardViewSelectItemComponent,
CardViewKeyValuePairsItemComponent
]
})

View File

@@ -0,0 +1,13 @@
<div class="adf-property-label">{{ property.label | translate }}</div>
<div class="adf-property-value">
<div *ngIf="!isEditable()" data-automation-class="read-only-value">{{ property.displayValue | async }}</div>
<div *ngIf="isEditable()">
<mat-form-field>
<mat-select placeholder="{{ property.label | translate }}" [(value)]="value" (change)="onChange($event)" data-automation-class="select-box">
<mat-option *ngFor="let option of getOptions() | async" [value]="option.key">
{{ option.label | translate }}
</mat-option>
</mat-select>
</mat-form-field>
</div>
</div>

View File

@@ -0,0 +1,113 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { CardViewSelectItemModel } from '../../models/card-view-selectitem.model';
import { CardViewUpdateService } from '../../services/card-view-update.service';
import { CardViewSelectItemComponent } from './card-view-selectitem.component';
import { setupTestBed } from '../../../testing/setupTestBed';
import { CoreTestingModule } from '../../../testing/core.testing.module';
import { of } from 'rxjs/observable/of';
describe('CardViewSelectItemComponent', () => {
let fixture: ComponentFixture<CardViewSelectItemComponent>;
let component: CardViewSelectItemComponent;
let cardViewUpdateService;
const mockData = [{ key: 'one', label: 'One' }, { key: 'two', label: 'Two' }, { key: 'three', label: 'Three' }];
const mockDefaultProps = {
label: 'Select box label',
value: 'two',
options$: of(mockData),
key: 'key',
editable: true
};
setupTestBed({
imports: [CoreTestingModule]
});
beforeEach(() => {
fixture = TestBed.createComponent(CardViewSelectItemComponent);
cardViewUpdateService = TestBed.get(CardViewUpdateService);
component = fixture.componentInstance;
component.property = new CardViewSelectItemModel(mockDefaultProps);
});
afterEach(() => {
fixture.destroy();
});
describe('Rendering', () => {
it('should render the label', () => {
fixture.detectChanges();
const labelValue = fixture.debugElement.query(By.css('.adf-property-label'));
expect(labelValue).not.toBeNull();
expect(labelValue.nativeElement.innerText).toBe('Select box label');
});
it('should render readOnly value is editable property is FALSE', () => {
component.property = new CardViewSelectItemModel({
...mockDefaultProps,
editable: false
});
component.ngOnChanges();
fixture.detectChanges();
const readOnly = fixture.debugElement.query(By.css('[data-automation-class="read-only-value"]'));
const selectBox = fixture.debugElement.query(By.css('[data-automation-class="select-box"]'));
expect(readOnly).not.toBeNull();
expect(selectBox).toBeNull();
});
it('should render select box if editable property is TRUE', () => {
component.ngOnChanges();
component.editable = true;
fixture.detectChanges();
const selectBox = fixture.debugElement.query(By.css('[data-automation-class="select-box"]'));
expect(selectBox).not.toBeNull();
});
it('should update property on input blur', async(() => {
spyOn(cardViewUpdateService, 'update');
component.ngOnChanges();
component.editable = true;
fixture.detectChanges();
const selectBox = fixture.debugElement.query(By.css('[data-automation-class="select-box"]'));
selectBox.nativeElement.click();
fixture.detectChanges();
const option = fixture.debugElement.query(By.css(`mat-option[ng-reflect-value="${mockData[2].key}"]`));
option.nativeElement.click();
fixture.detectChanges();
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(cardViewUpdateService.update).toHaveBeenCalled();
expect(component.property.value).toBe(mockData[2].key);
});
}));
});
});

View File

@@ -0,0 +1,56 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Component, Input, OnChanges } from '@angular/core';
import { CardViewSelectItemModel } from '../../models/card-view-selectitem.model';
import { CardViewUpdateService } from '../../services/card-view-update.service';
import { Observable } from 'rxjs/Observable';
import { CardViewSelectItemOption } from '../../interfaces/card-view.interfaces';
import { MatSelectChange } from '@angular/material';
@Component({
selector: 'adf-card-view-selectitem',
templateUrl: './card-view-selectitem.component.html'
})
export class CardViewSelectItemComponent implements OnChanges {
@Input() property: CardViewSelectItemModel<string>;
@Input() editable: boolean = false;
@Input() options$: Observable<CardViewSelectItemOption<string>[]>;
value: string;
constructor(private cardViewUpdateService: CardViewUpdateService) {}
ngOnChanges(): void {
this.value = this.property.value;
}
isEditable(): boolean {
return this.editable && this.property.editable;
}
getOptions(): Observable<CardViewSelectItemOption<string>[]> {
return this.options$ || this.property.options$;
}
onChange(event: MatSelectChange): void {
this.cardViewUpdateService.update(this.property, event.value);
this.property.value = event.value;
}
}

View File

@@ -21,4 +21,5 @@ export * from './card-view-dateitem/card-view-dateitem.component';
export * from './card-view-item-dispatcher/card-view-item-dispatcher.component';
export * from './card-view-mapitem/card-view-mapitem.component';
export * from './card-view-textitem/card-view-textitem.component';
export * from './card-view-selectitem/card-view-selectitem.component';
export * from './card-view-keyvaluepairsitem/card-view-keyvaluepairsitem.component';

View File

@@ -0,0 +1,29 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CardViewItemProperties } from './card-view-item-properties.interface';
import { Observable } from 'rxjs/Observable';
export interface CardViewSelectItemOption<T> {
label: string;
key: T;
}
export interface CardViewSelectItemProperties<T> extends CardViewItemProperties {
value: string;
options$: Observable<CardViewSelectItemOption<T>[]>;
}

View File

@@ -23,3 +23,4 @@ export * from './card-view-dateitem-properties.interface';
export * from './card-view-boolitem-properties.interface';
export * from './card-view-textitem-pipe-property.interface';
export * from './card-view-keyvaluepairsitem-properties.interface';
export * from './card-view-selectitem-properties.interface';

View File

@@ -0,0 +1,46 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { async } from '@angular/core/testing';
import { CardViewSelectItemModel } from './card-view-selectitem.model';
import { CardViewSelectItemProperties } from '../interfaces/card-view.interfaces';
import { of } from 'rxjs/observable/of';
describe('CardViewSelectItemModel', () => {
let properties: CardViewSelectItemProperties;
const mockData = [{ key: 'one', label: 'One' }, { key: 'two', label: 'Two' }, { key: 'three', label: 'Three' }];
beforeEach(() => {
properties = {
label: 'Select box label',
value: 'two',
options$: of(mockData),
key: 'key',
editable: true
};
});
describe('displayValue', () => {
it('should return the value if it is present', async(() => {
const itemModel = new CardViewSelectItemModel(properties);
itemModel.displayValue.subscribe(value => {
expect(value).toBe(mockData[1].label);
});
}));
});
});

View File

@@ -0,0 +1,44 @@
/*!
* @license
* Copyright 2016 Alfresco Software, Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CardViewItem } from '../interfaces/card-view-item.interface';
import { DynamicComponentModel } from '../../services/dynamic-component-mapper.service';
import { CardViewBaseItemModel } from './card-view-baseitem.model';
import { CardViewSelectItemProperties, CardViewSelectItemOption } from '../interfaces/card-view.interfaces';
import { Observable } from 'rxjs/Observable';
import { switchMap } from 'rxjs/operators';
import { of } from 'rxjs/observable/of';
export class CardViewSelectItemModel<T> extends CardViewBaseItemModel implements CardViewItem, DynamicComponentModel {
type: string = 'select';
options$: Observable<CardViewSelectItemOption<T>[]>;
constructor(obj: CardViewSelectItemProperties<T>) {
super(obj);
this.options$ = obj.options$;
}
get displayValue() {
return this.options$.pipe(
switchMap(options => {
const option = options.find(o => o.key === this.value);
return of(option ? option.label : '');
})
);
}
}

View File

@@ -24,3 +24,4 @@ export * from './card-view-intitem.model';
export * from './card-view-mapitem.model';
export * from './card-view-textitem.model';
export * from './card-view-keyvaluepairs.model';
export * from './card-view-selectitem.model';

View File

@@ -21,6 +21,7 @@ export {
CardViewDateItemComponent,
CardViewMapItemComponent,
CardViewTextItemComponent,
CardViewSelectItemComponent,
CardViewKeyValuePairsItemComponent
} from './components/card-view.components';

View File

@@ -19,6 +19,7 @@ import { Injectable, Type } from '@angular/core';
import { CardViewDateItemComponent } from '../components/card-view-dateitem/card-view-dateitem.component';
import { CardViewMapItemComponent } from '../components/card-view-mapitem/card-view-mapitem.component';
import { CardViewTextItemComponent } from '../components/card-view-textitem/card-view-textitem.component';
import { CardViewSelectItemComponent } from '../components/card-view-selectitem/card-view-selectitem.component';
import { CardViewBoolItemComponent } from '../components/card-view-boolitem/card-view-boolitem.component';
import { CardViewKeyValuePairsItemComponent } from '../components/card-view-keyvaluepairsitem/card-view-keyvaluepairsitem.component';
import { DynamicComponentMapper, DynamicComponentResolveFunction, DynamicComponentResolver } from '../../services/dynamic-component-mapper.service';
@@ -30,6 +31,7 @@ export class CardItemTypeService extends DynamicComponentMapper {
protected types: { [key: string]: DynamicComponentResolveFunction } = {
'text': DynamicComponentResolver.fromType(CardViewTextItemComponent),
'select': DynamicComponentResolver.fromType(CardViewSelectItemComponent),
'int': DynamicComponentResolver.fromType(CardViewTextItemComponent),
'float': DynamicComponentResolver.fromType(CardViewTextItemComponent),
'date': DynamicComponentResolver.fromType(CardViewDateItemComponent),