Skip to content

Commit 0096396

Browse files
fix: don't constrain day
1 parent 27bed18 commit 0096396

File tree

5 files changed

+91
-82
lines changed

5 files changed

+91
-82
lines changed

packages/@internationalized/date/src/CalendarDate.ts

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,27 @@ export class CalendarDate {
5959
/** The day number within the month. */
6060
public readonly day: number;
6161

62-
constructor(year: number, month: number, day: number);
63-
constructor(era: string, year: number, month: number, day: number);
64-
constructor(calendar: Calendar, year: number, month: number, day: number);
65-
constructor(calendar: Calendar, era: string, year: number, month: number, day: number);
62+
constructor(year: number, month: number, day: number, constrainDay?: boolean);
63+
constructor(era: string, year: number, month: number, day: number, constrainDay?: boolean);
64+
constructor(calendar: Calendar, year: number, month: number, day: number, constrainDay?: boolean);
65+
constructor(calendar: Calendar, era: string, year: number, month: number, day: number, constrainDay?: boolean);
6666
constructor(...args: any[]) {
67-
let [calendar, era, year, month, day] = shiftArgs(args);
67+
let [calendar, era, year, month, day, constrainDay] = shiftArgs(args);
6868
this.calendar = calendar;
6969
this.era = era;
7070
this.year = year;
7171
this.month = month;
7272
this.day = day;
7373

74-
constrain(this);
74+
constrain(this, constrainDay);
7575
}
7676

7777
/** Returns a copy of this date. */
78-
copy(): CalendarDate {
78+
copy(constrainDay?: boolean): CalendarDate {
7979
if (this.era) {
80-
return new CalendarDate(this.calendar, this.era, this.year, this.month, this.day);
80+
return new CalendarDate(this.calendar, this.era, this.year, this.month, this.day, constrainDay);
8181
} else {
82-
return new CalendarDate(this.calendar, this.year, this.month, this.day);
82+
return new CalendarDate(this.calendar, this.year, this.month, this.day, constrainDay);
8383
}
8484
}
8585

@@ -94,16 +94,16 @@ export class CalendarDate {
9494
}
9595

9696
/** Returns a new `CalendarDate` with the given fields set to the provided values. Other fields will be constrained accordingly. */
97-
set(fields: DateFields, ignoreDay?: boolean): CalendarDate {
98-
return set(this, fields, ignoreDay);
97+
set(fields: DateFields, constrainDay?: boolean): CalendarDate {
98+
return set(this, fields, constrainDay);
9999
}
100100

101101
/**
102102
* Returns a new `CalendarDate` with the given field adjusted by a specified amount.
103103
* When the resulting value reaches the limits of the field, it wraps around.
104104
*/
105-
cycle(field: DateField, amount: number, options?: CycleOptions, ignoreDay?: boolean): CalendarDate {
106-
return cycleDate(this, field, amount, options, ignoreDay);
105+
cycle(field: DateField, amount: number, options?: CycleOptions, constrainDay?: boolean): CalendarDate {
106+
return cycleDate(this, field, amount, options, constrainDay);
107107
}
108108

109109
/** Converts the date to a native JavaScript Date object, with the time set to midnight in the given time zone. */
@@ -165,8 +165,8 @@ export class Time {
165165
}
166166

167167
/** Returns a new `Time` with the given fields set to the provided values. Other fields will be constrained accordingly. */
168-
set(fields: TimeFields, ignoreDay?: boolean): Time {
169-
return setTime(this, fields, ignoreDay);
168+
set(fields: TimeFields, constrainDay?: boolean): Time {
169+
return setTime(this, fields, constrainDay);
170170
}
171171

172172
/**
@@ -216,10 +216,10 @@ export class CalendarDateTime {
216216
/** The millisecond in the second. */
217217
public readonly millisecond: number;
218218

219-
constructor(year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number, ignoreDay?: boolean);
220-
constructor(era: string, year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number, ignoreDay?: boolean);
221-
constructor(calendar: Calendar, year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number, ignoreDay?: boolean);
222-
constructor(calendar: Calendar, era: string, year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number, ignoreDay?: boolean);
219+
constructor(year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number, constrainDay?: boolean);
220+
constructor(era: string, year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number, constrainDay?: boolean);
221+
constructor(calendar: Calendar, year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number, constrainDay?: boolean);
222+
constructor(calendar: Calendar, era: string, year: number, month: number, day: number, hour?: number, minute?: number, second?: number, millisecond?: number, constrainDay?: boolean);
223223
constructor(...args: any[]) {
224224
let [calendar, era, year, month, day] = shiftArgs(args);
225225
this.calendar = calendar;
@@ -231,17 +231,17 @@ export class CalendarDateTime {
231231
this.minute = args.shift() || 0;
232232
this.second = args.shift() || 0;
233233
this.millisecond = args.shift() || 0;
234-
const ignoreDay = args.shift() || 0;
234+
const constrainDay = args.shift() || 0;
235235

236-
constrain(this, ignoreDay);
236+
constrain(this, constrainDay);
237237
}
238238

239239
/** Returns a copy of this date. */
240-
copy(ignoreDay?: boolean): CalendarDateTime {
240+
copy(constrainDay?: boolean): CalendarDateTime {
241241
if (this.era) {
242-
return new CalendarDateTime(this.calendar, this.era, this.year, this.month, this.day, this.hour, this.minute, this.second, this.millisecond, ignoreDay);
242+
return new CalendarDateTime(this.calendar, this.era, this.year, this.month, this.day, this.hour, this.minute, this.second, this.millisecond, constrainDay);
243243
} else {
244-
return new CalendarDateTime(this.calendar, this.year, this.month, this.day, this.hour, this.minute, this.second, this.millisecond, ignoreDay);
244+
return new CalendarDateTime(this.calendar, this.year, this.month, this.day, this.hour, this.minute, this.second, this.millisecond, constrainDay);
245245
}
246246
}
247247

@@ -256,21 +256,21 @@ export class CalendarDateTime {
256256
}
257257

258258
/** Returns a new `CalendarDateTime` with the given fields set to the provided values. Other fields will be constrained accordingly. */
259-
set(fields: DateFields & TimeFields, ignoreDay?: boolean): CalendarDateTime {
260-
return set(setTime(this, fields), fields, ignoreDay);
259+
set(fields: DateFields & TimeFields, constrainDay?: boolean): CalendarDateTime {
260+
return set(setTime(this, fields), fields, constrainDay);
261261
}
262262

263263
/**
264264
* Returns a new `CalendarDateTime` with the given field adjusted by a specified amount.
265265
* When the resulting value reaches the limits of the field, it wraps around.
266266
*/
267-
cycle(field: DateField | TimeField, amount: number, options?: CycleTimeOptions, ignoreDay?: boolean): CalendarDateTime {
267+
cycle(field: DateField | TimeField, amount: number, options?: CycleTimeOptions, constrainDay?: boolean): CalendarDateTime {
268268
switch (field) {
269269
case 'era':
270270
case 'year':
271271
case 'month':
272272
case 'day':
273-
return cycleDate(this, field, amount, options, ignoreDay);
273+
return cycleDate(this, field, amount, options, constrainDay);
274274
default:
275275
return cycleTime(this, field, amount, options);
276276
}
@@ -329,10 +329,10 @@ export class ZonedDateTime {
329329
/** The UTC offset for this time, in milliseconds. */
330330
public readonly offset: number;
331331

332-
constructor(year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number, ignoreDay?: boolean);
333-
constructor(era: string, year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number, ignoreDay?: boolean);
334-
constructor(calendar: Calendar, year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number, ignoreDay?: boolean);
335-
constructor(calendar: Calendar, era: string, year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number, ignoreDay?: boolean);
332+
constructor(year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number, constrainDay?: boolean);
333+
constructor(era: string, year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number, constrainDay?: boolean);
334+
constructor(calendar: Calendar, year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number, constrainDay?: boolean);
335+
constructor(calendar: Calendar, era: string, year: number, month: number, day: number, timeZone: string, offset: number, hour?: number, minute?: number, second?: number, millisecond?: number, constrainDay?: boolean);
336336
constructor(...args: any[]) {
337337
let [calendar, era, year, month, day] = shiftArgs(args);
338338
let timeZone = args.shift();
@@ -348,17 +348,17 @@ export class ZonedDateTime {
348348
this.minute = args.shift() || 0;
349349
this.second = args.shift() || 0;
350350
this.millisecond = args.shift() || 0;
351-
const ignoreDay = args.shift() || 0;
351+
const constrainDay = args.shift() || 0;
352352

353-
constrain(this, ignoreDay);
353+
constrain(this, constrainDay);
354354
}
355355

356356
/** Returns a copy of this date. */
357-
copy(ignoreDay?: boolean): ZonedDateTime {
357+
copy(constrainDay?: boolean): ZonedDateTime {
358358
if (this.era) {
359-
return new ZonedDateTime(this.calendar, this.era, this.year, this.month, this.day, this.timeZone, this.offset, this.hour, this.minute, this.second, this.millisecond, ignoreDay);
359+
return new ZonedDateTime(this.calendar, this.era, this.year, this.month, this.day, this.timeZone, this.offset, this.hour, this.minute, this.second, this.millisecond, constrainDay);
360360
} else {
361-
return new ZonedDateTime(this.calendar, this.year, this.month, this.day, this.timeZone, this.offset, this.hour, this.minute, this.second, this.millisecond, ignoreDay);
361+
return new ZonedDateTime(this.calendar, this.year, this.month, this.day, this.timeZone, this.offset, this.hour, this.minute, this.second, this.millisecond, constrainDay);
362362
}
363363
}
364364

@@ -373,16 +373,25 @@ export class ZonedDateTime {
373373
}
374374

375375
/** Returns a new `ZonedDateTime` with the given fields set to the provided values. Other fields will be constrained accordingly. */
376-
set(fields: DateFields & TimeFields, disambiguation?: Disambiguation, ignoreDay?: boolean): ZonedDateTime {
377-
return setZoned(this, fields, disambiguation, ignoreDay);
376+
set(fields: DateFields & TimeFields, disambiguation?: Disambiguation): ZonedDateTime;
377+
set(fields: DateFields & TimeFields, constrainDay?: boolean): ZonedDateTime;
378+
set(...args: any[]) {
379+
let disambiguation , constrainDay = false;
380+
if(args[1] && typeof args[1] === "string") {
381+
disambiguation = args[1]
382+
}else if(args[1] && typeof args[1] === "boolean"){
383+
constrainDay = args[1]
384+
}
385+
return setZoned(this, args[0], disambiguation, constrainDay);
378386
}
379387

388+
380389
/**
381390
* Returns a new `ZonedDateTime` with the given field adjusted by a specified amount.
382391
* When the resulting value reaches the limits of the field, it wraps around.
383392
*/
384-
cycle(field: DateField | TimeField, amount: number, options?: CycleTimeOptions, ignoreDay?: boolean): ZonedDateTime {
385-
return cycleZoned(this, field, amount, options, ignoreDay);
393+
cycle(field: DateField | TimeField, amount: number, options?: CycleTimeOptions, constrainDay?: boolean): ZonedDateTime {
394+
return cycleZoned(this, field, amount, options, constrainDay);
386395
}
387396

388397
/** Converts the date to a native JavaScript Date object. */

packages/@internationalized/date/src/conversion.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export function toTime(dateTime: CalendarDateTime | ZonedDateTime): Time {
259259
}
260260

261261
/** Converts a date from one calendar system to another. */
262-
export function toCalendar<T extends AnyCalendarDate>(date: T, calendar: Calendar): T {
262+
export function toCalendar<T extends AnyCalendarDate>(date: T, calendar: Calendar, constrainDay?: boolean): T {
263263
if (isEqualCalendar(date.calendar, calendar)) {
264264
return date;
265265
}
@@ -271,7 +271,7 @@ export function toCalendar<T extends AnyCalendarDate>(date: T, calendar: Calenda
271271
copy.year = calendarDate.year;
272272
copy.month = calendarDate.month;
273273
copy.day = calendarDate.day;
274-
constrain(copy);
274+
constrain(copy, constrainDay);
275275
return copy;
276276
}
277277

0 commit comments

Comments
 (0)