Skip to content

Commit a255dca

Browse files
committed
fix(calendar-web): format render issue
1 parent f485378 commit a255dca

File tree

1 file changed

+57
-38
lines changed

1 file changed

+57
-38
lines changed

packages/pluggableWidgets/calendar-web/src/helpers/CalendarPropsBuilder.ts

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export class CalendarPropsBuilder {
3535
build(): DragAndDropCalendarProps<CalendarEvent> {
3636
const formats = this.buildFormats();
3737
const views = this.buildVisibleViews();
38-
3938
const toolbar =
4039
this.isCustomView && this.toolbarItems && this.toolbarItems.length > 0
4140
? createConfigurableToolbar(this.toolbarItems)
@@ -44,11 +43,17 @@ export class CalendarPropsBuilder {
4443
// Use custom caption for work_week if provided in toolbar items, else default to "Custom"
4544
const workWeekCaption = this.toolbarItems?.find(item => item.itemType === "work_week")?.caption || "Custom";
4645

46+
// Ensure defaultView is actually enabled in views, otherwise pick the first enabled view
47+
const enabledViews = Object.entries(views)
48+
.filter(([_, enabled]) => enabled !== false)
49+
.map(([view]) => view as "day" | "week" | "work_week" | "month" | "agenda");
50+
const safeDefaultView = enabledViews.includes(this.defaultView) ? this.defaultView : enabledViews[0];
51+
4752
return {
4853
components: {
4954
toolbar
5055
},
51-
defaultView: this.defaultView,
56+
defaultView: safeDefaultView,
5257
messages: this.buildMessages(workWeekCaption),
5358
events: this.events,
5459
formats,
@@ -170,63 +175,77 @@ export class CalendarPropsBuilder {
170175

171176
type HeaderFormat = Formats["dayHeaderFormat"];
172177
const applyHeader = (pattern?: string, existing?: HeaderFormat): HeaderFormat | undefined => {
173-
if (!pattern) return existing;
178+
if (!pattern || pattern.trim() === "") return existing;
174179
return (date: Date, _culture: string, loc: DateLocalizer) => loc.format(date, pattern);
175180
};
176181

177-
const dayHeaderPattern = byType.get("day")?.customViewHeaderDayFormat || this.props.topBarDateFormat?.value;
178-
const weekHeaderPattern =
179-
byType.get("week")?.customViewHeaderDayFormat ||
180-
byType.get("work_week")?.customViewHeaderDayFormat ||
181-
this.props.topBarDateFormat?.value;
182-
const monthHeaderPattern =
183-
byType.get("month")?.customViewHeaderDayFormat || this.props.topBarDateFormat?.value;
184-
const agendaHeaderPattern =
185-
byType.get("agenda")?.customViewHeaderDayFormat || this.props.topBarDateFormat?.value;
186-
187-
formats.dayHeaderFormat = applyHeader(dayHeaderPattern, formats.dayHeaderFormat);
188-
formats.dayRangeHeaderFormat = (
189-
range: { start: Date; end: Date },
190-
_culture: string,
191-
loc: DateLocalizer
192-
) => {
193-
const pattern = weekHeaderPattern;
194-
if (pattern) {
195-
return `${loc.format(range.start, pattern)}${loc.format(range.end, pattern)}`;
196-
}
197-
return `${loc.format(range.start, "P")}${loc.format(range.end, "P")}`;
198-
};
199-
formats.monthHeaderFormat = applyHeader(monthHeaderPattern, formats.monthHeaderFormat);
200-
formats.agendaHeaderFormat = (range: { start: Date; end: Date }, _culture: string, loc: DateLocalizer) => {
201-
const pattern = agendaHeaderPattern;
202-
if (pattern) {
203-
return `${loc.format(range.start, pattern)}${loc.format(range.end, pattern)}`;
204-
}
205-
return `${loc.format(range.start, "P")}${loc.format(range.end, "P")}`;
182+
// Helper to get non-empty pattern or fallback
183+
const getPattern = (pattern?: string, fallback?: string): string | undefined => {
184+
const trimmed = pattern?.trim();
185+
return trimmed && trimmed.length > 0 ? trimmed : fallback;
206186
};
207187

188+
const dayHeaderPattern = getPattern(
189+
byType.get("day")?.customViewHeaderDayFormat,
190+
this.props.topBarDateFormat?.value
191+
);
192+
const weekHeaderPattern = getPattern(
193+
byType.get("week")?.customViewHeaderDayFormat || byType.get("work_week")?.customViewHeaderDayFormat,
194+
this.props.topBarDateFormat?.value
195+
);
196+
const monthHeaderPattern = getPattern(
197+
byType.get("month")?.customViewHeaderDayFormat,
198+
this.props.topBarDateFormat?.value
199+
);
200+
const agendaHeaderPattern = getPattern(
201+
byType.get("agenda")?.customViewHeaderDayFormat,
202+
this.props.topBarDateFormat?.value
203+
);
204+
205+
// Only apply if we have a valid pattern
206+
if (dayHeaderPattern) {
207+
formats.dayHeaderFormat = applyHeader(dayHeaderPattern, formats.dayHeaderFormat);
208+
}
209+
if (weekHeaderPattern) {
210+
formats.dayRangeHeaderFormat = (
211+
range: { start: Date; end: Date },
212+
_culture: string,
213+
loc: DateLocalizer
214+
) => `${loc.format(range.start, weekHeaderPattern)}${loc.format(range.end, weekHeaderPattern)}`;
215+
}
216+
if (monthHeaderPattern) {
217+
formats.monthHeaderFormat = applyHeader(monthHeaderPattern, formats.monthHeaderFormat);
218+
}
219+
if (agendaHeaderPattern) {
220+
formats.agendaHeaderFormat = (
221+
range: { start: Date; end: Date },
222+
_culture: string,
223+
loc: DateLocalizer
224+
) => `${loc.format(range.start, agendaHeaderPattern)}${loc.format(range.end, agendaHeaderPattern)}`;
225+
}
226+
208227
// Month day numbers
209-
const monthCellDate = byType.get("month")?.customViewCellDateFormat;
228+
const monthCellDate = getPattern(byType.get("month")?.customViewCellDateFormat);
210229
if (monthCellDate) {
211230
formats.dateFormat = (date: Date, _culture: string, loc: DateLocalizer) =>
212231
loc.format(date, monthCellDate);
213232
}
214233

215234
// Time gutters
216-
const weekTimeGutter = byType.get("week")?.customViewGutterTimeFormat;
217-
const dayTimeGutter = byType.get("day")?.customViewGutterTimeFormat;
218-
const workWeekTimeGutter = byType.get("work_week")?.customViewGutterTimeFormat;
235+
const weekTimeGutter = getPattern(byType.get("week")?.customViewGutterTimeFormat);
236+
const dayTimeGutter = getPattern(byType.get("day")?.customViewGutterTimeFormat);
237+
const workWeekTimeGutter = getPattern(byType.get("work_week")?.customViewGutterTimeFormat);
219238
const chosenTimeGutter = weekTimeGutter || dayTimeGutter || workWeekTimeGutter;
220239
if (chosenTimeGutter) {
221240
formats.timeGutterFormat = (date: Date, _culture: string, loc: DateLocalizer) =>
222241
loc.format(date, chosenTimeGutter);
223242
}
224-
const agendaTime = byType.get("agenda")?.customViewGutterTimeFormat;
243+
const agendaTime = getPattern(byType.get("agenda")?.customViewGutterTimeFormat);
225244
if (agendaTime) {
226245
formats.agendaTimeFormat = (date: Date, _culture: string, loc: DateLocalizer) =>
227246
loc.format(date, agendaTime);
228247
}
229-
const agendaDate = byType.get("agenda")?.customViewGutterDateFormat;
248+
const agendaDate = getPattern(byType.get("agenda")?.customViewGutterDateFormat);
230249
if (agendaDate) {
231250
formats.agendaDateFormat = (date: Date, _culture: string, loc: DateLocalizer) =>
232251
loc.format(date, agendaDate);

0 commit comments

Comments
 (0)