Browse Source

Should not apply default for null.

pull/664/head
Sebastian 5 years ago
parent
commit
98b555b056
  1. 7
      backend/src/Squidex.Domain.Apps.Core.Operations/DefaultValues/DefaultValueGenerator.cs
  2. 4
      backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/DefaultValues/DefaultValuesTests.cs
  3. 40
      backend/tools/TestSuite/TestSuite.ApiTests/SchemaTests.cs
  4. 2
      frontend/app/features/content/shared/forms/field-editor.component.html
  5. 2
      frontend/app/framework/angular/forms/editors/toggle.component.html
  6. 4
      frontend/app/framework/angular/forms/editors/toggle.component.ts
  7. 34
      frontend/app/framework/angular/forms/indeterminate-value.directive.ts

7
backend/src/Squidex.Domain.Apps.Core.Operations/DefaultValues/DefaultValueGenerator.cs

@ -64,15 +64,10 @@ namespace Squidex.Domain.Apps.Core.DefaultValues
return; return;
} }
if (!fieldData.TryGetValue(partitionKey, out var value) || ShouldApplyDefaultValue(field, value)) if (!fieldData.TryGetValue(partitionKey, out var value))
{ {
fieldData.AddLocalized(partitionKey, defaultValue); fieldData.AddLocalized(partitionKey, defaultValue);
} }
} }
private static bool ShouldApplyDefaultValue(IField field, IJsonValue value)
{
return value.Type == JsonValueType.Null || (field is IField<StringFieldProperties> && value is JsonString s && string.IsNullOrEmpty(s.Value));
}
} }
} }

4
backend/tests/Squidex.Domain.Apps.Core.Tests/Operations/DefaultValues/DefaultValuesTests.cs

@ -62,7 +62,7 @@ namespace Squidex.Domain.Apps.Core.Operations.DefaultValues
} }
[Fact] [Fact]
public void Should_also_enrich_with_default_values_when_string_is_empty() public void Should_not_enrich_with_default_values_when_string_is_empty()
{ {
var data = var data =
new ContentData() new ContentData()
@ -75,7 +75,7 @@ namespace Squidex.Domain.Apps.Core.Operations.DefaultValues
data.GenerateDefaultValues(schema, languagesConfig.ToResolver()); data.GenerateDefaultValues(schema, languagesConfig.ToResolver());
Assert.Equal("en-string", data["my-string"]!["de"].ToString()); Assert.Equal(string.Empty, data["my-string"]!["de"].ToString());
Assert.Equal("en-string", data["my-string"]!["en"].ToString()); Assert.Equal("en-string", data["my-string"]!["en"].ToString());
} }

40
backend/tools/TestSuite/TestSuite.ApiTests/SchemaTests.cs

