Skip to content

Commit

Permalink
fix(module:sf): shoule trigger detect change when reset value (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk authored Dec 21, 2018
1 parent 6c8c2d1 commit ffd365b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/form/spec/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,14 @@ export class SFPage {
return path.startsWith('/') ? path : '/' + path;
}

getValue(path: string): any {
path = this.fixPath(path);
return this.comp.getValue(path);
}

setValue(path: string, value: any): this {
path = this.fixPath(path);
const property = this.comp.rootProperty.searchProperty(path);
expect(property).not.toBeNull(`can't found ${path}`);
property.widget.setValue(value);
this.comp.setValue(path, value);
return this;
}

Expand Down
9 changes: 9 additions & 0 deletions packages/form/src/widgets/date/date.widget.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ describe('form: widget: date', () => {
expect(format(comp.value)).toBe(format(time));
});
});
it('should be set value', () => {
const s: SFSchema = {
properties: { a: { type: 'string', format: 'date-time', ui: { widget } } },
};
page.newSchema(s)
.checkValue('a', null)
.setValue('a', new Date());
expect(page.getValue('a') instanceof Date).toBe(true);
});
});

describe('#mode', () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/form/src/widgets/date/date.widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,19 @@ export class DateWidget extends ControlWidget implements OnInit {
};
}

private compCd() {
// TODO: removed after nz-datepick support OnPush mode
setTimeout(() => this.detectChanges());
}

reset(value: SFValue) {
value = this.toDate(value);
if (this.flatRange) {
this.displayValue = value == null ? [] : [value, this.toDate(this.endProperty.formData)];
} else {
this.displayValue = value;
}
this.compCd();
}

_change(value: Date | Date[]) {
Expand Down
7 changes: 7 additions & 0 deletions packages/form/src/widgets/time/time.widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@ export class TimeWidget extends ControlWidget implements OnInit {
};
}

private compCd() {
// TODO: removed after nz-datepick support OnPush mode
setTimeout(() => this.detectChanges());
}

reset(value: SFValue) {
if (value instanceof Date) {
this.displayValue = value;
this.compCd();
return;
}
let v = value != null && value.toString().length ? new Date(value) : null;
Expand All @@ -70,6 +76,7 @@ export class TimeWidget extends ControlWidget implements OnInit {
v = new Date(`1970-1-1 ` + value);
}
this.displayValue = v;
this.compCd();
}

_change(value: Date) {
Expand Down

0 comments on commit ffd365b

Please sign in to comment.