Skip to content

Commit b294981

Browse files
zChangesypzhao
and
ypzhao
authored
fix: range picker year disable (#580)
Co-authored-by: ypzhao <ypzhao@alauda.io>
1 parent 46eaea0 commit b294981

File tree

5 files changed

+89
-14
lines changed

5 files changed

+89
-14
lines changed

.changeset/seven-elephants-nail.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@alauda/ui': patch
3+
---
4+
5+
range picker year disable error

src/date-picker/calendar/header/component.ts

+16-10
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,23 @@ export class CalendarHeaderComponent {
110110
const availValue = (
111111
side === Side.Left ? this._minAvail : this._maxAvail
112112
)?.clone();
113-
if (!availValue) {
114-
return true;
113+
/**
114+
* 对于 range-picker
115+
* 左侧部分 minAvail = minDate, maxAvail = min(maxData, rightAnchor),从而左侧部分的按钮,仅在小于右侧部分时显示
116+
* 右侧部分 maxAvail = maxDate, minAvail = max(minData, leftAnchor),从而左侧部分的按钮,仅在小于右侧部分时显示
117+
*/
118+
if (side === Side.Left) {
119+
return type === DateNavRange.Month
120+
? !this.anchor.subtract(1, 'month').isBefore(availValue, 'month')
121+
: type === DateNavRange.Year
122+
? !this.anchor.subtract(1, 'year').isBefore(availValue, 'year')
123+
: false;
115124
}
116-
// 对于年的判别,2014-5-1至2015-6-1日,仍当展示按钮
117-
const constrainDate = [DateNavRange.Month, DateNavRange.Year].includes(type)
118-
? availValue.add(side === Side.Left ? 1 : -1, type as 'month' | 'year')
119-
: availValue;
120-
return (
121-
this.compareNavValue(type, constrainDate, this.anchor) ===
122-
(side === Side.Left ? -1 : 1)
123-
);
125+
return type === DateNavRange.Month
126+
? !this.anchor.add(1, 'month').isAfter(availValue, 'month')
127+
: type === DateNavRange.Year
128+
? !this.anchor.add(1, 'year').isAfter(availValue, 'year')
129+
: false;
124130
}
125131

126132
// @return isBetween|isEqual:0, isBefore:-1,isAfter:1

src/date-picker/calendar/panel/picker-panel.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ export class PickerPanelComponent implements OnChanges {
9191
get disabledDateFn() {
9292
return composeDisabledDateFn(
9393
date => this.minDate && date.isBefore(this.minDate, 'date'),
94-
date => this.maxDate && date.isAfter(this.maxDate, 'date'),
94+
(date, navRange) =>
95+
this.maxDate &&
96+
(navRange === DateNavRange.Decade
97+
? date.isAfter(this.maxDate, 'year')
98+
: date.isAfter(this.maxDate, 'date')),
9599
this.disabledDate,
96100
);
97101
}
@@ -128,7 +132,7 @@ export class PickerPanelComponent implements OnChanges {
128132
}
129133

130134
// 根据当前数据,计算渲染表格
131-
// eslint-disable-next-line sonarjs/cognitive-complexity
135+
132136
renderPanelData(date: Dayjs, navRange: DateNavRange) {
133137
const value = [];
134138
let colCounts = 0;
@@ -199,8 +203,8 @@ export class PickerPanelComponent implements OnChanges {
199203
this.navRange === DateNavRange.Decade
200204
? value.isSame(dateValue, YEAR)
201205
: this.navRange === DateNavRange.Year
202-
? value.isSame(dateValue, MONTH)
203-
: value.isSame(dateValue, DAY),
206+
? value.isSame(dateValue, MONTH)
207+
: value.isSame(dateValue, DAY),
204208
);
205209
}
206210

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
import { FormControl } from '@angular/forms';
3+
import dayjs from 'dayjs';
4+
5+
@Component({
6+
template: `
7+
<aui-range-picker
8+
[(ngModel)]="range"
9+
[showTime]="false"
10+
[maxDate]="maxDate"
11+
[minDate]="minDate"
12+
></aui-range-picker>
13+
<br />
14+
Form value: {{ control?.value | json }}
15+
`,
16+
changeDetection: ChangeDetectionStrategy.OnPush,
17+
})
18+
export class RangeDisableSomeYearComponent {
19+
range = [dayjs(), dayjs()];
20+
21+
minDate = dayjs().subtract(1, 'year');
22+
23+
maxDate = dayjs();
24+
25+
control = new FormControl({
26+
value: [dayjs(), dayjs().add(3, 'day')],
27+
disabled: true,
28+
});
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
2+
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
3+
import { Meta, StoryObj, moduleMetadata } from '@storybook/angular';
4+
5+
import { RangeDisableSomeYearComponent } from './disable-some-year.component';
6+
7+
import { ButtonModule, DatePickerModule } from '@alauda/ui';
8+
9+
const meta: Meta<RangeDisableSomeYearComponent> = {
10+
title: 'Example/RangePicker',
11+
component: RangeDisableSomeYearComponent,
12+
decorators: [
13+
moduleMetadata({
14+
declarations: [RangeDisableSomeYearComponent],
15+
imports: [
16+
DatePickerModule,
17+
FormsModule,
18+
ReactiveFormsModule,
19+
ButtonModule,
20+
BrowserAnimationsModule,
21+
],
22+
}),
23+
],
24+
};
25+
26+
export default meta;
27+
type Story = StoryObj<RangeDisableSomeYearComponent>;
28+
29+
export const DisableSomeYear: Story = {
30+
name: 'Disable some Year',
31+
};

0 commit comments

Comments
 (0)