@ -6,6 +6,7 @@
// ========================================================================== // ==========================================================================
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Squidex.ClientLibrary.Management; using Squidex.ClientLibrary.Management;
using TestSuite.Fixtures; using TestSuite.Fixtures;
@ -46,6 +47,45 @@ namespace TestSuite.ApiTests
Assert.Contains(schemas.Items, x => x.Name == schemaName); Assert.Contains(schemas.Items, x => x.Name == schemaName);
} }
[Fact]
public async Task Should_create_schema_with_checkboxes()
{
var schemaName = $"schema-{Guid.NewGuid()}";
// STEP 1: Create schema
var createRequest = new CreateSchemaDto
{
Name = schemaName,
Fields = new List<UpsertSchemaFieldDto>
{
new UpsertSchemaFieldDto
{
Name = "references",
Partitioning = "invariant",
Properties = new ReferencesFieldPropertiesDto
{
Editor = ReferencesFieldEditor.Checkboxes
}
},
new UpsertSchemaFieldDto
{
Name = "tags",
Partitioning = "invariant",
Properties = new TagsFieldPropertiesDto
{
Editor = TagsFieldEditor.Checkboxes,
AllowedValues = new List<string> { "value1" }
}
}
}
};
var schema = await _.Schemas.PostSchemaAsync(_.AppName, createRequest);
// Should return created schemas with correct name.
Assert.Equal(schemaName, schema.Name);
}
[Fact] [Fact]
public async Task Should_delete_Schema() public async Task Should_delete_Schema()
{ {

2
frontend/app/features/content/shared/forms/field-editor.component.html

@ -39,7 +39,7 @@
</ng-container> </ng-container>
<ng-container *ngSwitchCase="'Checkbox'"> <ng-container *ngSwitchCase="'Checkbox'">
<div class="custom-control custom-checkbox"> <div class="custom-control custom-checkbox">
<input class="custom-control-input" type="checkbox" [formControl]="editorControl" id="{{uniqueId}}" sqxIndeterminateValue> <input class="custom-control-input" type="checkbox" [formControl]="editorControl" id="{{uniqueId}}" sqxIndeterminateValue [threeStates]="!field.properties.isRequired">
<label class="custom-control-label" for="{{uniqueId}}"></label> <label class="custom-control-label" for="{{uniqueId}}"></label>
</div> </div>
</ng-container> </ng-container>

2
frontend/app/framework/angular/forms/editors/toggle.component.html

@ -1,4 +1,4 @@
<div class="toggle-container" (click)="changeState($event)" <div class="toggle-container" (click)="changeState()"
[class.disabled]="snapshot.isDisabled" [class.disabled]="snapshot.isDisabled"
[class.checked]="snapshot.isChecked === true" [class.checked]="snapshot.isChecked === true"
[class.unchecked]="snapshot.isChecked === false"> [class.unchecked]="snapshot.isChecked === false">

4
frontend/app/framework/angular/forms/editors/toggle.component.ts

@ -46,7 +46,7 @@ export class ToggleComponent extends StatefulControlComponent<State, boolean | n
this.next({ isChecked }); this.next({ isChecked });
} }
public changeState(event: MouseEvent) { public changeState() {
const isDisabled = this.snapshot.isDisabled; const isDisabled = this.snapshot.isDisabled;
if (isDisabled) { if (isDisabled) {
@ -55,7 +55,7 @@ export class ToggleComponent extends StatefulControlComponent<State, boolean | n
let isChecked = this.snapshot.isChecked; let isChecked = this.snapshot.isChecked;
if (this.threeStates && (event.ctrlKey || event.shiftKey)) { if (this.threeStates) {
if (isChecked) { if (isChecked) {
isChecked = null; isChecked = null;
} else if (isChecked === null) { } else if (isChecked === null) {

34
frontend/app/framework/angular/forms/indeterminate-value.directive.ts

@ -5,7 +5,7 @@
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/ */
import { Directive, ElementRef, forwardRef, HostListener, Renderer2 } from '@angular/core'; import { Directive, ElementRef, forwardRef, HostListener, Input, Renderer2 } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { Types } from '@app/framework/internal'; import { Types } from '@app/framework/internal';
@ -22,16 +22,38 @@ export const SQX_INDETERMINATE_VALUE_CONTROL_VALUE_ACCESSOR: any = {
export class IndeterminateValueDirective implements ControlValueAccessor { export class IndeterminateValueDirective implements ControlValueAccessor {
private callChange = (_: any) => { /* NOOP */ }; private callChange = (_: any) => { /* NOOP */ };
private callTouched = () => { /* NOOP */ }; private callTouched = () => { /* NOOP */ };
private isChecked: boolean | null;
@Input()
public threeStates = true;
constructor( constructor(
private readonly element: ElementRef, private readonly element: ElementRef<HTMLInputElement>,
private readonly renderer: Renderer2 private readonly renderer: Renderer2
) { ) {
} }
@HostListener('change', ['$event.target.checked']) @HostListener('click')
public onChange(value: any) { public onClick() {
this.callChange(value); let isChecked = this.isChecked;
if (this.threeStates) {
if (isChecked) {
isChecked = null;
} else if (isChecked === null) {
isChecked = false;
} else {
isChecked = true;
}
} else {
isChecked = !(isChecked === true);
}
if (isChecked !== this.isChecked) {
this.callChange(isChecked);
}
this.writeValue(isChecked);
} }
@HostListener('blur') @HostListener('blur')
@ -47,6 +69,8 @@ export class IndeterminateValueDirective implements ControlValueAccessor {
this.renderer.setProperty(this.element.nativeElement, 'indeterminate', false); this.renderer.setProperty(this.element.nativeElement, 'indeterminate', false);
this.renderer.setProperty(this.element.nativeElement, 'checked', obj); this.renderer.setProperty(this.element.nativeElement, 'checked', obj);
} }
this.isChecked = obj;
} }
public setDisabledState(isDisabled: boolean): void { public setDisabledState(isDisabled: boolean): void {

Loading…
Cancel
Save