Skip to content

Commit

Permalink
fix(module:sf:checkbox): fix invalid indeterminate status (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed Feb 15, 2019
1 parent 3a6d900 commit 2b07901
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 5 deletions.
26 changes: 26 additions & 0 deletions packages/form/docs/error.md
Expand Up @@ -128,3 +128,29 @@ schema: SFSchema = {
## 视觉

可以通过设置 `DelonFormConfig.onlyVisual``ui.onlyVisual` 属性控制只展示错误视觉不显示错误文本。

## Debug

JSON Schema 对格式有严格的要求,例如日期格式必须遵守 [RFC3339](https://tools.ietf.org/html/rfc3339#section-5.6) 时间格式:

```ts
{
properties: {
time: {
type: 'string',
ui: { widget: 'date', mode: 'range' },
title: 'Date',
format: 'YYYY-MM-DD HH:mm:ss'
}
},
ui: {
debug: true
}
}
```

其中 `format` 是一个错误时间格式,当指定 `debug: true` 时,会在控制台接收到详细的校验错误描述:

```
Error: unknown format "YYYY-MM-DD HH:mm:ss" is used in schema at path "#/properties/time"
```
20 changes: 20 additions & 0 deletions packages/form/spec/form.spec.ts
Expand Up @@ -106,6 +106,26 @@ describe('form: component', () => {
page.setValue('/name', 'a');
expect(console.warn).toHaveBeenCalled();
});

it('should be console debug informations when ajv throw error', () => {
spyOn(console, 'warn');
expect(console.warn).not.toHaveBeenCalled();
context.schema = {
properties: {
time: {
type: 'string',
ui: { widget: 'date', mode: 'range' },
title: 'Date',
format: 'YYYY-MM-DD HH:mm:ss',
},
},
ui: {
debug: true,
},
};
fixture.detectChanges();
expect(console.warn).toHaveBeenCalled();
});
});

describe('[button]', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/form/src/model/form.property.ts
Expand Up @@ -40,6 +40,7 @@ export abstract class FormProperty {
this.ui = ui;
this.schemaValidator = schemaValidatorFactory.createValidatorFn(schema, {
ingoreKeywords: this.ui.ingoreKeywords as string[],
debug: (ui as SFUISchemaItem)!.debug,
});
this.formData = formData || schema.default;
this._parent = parent;
Expand Down
9 changes: 7 additions & 2 deletions packages/form/src/validator.factory.ts
Expand Up @@ -3,14 +3,16 @@ import { DelonFormConfig } from './config';
import { ErrorData } from './errors';
import { SFValue } from './interface';
import { SFSchema } from './schema';
import { SFUISchemaItem } from './schema/ui';
import { di } from './utils';

// tslint:disable-next-line:no-any
declare var Ajv: any;

export abstract class SchemaValidatorFactory {
abstract createValidatorFn(
schema: SFSchema,
extraOptions: { ingoreKeywords: string[] },
extraOptions: { ingoreKeywords: string[], debug: boolean },
): (value: SFSchema) => ErrorData[];
}

Expand All @@ -37,7 +39,7 @@ export class AjvSchemaValidatorFactory extends SchemaValidatorFactory {

createValidatorFn(
schema: SFSchema,
extraOptions: { ingoreKeywords: string[] },
extraOptions: { ingoreKeywords: string[], debug: boolean },
): (value: SFValue) => ErrorData[] {
const ingoreKeywords: string[] = []
.concat(this.options.ingoreKeywords)
Expand All @@ -49,6 +51,9 @@ export class AjvSchemaValidatorFactory extends SchemaValidatorFactory {
} catch (e) {
// swallow errors thrown in ajv due to invalid schemas, these
// still get displayed
if (extraOptions.debug) {
console.warn(e);
}
}
let errors = this.ajv.errors;
if (this.options && ingoreKeywords && errors) {
Expand Down
2 changes: 1 addition & 1 deletion packages/form/src/widgets/checkbox/checkbox.widget.html
@@ -1,7 +1,7 @@
<ng-template #all>
<label *ngIf="ui.checkAll"
nz-checkbox
class="mr-sm"
class="sf__checkbox-all mr-sm"
[(ngModel)]="allChecked"
[nzIndeterminate]="indeterminate"
(click)="onAllChecked($event)">{{ ui.checkAllText || l.checkAllText }}</label>
Expand Down
24 changes: 23 additions & 1 deletion packages/form/src/widgets/checkbox/checkbox.widget.spec.ts
Expand Up @@ -84,12 +84,34 @@ describe('form: widget: checkbox', () => {
checkAllBtn.click();
expect(comp.allChecked).toBe(false);
});
it('should be indeterminate when defaut inlcudes value', () => {
page
.newSchema({
properties: {
a: {
type: 'string',
title: 'Mulit',
enum: ['Apple', 'Pear', 'Orange'],
ui: {
widget: 'checkbox',
checkAll: true,
},
default: ['Apple'],
},
},
})
.checkCount('.sf__checkbox-all .ant-checkbox-indeterminate', 1);
});
});

it('should be use nz-checkbox-wrapper when spcify grid_span value', fakeAsync(() => {
const s: SFSchema = {
properties: {
a: { type: 'string', ui: { widget, span: 8, change: jasmine.createSpy() }, enum: ['item1', 'item2'] },
a: {
type: 'string',
ui: { widget, span: 8, change: jasmine.createSpy() },
enum: ['item1', 'item2'],
},
},
};
page
Expand Down
2 changes: 1 addition & 1 deletion packages/form/src/widgets/checkbox/checkbox.widget.ts
Expand Up @@ -59,7 +59,7 @@ export class CheckboxWidget extends ControlWidget {
}

updateAllChecked(): this {
if (this.data.every(item => item.checked === false)) {
if (this.data.every(item => item.checked !== true)) {
this.allChecked = false;
this.indeterminate = false;
} else if (this.data.every(item => item.checked === true)) {
Expand Down

0 comments on commit 2b07901

Please sign in to comment.