Skip to content

Commit

Permalink
Add Sort buttons to Angular Material ArrayLayoutRenderer and TableRen…
Browse files Browse the repository at this point in the history
…derer (#2038)

* Add Sort buttons to Angular Material's ArrayLayoutRenderer and TableRenderer
* Only render sort buttons in ArrayLayoutRenderer if showSortButtons=true
* Add new example Arrays with sorting
  • Loading branch information
fredjohnd authored Nov 7, 2022
1 parent 770c78f commit f6a6e1b
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 7 deletions.
45 changes: 43 additions & 2 deletions packages/angular-material/src/layouts/array-layout.renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,39 @@ import {
let item of [].constructor(data);
let idx = index;
trackBy: trackByFn;
last as last
last as last;
first as first
"
>
<mat-card>
<mat-card-content>
<jsonforms-outlet [renderProps]="getProps(idx)"></jsonforms-outlet>
</mat-card-content>
<mat-card-actions *ngIf="isEnabled()">
<button
*ngIf="uischema?.options?.showSortButtons"
class="item-up"
mat-button
[disabled]="first"
(click)="up(idx)"
attr.aria-label="{{ this.upAriaLabel }}"
matTooltip="{{ this.upTooltip }}"
matTooltipPosition="right"
>
<mat-icon>arrow_upward</mat-icon>
</button>
<button
*ngIf="uischema?.options?.showSortButtons"
class="item-down"
mat-button
[disabled]="last"
(click)="down(idx)"
attr.aria-label="{{ this.downAriaLabel }}"
matTooltip="{{ this.downTooltip }}"
matTooltipPosition="right"
>
<mat-icon>arrow_downward</mat-icon>
</button>
<button
mat-button
color="warn"
Expand Down Expand Up @@ -128,8 +153,14 @@ export class ArrayLayoutRenderer
noDataMessage: string;
removeTooltip: string;
removeAriaLabel: string;
upTooltip: string;
upAriaLabel: string;
downTooltip: string;
downAriaLabel:string;
noData: boolean;
addItem: (path: string, value: any) => () => void;
moveItemUp: (path: string, index: number) => () => void;
moveItemDown: (path: string, index: number) => () => void;
removeItems: (path: string, toDelete: number[]) => () => void;
uischemas: {
tester: UISchemaTester;
Expand All @@ -148,19 +179,29 @@ export class ArrayLayoutRenderer
add(): void {
this.addItem(this.propsPath, createDefaultValue(this.scopedSchema))();
}
up(index: number): void {
this.moveItemUp(this.propsPath, index)();
}
down(index: number): void {
this.moveItemDown(this.propsPath, index)();
}
ngOnInit() {
super.ngOnInit();
const { addItem, removeItems } = mapDispatchToArrayControlProps(
const { addItem, removeItems, moveUp, moveDown } = mapDispatchToArrayControlProps(
this.jsonFormsService.updateCore.bind(this.jsonFormsService)
);
this.addItem = addItem;
this.moveItemUp = moveUp;
this.moveItemDown = moveDown;
this.removeItems = removeItems;
}
mapAdditionalProps(props: ArrayLayoutProps) {
this.noData = !props.data || props.data === 0;
this.uischemas = props.uischemas;
this.addTooltip = `Add to ${this.label}`;
this.addAriaLabel = `Add to ${this.label} button`;
this.upAriaLabel = `Move ${this.label} up`;
this.downAriaLabel = `Move ${this.label} down`;
this.removeTooltip = `Delete`;
this.removeAriaLabel = `Delete button`;
this.noDataMessage = `No data`;
Expand Down
38 changes: 35 additions & 3 deletions packages/angular-material/src/other/table.renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,29 @@ import {
</th>
</tr>
<tr>
<td mat-cell *matCellDef="let row; let i = index">
<td mat-cell *matCellDef="let row; let i = index; let first = first; let last = last;">
<button
*ngIf="uischema?.options?.showSortButtons"
class="item-up"
mat-button
[disabled]="first"
(click)="up(i)"
matTooltip="Move item up"
matTooltipPosition="right"
>
<mat-icon>arrow_upward</mat-icon>
</button>
<button
*ngIf="uischema?.options?.showSortButtons"
class="item-down"
mat-button
[disabled]="last"
(click)="down(i)"
matTooltip="Move item down"
matTooltipPosition="right"
>
<mat-icon>arrow_downward</mat-icon>
</button>
<button
mat-button
color="warn"
Expand Down Expand Up @@ -93,14 +115,16 @@ import {
`,
styles: [
'table {width: 100%;}',
'.cdk-column-action { width: 5%}']
'.cdk-column-action { width: 15%}']
})
export class TableRenderer extends JsonFormsArrayControl {
detailUiSchema: UISchemaElement;
displayedColumns: string[];
items: ColumnDescription[];
readonly columnsToIgnore = ['array', 'object'];
addItem: (path: string, value: any) => () => void;
moveItemUp: (path: string, index: number) => () => void;
moveItemDown: (path: string, index: number) => () => void;
removeItems: (path: string, toDelete: number[]) => () => void;

constructor(jsonformsService: JsonFormsAngularService) {
Expand Down Expand Up @@ -131,13 +155,21 @@ export class TableRenderer extends JsonFormsArrayControl {
add(): void {
this.addItem(this.propsPath, createDefaultValue(this.scopedSchema))();
}
up(index: number): void {
this.moveItemUp(this.propsPath, index)();
}
down(index: number): void {
this.moveItemDown(this.propsPath, index)();
}
ngOnInit() {
super.ngOnInit();

const { addItem, removeItems } = mapDispatchToArrayControlProps(
const { addItem, removeItems, moveUp, moveDown } = mapDispatchToArrayControlProps(
this.jsonFormsService.updateCore.bind(this.jsonFormsService)
);
this.addItem = addItem;
this.moveItemUp = moveUp;
this.moveItemDown = moveDown;
this.removeItems = removeItems;
}

Expand Down
39 changes: 39 additions & 0 deletions packages/angular-material/test/table-control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ const uischema2: ControlElement = {
type: 'Control',
scope: '#/properties/my'
};
const uischemaWithSorting: ControlElement = {
type: 'Control',
scope: '#',
options: {
showSortButtons: true
}
};
const schema_object1 = {
type: 'array',
items: {
Expand Down Expand Up @@ -326,5 +333,37 @@ describe('Table', () => {
expect(fixture.nativeElement.querySelectorAll('td').length).toBe(4);
});
}));
it('when options.showSortButtons is True, it should render sort buttons', async(() => {
setupMockStore(fixture, {
uischema: uischemaWithSorting,
schema: schema_simple1,
data: ['foo', 'bar'],
renderers
});
component.disabled = false;
fixture.detectChanges();

component.ngOnInit();
fixture.whenStable().then(() => {
expect(fixture.nativeElement.querySelectorAll('.item-up').length).toBe(2);
expect(fixture.nativeElement.querySelectorAll('.item-down').length).toBe(2);
});
}));
it('when options.showSortButtons is False, it should NOT render sort buttons', async(() => {
setupMockStore(fixture, {
uischema: uischema1,
schema: schema_simple1,
data: ['foo', 'bar'],
renderers
});
component.disabled = false;
fixture.detectChanges();

component.ngOnInit();
fixture.whenStable().then(() => {
expect(fixture.nativeElement.querySelectorAll('.item-up').length).toBe(0);
expect(fixture.nativeElement.querySelectorAll('.item-down').length).toBe(0);
});
}));

});
135 changes: 135 additions & 0 deletions packages/examples/src/examples/arrays-with-sorting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
The MIT License
Copyright (c) 2017-2019 EclipseSource Munich
https://github.com/eclipsesource/jsonforms
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { registerExamples } from '../register';
import { StateProps } from '../example';

export const schema = {
type: 'object',
properties: {
comments: {
type: 'array',
items: {
type: 'object',
properties: {
date: {
type: 'string',
format: 'date'
},
message: {
type: 'string',
maxLength: 5
},
enum: {
type: 'string',
const: 'foo'
}
}
}
},
foo:{type:'string'}
}
};

export const uischema = {
type: 'VerticalLayout',
elements: [
{
type: 'Control',
scope: '#/properties/foo'
},
{
type: 'Control',
scope: '#/properties/comments',
options: {
showSortButtons: true
}
}
]
};

export const uischemaWithSorting = {
type: 'VerticalLayout',
elements: [
{
type: 'Control',
scope: '#/properties/foo'
},
{
type: 'Control',
scope: '#/properties/comments',
options: {
showSortButtons: true
}
}
]
};

export const data = {
comments: [
{
date: new Date(2001, 8, 11).toISOString().substr(0, 10),
message: 'This is an example message With sorting'
},
{
date: new Date().toISOString().substr(0, 10),
message: 'Get ready for booohay'
}
]
};

const actions = [
{
'label': 'Enable Sorting',
'apply': (props: StateProps) => {
return {
...props,
uischema: uischemaWithSorting
}
}
},
{
'label': 'Disable Sorting',
'apply': (props: StateProps) => {
return {
...props,
uischema: uischema
}
}
},
];

registerExamples([
{
name: 'array-with-sorting',
label: 'Array with sorting',
data,
schema,
uischema,
config: {
withSort: uischemaWithSorting
},
actions
}
]);
7 changes: 5 additions & 2 deletions packages/examples/src/examples/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ export const uischema = {
},
{
type: 'Control',
scope: '#/properties/comments'
scope: '#/properties/comments',
options: {
showSortButtons: false
}
}
]
};
Expand All @@ -77,7 +80,7 @@ export const uischemaWithSorting = {
type: 'Control',
scope: '#/properties/comments',
options: {
showSortButtons: true
showSortButtons: false
}
}
]
Expand Down
2 changes: 2 additions & 0 deletions packages/examples/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import * as nestedArray from './examples/nestedArrays';
import * as arrayWithDetail from './examples/arrays-with-detail';
import * as arrayWithDetailAndRule from './examples/arrays-with-detail-and-rule';
import * as arrayWithCustomChildLabel from './examples/arrays-with-custom-element-label';
import * as arrayWithSorting from './examples/arrays-with-sorting';
import * as stringArray from './examples/stringArray';
import * as categorization from './examples/categorization';
import * as stepper from './examples/stepper';
Expand Down Expand Up @@ -100,6 +101,7 @@ export {
arrayWithDetail,
arrayWithDetailAndRule,
arrayWithCustomChildLabel,
arrayWithSorting,
categorization,
stepper,
steppershownav,
Expand Down

0 comments on commit f6a6e1b

Please sign in to comment.