Skip to content

Commit c3d0874

Browse files
authored
feat(module:date-picker): support setting status (#7479)
* feat(module:date-picker): support setting status * feat(module:date-picker): add tests
1 parent 44b7fe0 commit c3d0874

7 files changed

Lines changed: 132 additions & 5 deletions

File tree

components/date-picker/date-picker.component.spec.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ import {
1919
dispatchMouseEvent,
2020
typeInElement
2121
} from 'ng-zorro-antd/core/testing';
22-
import { NgStyleInterface } from 'ng-zorro-antd/core/types';
22+
import { ComponentBed, createComponentBed } from 'ng-zorro-antd/core/testing/component-bed';
23+
import { NgStyleInterface, NzStatus } from 'ng-zorro-antd/core/types';
2324
import { NzI18nModule, NzI18nService, NZ_DATE_LOCALE } from 'ng-zorro-antd/i18n';
25+
import { NzIconModule } from 'ng-zorro-antd/icon';
2426

2527
import en_US from '../i18n/languages/en_US';
2628
import { NzDatePickerComponent } from './date-picker.component';
@@ -1185,6 +1187,31 @@ describe('date-fns testing', () => {
11851187
}));
11861188
});
11871189

1190+
describe('status', () => {
1191+
let testBed: ComponentBed<NzTestDatePickerStatusComponent>;
1192+
let fixture: ComponentFixture<NzTestDatePickerStatusComponent>;
1193+
let fixtureInstance: NzTestDatePickerStatusComponent;
1194+
let datePickerElement!: HTMLElement;
1195+
beforeEach(() => {
1196+
testBed = createComponentBed(NzTestDatePickerStatusComponent, { imports: [NzDatePickerModule, NzIconModule] });
1197+
fixture = testBed.fixture;
1198+
fixtureInstance = fixture.componentInstance;
1199+
datePickerElement = fixture.debugElement.query(By.directive(NzDatePickerComponent)).nativeElement;
1200+
fixture.detectChanges();
1201+
});
1202+
it('should classname correct', () => {
1203+
expect(datePickerElement.classList).toContain('ant-picker-status-error');
1204+
1205+
fixtureInstance.status = 'warning';
1206+
fixture.detectChanges();
1207+
expect(datePickerElement.classList).toContain('ant-picker-status-warning');
1208+
1209+
fixtureInstance.status = '';
1210+
fixture.detectChanges();
1211+
expect(datePickerElement.classList).not.toContain('ant-picker-status-warning');
1212+
});
1213+
});
1214+
11881215
@Component({
11891216
template: `
11901217
<ng-container [ngSwitch]="useSuite">
@@ -1298,3 +1325,10 @@ class NzTestDatePickerComponent {
12981325
firstValue!: Date;
12991326
secondValue!: Date;
13001327
}
1328+
1329+
@Component({
1330+
template: ` <nz-date-picker [nzStatus]="status"></nz-date-picker> `
1331+
})
1332+
class NzTestDatePickerStatusComponent {
1333+
status: NzStatus = 'error';
1334+
}

components/date-picker/date-picker.component.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,16 @@ import { slideMotion } from 'ng-zorro-antd/core/animation';
4848
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
4949
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
5050
import { CandyDate, cloneDate, CompatibleValue, wrongSortOrder } from 'ng-zorro-antd/core/time';
51-
import { BooleanInput, FunctionProp, NzSafeAny, OnChangeType, OnTouchedType } from 'ng-zorro-antd/core/types';
52-
import { InputBoolean, toBoolean, valueFunctionProp } from 'ng-zorro-antd/core/util';
51+
import {
52+
BooleanInput,
53+
FunctionProp,
54+
NgClassInterface,
55+
NzSafeAny,
56+
NzStatus,
57+
OnChangeType,
58+
OnTouchedType
59+
} from 'ng-zorro-antd/core/types';
60+
import { getStatusClassNames, InputBoolean, toBoolean, valueFunctionProp } from 'ng-zorro-antd/core/util';
5361
import {
5462
DateHelperService,
5563
NzDatePickerI18nInterface,
@@ -253,6 +261,10 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
253261
extraFooter?: TemplateRef<NzSafeAny> | string;
254262
dir: Direction = 'ltr';
255263

264+
// status
265+
statusCls: NgClassInterface = {};
266+
hasFeedback: boolean = false;
267+
256268
public panelMode: NzDateMode | NzDateMode[] = 'date';
257269
private destroyed$: Subject<void> = new Subject();
258270
private isCustomPlaceHolder: boolean = false;
@@ -273,6 +285,7 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
273285
@Input() nzPopupStyle: object = POPUP_STYLE_PATCH;
274286
@Input() nzDropdownClassName?: string;
275287
@Input() nzSize: NzDatePickerSizeType = 'default';
288+
@Input() nzStatus?: NzStatus;
276289
@Input() nzFormat!: string;
277290
@Input() nzDateRender?: TemplateRef<NzSafeAny> | string | FunctionProp<TemplateRef<Date> | string>;
278291
@Input() nzDisabledTime?: DisabledTimeFn;
@@ -648,6 +661,7 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
648661
}
649662

650663
ngOnChanges(changes: SimpleChanges): void {
664+
const { nzStatus } = changes;
651665
if (changes.nzPopupStyle) {
652666
// Always assign the popup style patch
653667
this.nzPopupStyle = this.nzPopupStyle ? { ...this.nzPopupStyle, ...POPUP_STYLE_PATCH } : POPUP_STYLE_PATCH;
@@ -675,6 +689,10 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
675689
this.setDefaultPlaceHolder();
676690
this.setModeAndFormat();
677691
}
692+
693+
if (nzStatus) {
694+
this.setStatusStyles();
695+
}
678696
}
679697

680698
ngOnDestroy(): void {
@@ -823,4 +841,17 @@ export class NzDatePickerComponent implements OnInit, OnChanges, OnDestroy, Afte
823841
}
824842
}
825843
}
844+
845+
// status
846+
private setStatusStyles(): void {
847+
// render status if nzStatus is set
848+
this.statusCls = getStatusClassNames(this.prefixCls, this.nzStatus, this.hasFeedback);
849+
Object.keys(this.statusCls).forEach(status => {
850+
if (this.statusCls[status]) {
851+
this.renderer.addClass(this.elementRef.nativeElement, status);
852+
} else {
853+
this.renderer.removeClass(this.elementRef.nativeElement, status);
854+
}
855+
});
856+
}
826857
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
order: 13
3+
title:
4+
zh-CN: 自定义状态
5+
en-US: Status
6+
---
7+
8+
## zh-CN
9+
10+
使用 `nzStatus` 为 DatePicker 添加状态,可选 `error` 或者 `warning`
11+
12+
## en-US
13+
14+
Add status to DatePicker with `nzStatus`, which could be `error` or `warning`.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'nz-demo-date-picker-status',
5+
template: `
6+
<nz-space nzDirection="vertical" style="width: 100%">
7+
<nz-date-picker *nzSpaceItem nzStatus="error" style="width: 100%"></nz-date-picker>
8+
<nz-date-picker *nzSpaceItem nzStatus="warning" style="width: 100%"></nz-date-picker>
9+
<nz-range-picker *nzSpaceItem nzStatus="error" style="width: 100%"></nz-range-picker>
10+
<nz-range-picker *nzSpaceItem nzStatus="warning" style="width: 100%"></nz-range-picker>
11+
</nz-space>
12+
`
13+
})
14+
export class NzDemoDatePickerStatusComponent {}

components/date-picker/doc/index.en-US.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ The following APIs are shared by nz-date-picker, nz-range-picker.
5050
| `[nzPopupStyle]` | to customize the style of the popup calendar | `object` | `{}` | - |
5151
| `[nzRenderExtraFooter]` | render extra footer in panel | `TemplateRef \| string \| (() => TemplateRef \| string)` | - |
5252
| `[nzSize]` | determine the size of the input box, the height of `large` and `small`, are 40px and 24px respectively, while default size is 32px | `'large' \| 'small'` | - | - |
53+
| `[nzStatus]` | Set validation status | `'error' \| 'warning'` | - |
5354
| `[nzSuffixIcon]` | the custom suffix icon | `string` \| `TemplateRef` | - ||
5455
| `[nzBorderless]` | remove the border | `boolean` | `false` | - |
5556
| `[nzInline]` | inline mode | `boolean` | `false` | - |

components/date-picker/doc/index.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ registerLocaleData(zh);
5050
| `[nzPopupStyle]` | 额外的弹出日历样式 | `object` | `{}` | - |
5151
| `[nzRenderExtraFooter]` | 在面板中添加额外的页脚 | `TemplateRef \| string \| (() => TemplateRef \| string)` | - |
5252
| `[nzSize]` | 输入框大小,`large` 高度为 40px,`small` 为 24px,默认是 32px | `'large' \| 'small'` | - | - |
53+
| `[nzStatus]` | 设置校验状态 | `'error' \| 'warning'` | - |
5354
| `[nzSuffixIcon]` | 自定义的后缀图标 | `string` \| `TemplateRef` | - ||
5455
| `[nzBorderless]` | 移除边框 | `boolean` | `false` | - |
5556
| `[nzInline]` | 内联模式 | `boolean` | `false` | - |

components/date-picker/range-picker.component.spec.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
typeInElement
1919
} from 'ng-zorro-antd/core/testing';
2020
import { CandyDate } from 'ng-zorro-antd/core/time';
21-
import { NgStyleInterface } from 'ng-zorro-antd/core/types';
21+
import { NgStyleInterface, NzStatus } from 'ng-zorro-antd/core/types';
22+
import { NzRangePickerComponent } from 'ng-zorro-antd/date-picker/range-picker.component';
2223
import { RangePartType } from 'ng-zorro-antd/date-picker/standard-types';
2324
import {
2425
ENTER_EVENT,
@@ -43,7 +44,7 @@ describe('NzRangePickerComponent', () => {
4344
TestBed.configureTestingModule({
4445
imports: [FormsModule, NoopAnimationsModule, NzDatePickerModule],
4546
providers: [],
46-
declarations: [NzTestRangePickerComponent]
47+
declarations: [NzTestRangePickerComponent, NzTestRangePickerStatusComponent]
4748
});
4849

4950
TestBed.compileComponents();
@@ -984,6 +985,30 @@ describe('NzRangePickerComponent', () => {
984985
}));
985986
});
986987

988+
describe('status', () => {
989+
let fixtureStatus: ComponentFixture<NzTestRangePickerStatusComponent>;
990+
let fixtureStatusInstance: NzTestRangePickerStatusComponent;
991+
let rangePickerElement!: HTMLElement;
992+
beforeEach(() => {
993+
fixtureStatus = TestBed.createComponent(NzTestRangePickerStatusComponent);
994+
fixtureStatusInstance = fixtureStatus.componentInstance;
995+
rangePickerElement = fixtureStatus.debugElement.query(By.directive(NzRangePickerComponent)).nativeElement;
996+
fixtureStatus.detectChanges();
997+
});
998+
999+
it('should classname correct', fakeAsync(() => {
1000+
expect(rangePickerElement.classList).toContain('ant-picker-status-error');
1001+
1002+
fixtureStatusInstance.status = 'warning';
1003+
fixtureStatus.detectChanges();
1004+
expect(rangePickerElement.classList).toContain('ant-picker-status-warning');
1005+
1006+
fixtureStatusInstance.status = '';
1007+
fixtureStatus.detectChanges();
1008+
expect(rangePickerElement.classList).not.toContain('ant-picker-status-warning');
1009+
}));
1010+
});
1011+
9871012
////////////
9881013

9891014
function getCssIndex(part: RangePartType): string {
@@ -1168,3 +1193,10 @@ class NzTestRangePickerComponent {
11681193
// --- Suite 4
11691194
singleValue!: Date;
11701195
}
1196+
1197+
@Component({
1198+
template: ` <nz-range-picker [nzStatus]="status"></nz-range-picker> `
1199+
})
1200+
class NzTestRangePickerStatusComponent {
1201+
status: NzStatus = 'error';
1202+
}

0 commit comments

Comments
 (0)