Skip to content

Commit

Permalink
Fixes for nested array.
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastianStehle committed Oct 11, 2018
1 parent d646e0b commit 0f77fa5
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 46 deletions.
6 changes: 6 additions & 0 deletions NuGet.Config
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
</configuration>
1 change: 1 addition & 0 deletions src/Squidex/app/features/content/declarations.ts
Expand Up @@ -13,6 +13,7 @@ export * from './pages/schemas/schemas-page.component';

export * from './shared/array-editor.component';
export * from './shared/assets-editor.component';
export * from './shared/array-item.component';
export * from './shared/content-item.component';
export * from './shared/content-status.component';
export * from './shared/contents-selector.component';
Expand Down
2 changes: 2 additions & 0 deletions src/Squidex/app/features/content/module.ts
Expand Up @@ -23,6 +23,7 @@ import {

import {
ArrayEditorComponent,
ArrayItemComponent,
AssetsEditorComponent,
ContentFieldComponent,
ContentHistoryComponent,
Expand Down Expand Up @@ -92,6 +93,7 @@ const routes: Routes = [
],
declarations: [
ArrayEditorComponent,
ArrayItemComponent,
AssetsEditorComponent,
ContentFieldComponent,
ContentHistoryComponent,
Expand Down
Expand Up @@ -8,6 +8,7 @@
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, Output, SimpleChanges } from '@angular/core';
import { AbstractControl, FormGroup } from '@angular/forms';
import { Observable } from 'rxjs';
import { map, startWith } from 'rxjs/operators';

import {
AppLanguageDto,
Expand All @@ -17,7 +18,6 @@ import {
RootFieldDto,
Types
} from '@app/shared';
import { map, startWith } from 'rxjs/operators';

@Component({
selector: 'sqx-content-field',
Expand Down
27 changes: 10 additions & 17 deletions src/Squidex/app/features/content/shared/array-editor.component.html
@@ -1,20 +1,13 @@
<div class="array-container" *ngIf="arrayControl.controls.length > 0">
<div class="array-item" *ngFor="let nestedForm of arrayControl.controls; let i = index" [class.invalid]="nestedForm.invalid">
<button type="button" class="btn btn-link btn-danger array-item-remove" (click)="removeItem(i); $event.preventDefault()">
<i class="icon-bin2"></i>
</button>

<div class="form-group" *ngFor="let nestedField of field.nested">
<ng-container *ngIf="nestedForm.get(nestedField.name); let nestedFieldForm">
<sqx-field-editor
[form]="form"
[field]="nestedField"
[language]="language"
[languages]="languages"
[control]="nestedFieldForm">
</sqx-field-editor>
</ng-container>
</div>
<div class="container" *ngIf="arrayControl.controls.length > 0">
<div class="item" *ngFor="let itemForm of arrayControl.controls; let i = index">
<sqx-array-item
[form]="form"
[field]="field"
[itemForm]="itemForm"
[language]="language"
[languages]="languages"
(removing)="removeItem(i)">
</sqx-array-item>
</div>
</div>

Expand Down
37 changes: 11 additions & 26 deletions src/Squidex/app/features/content/shared/array-editor.component.scss
@@ -1,34 +1,19 @@
@import '_vars';
@import '_mixins';

.array {
&-container {
background: $color-border;
padding: 1rem;
position: relative;
margin-bottom: 1rem;
}

&-item {
& {
background: $panel-light-background;
border: 1px solid darken($color-border, 5%);
border-left-width: 4px;
padding: 1rem;
position: relative;
margin-bottom: 1rem;
}
.container {
background: $color-border;
padding: 1rem;
position: relative;
margin-bottom: 1rem;
}

&:last-child {
margin-bottom: 0;
}
.item {
& {
margin-bottom: 1rem;
}

&-item-remove {
@include absolute(.5rem, .5rem, auto, auto);
&:last-child {
margin-bottom: 0;
}
}

.invalid {
border-left-color: $color-theme-error;
}
Expand Up @@ -5,7 +5,7 @@
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/

import { Component, Input } from '@angular/core';
import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
import { FormArray } from '@angular/forms';

import {
Expand All @@ -18,7 +18,8 @@ import {
@Component({
selector: 'sqx-array-editor',
styleUrls: ['./array-editor.component.scss'],
templateUrl: './array-editor.component.html'
templateUrl: './array-editor.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ArrayEditorComponent {
@Input()
Expand Down
15 changes: 15 additions & 0 deletions src/Squidex/app/features/content/shared/array-item.component.html
@@ -0,0 +1,15 @@
<div class="item" [class.invalid]="isInvalid | async">
<button type="button" class="btn btn-link btn-danger item-remove" (click)="removing.emit(); $event.preventDefault()">
<i class="icon-bin2"></i>
</button>

<div class="form-group" *ngFor="let fieldControl of fieldControls">
<sqx-field-editor
[form]="form"
[field]="fieldControl.field"
[language]="language"
[languages]="languages"
[control]="fieldControl.control">
</sqx-field-editor>
</div>
</div>
21 changes: 21 additions & 0 deletions src/Squidex/app/features/content/shared/array-item.component.scss
@@ -0,0 +1,21 @@
@import '_vars';
@import '_mixins';


.item {
& {
background: $panel-light-background;
border: 1px solid darken($color-border, 5%);
border-left-width: 4px;
padding: 1rem;
position: relative;
}

&-remove {
@include absolute(.5rem, .5rem, auto, auto);
}
}

.invalid {
border-left-color: $color-theme-error;
}
59 changes: 59 additions & 0 deletions src/Squidex/app/features/content/shared/array-item.component.ts
@@ -0,0 +1,59 @@
/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/

import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, Output, SimpleChanges } from '@angular/core';
import { AbstractControl, FormGroup } from '@angular/forms';
import { Observable } from 'rxjs';
import { map, startWith } from 'rxjs/operators';

import {
AppLanguageDto,
EditContentForm,
FieldDto,
ImmutableArray,
RootFieldDto
} from '@app/shared';

@Component({
selector: 'sqx-array-item',
styleUrls: ['./array-item.component.scss'],
templateUrl: './array-item.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ArrayItemComponent implements OnChanges {
@Output()
public removing = new EventEmitter();

@Input()
public form: EditContentForm;

@Input()
public field: RootFieldDto;

@Input()
public itemForm: FormGroup;

@Input()
public language: AppLanguageDto;

@Input()
public languages: ImmutableArray<AppLanguageDto>;

public isInvalid: Observable<boolean>;

public fieldControls: { field: FieldDto, control: AbstractControl }[];

public ngOnChanges(changes: SimpleChanges) {
if (changes['itemForm']) {
this.isInvalid = this.itemForm.statusChanges.pipe(startWith(this.itemForm.invalid), map(x => this.itemForm.invalid));
}

if (changes['itemForm'] || changes['field']) {
this.fieldControls = this.field.nested.map(field => ({ field, control: this.itemForm.get(field.name)! })).filter(x => !!x.control);
}
}
}

0 comments on commit 0f77fa5

Please sign in to comment.