Skip to content

Commit

Permalink
fix(form): fix reset value invalid of time widget
Browse files Browse the repository at this point in the history
  • Loading branch information
cipchk committed May 6, 2018
1 parent 3fd3695 commit a729418
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
7 changes: 4 additions & 3 deletions packages/form/src/widgets/time/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ type: Widgets
- 格式化分为:**数据格式化**表示表单数据和**显示格式化**显示数据
- 所有格式化单位,参考 [date-fns format](https://date-fns.org/v1.29.0/docs/format)(国内镜像:[moment format](http://momentjs.cn/docs/#/displaying/format/)
- 指定 `schema.format` 则必须遵守 [RFC3339](https://tools.ietf.org/html/rfc3339#section-5.6) 时间格式,否则都视为格式错误,默认的数据格式化:
- `time``full-time` 默认 `HH:mm:ss`
- `time``full-time` 默认 `HH:mm:ss`
- 不指定 `schema.format` 根据 `schema.type` 值按以下规则处理(允许通过 `DelonFormConfig` 替换)数据格式化:
- `string` 默认 `HH:mm:ss`
- `number` 默认 `x` 13位Unix Timestamp
- `string` 默认 `HH:mm:ss`
- `number` 默认 `x` 13位Unix Timestamp
- 由于 `disabledHours``disabledMinutes``disabledSeconds` 组合导致时间格式被破坏,可能会导致无法正常显示或显示不正确时可以指定一个完整的 `Date` 对象给默认值(`schema.default``formData`

## API

Expand Down
16 changes: 12 additions & 4 deletions packages/form/src/widgets/time/time.widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,18 @@ export class TimeWidget extends ControlWidget implements OnInit {
}

reset(value: any) {
this.displayValue =
value != null && typeof value === 'string' && value.length
? new Date(value)
: null;
if (value instanceof Date) {
this.displayValue = value;
return;
}
let v = value != null && value.toString().length ? new Date(value) : null;

// trying restore full Date format
if (v != null && v.toString() === 'Invalid Date') {
if (value.toString().split(':').length <= 1) value += ':00';
v = new Date(`1970-1-1 ` + value);
}
this.displayValue = v;
}

_change(value: Date) {
Expand Down

0 comments on commit a729418

Please sign in to comment.