Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BOOKINGSG-6107][JH] add optional showCurrentMonthOnly prop for Calendar #545

Merged
merged 4 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/shared/internal-calendar/day-cell/day-cell.style.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ export const Label = styled(Text.H5)<LabelStyleProps>`
return css`
color: ${Color.Neutral[4]};
`;
case "hidden":
return css`
visibility: hidden;
`;
case "available":
default:
return css`
Expand Down
7 changes: 6 additions & 1 deletion src/shared/internal-calendar/day-cell/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ export type CellType =
| "hover-dash"
| "hover-current";

export type LabelType = "available" | "unavailable" | "current" | "selected";
export type LabelType =
| "available"
| "unavailable"
| "current"
| "selected"
| "hidden";

export interface CellStyleProps {
bgLeft?: CellType | undefined;
Expand Down
2 changes: 2 additions & 0 deletions src/shared/internal-calendar/internal-calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const Component = (
selectWithinRange = true,
initialCalendarDate,
numberOfDays,
showActiveMonthDaysOnly = false,
}: InternalCalendarProps,
ref: React.ForwardedRef<InternalCalendarRef>
) => {
Expand Down Expand Up @@ -171,6 +172,7 @@ export const Component = (
maxDate={maxDate}
isNewSelection={selectWithinRange}
allowDisabledSelection={allowDisabledSelection}
showActiveMonthDaysOnly={showActiveMonthDaysOnly}
onSelect={handleDateSelect}
onHover={handleDateHover}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const StandardCalendarDayView = ({
minDate,
maxDate,
allowDisabledSelection,
showActiveMonthDaysOnly,
}: CalendarDayViewProps) => {
// =============================================================================
// CONST, STATE, REF
Expand Down Expand Up @@ -99,6 +100,9 @@ export const StandardCalendarDayView = ({
disabledDates={disabledDates}
allowDisabledSelection={allowDisabledSelection}
isNewSelection={isNewSelection}
showActiveMonthDaysOnly={
showActiveMonthDaysOnly
}
onSelect={handleDayClick}
onHover={handleHoverCell}
/>
Expand Down
11 changes: 10 additions & 1 deletion src/shared/internal-calendar/standard/standard-cell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface Props {
allowDisabledSelection?: boolean | undefined;
isNewSelection?: boolean | undefined;
currentFocus?: FocusType | undefined;
showActiveMonthDaysOnly?: boolean | undefined;
onSelect: (value: Dayjs, disabled: boolean) => void;
onHover: (value: string, disabled: boolean) => void;
}
Expand All @@ -32,6 +33,7 @@ export const StandardCell = ({
disabledDates,
allowDisabledSelection,
isNewSelection,
showActiveMonthDaysOnly,
onSelect,
onHover,
}: Props) => {
Expand Down Expand Up @@ -64,7 +66,9 @@ export const StandardCell = ({
const props: CellStyleProps = {};

if (calendarDate.month() !== date.month()) {
props.labelType = "unavailable";
props.labelType = showActiveMonthDaysOnly
? "hidden"
: "unavailable";
} else if (dayjs().isSame(date, "day") && !disabled) {
props.labelType = "current";
props.circleLeft = "current";
Expand All @@ -89,6 +93,11 @@ export const StandardCell = ({
const isStart = date.isSame(startDate, "day");
const isEnd = date.isSame(endDate, "day");

if (showActiveMonthDaysOnly && calendarDate.month() !== date.month()) {
props.labelType = "hidden";
return props;
}

if ((startDate && isStart) || (endDate && isEnd)) {
props.labelType = "selected";
props.circleLeft = "selected-outline";
Expand Down
2 changes: 2 additions & 0 deletions src/shared/internal-calendar/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export interface CommonCalendarProps {
disabledDates?: string[] | undefined;
/** Specifies if dates normally disabled by `minDate`, `maxDate` and `disabledDates` are still selectable */
allowDisabledSelection?: boolean | undefined;
/** Specifies if the calendar should display only dates for the selected month */
showActiveMonthDaysOnly?: boolean | undefined;
}

// =============================================================================
Expand Down
6 changes: 6 additions & 0 deletions stories/calendar/calendar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ When `allowDisabledSelection` is set, dates are visually disabled, but still sel

<Canvas of={CalendarStories.AllowDisabledSelection} />

<Heading3>Show active month days only</Heading3>

You can use `showActiveMonthDaysOnly` to display only dates for the selected month.

<Canvas of={CalendarStories.ShowActiveMonthDaysOnly} />

<Secondary>Component API</Secondary>

<PropsTable />
10 changes: 10 additions & 0 deletions stories/calendar/calendar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,13 @@ export const AllowDisabledSelection: StoryObj<Component> = {
);
},
};

export const ShowActiveMonthDaysOnly: StoryObj<Component> = {
render: () => {
return (
<FullWidthStoryContainer>
<Calendar showActiveMonthDaysOnly={true} />
</FullWidthStoryContainer>
);
},
};
7 changes: 7 additions & 0 deletions stories/calendar/props-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ const DATA: ApiTableSectionProps[] = [
"Called when there is a change in the current visible month and year",
propTypes: ["(value: YearMonthDisplay) => void"],
},
{
name: "showActiveMonthDaysOnly",
description:
"Specifies if the calendar should display only dates for the selected month",
propTypes: ["boolean"],
defaultValue: `false`,
},
{
name: "onHover",
description: (
Expand Down