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

feat(datepicker): added showSelectedCount for the component #5227

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
11 changes: 10 additions & 1 deletion src/date_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,21 @@ export function safeDateRangeFormat(
*/
export function safeMultipleDatesFormat(
dates: Date[],
props: { dateFormat: string | string[]; locale?: Locale },
props: {
dateFormat: string | string[];
locale?: Locale;
showSelectedCount?: boolean;
},
): string {
if (!dates?.length) {
return "";
}

if (props?.showSelectedCount === false) {
const formattedAllDates = dates.map((date) => safeDateFormat(date, props));
return formattedAllDates.join(", ");
}

const formattedFirstDate = dates[0] ? safeDateFormat(dates[0], props) : "";
if (dates.length === 1) {
return formattedFirstDate;
Expand Down
12 changes: 10 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ export type DatePickerProps = OmitUnion<
| {
selectsRange?: never;
selectsMultiple?: never;
showSelectedCount?: never;
onChange?: (
date: Date | null,
event?:
Expand All @@ -216,6 +217,7 @@ export type DatePickerProps = OmitUnion<
| {
selectsRange: true;
selectsMultiple?: never;
showSelectedCount?: never;
onChange?: (
date: [Date | null, Date | null],
event?:
Expand All @@ -226,6 +228,7 @@ export type DatePickerProps = OmitUnion<
| {
selectsRange?: never;
selectsMultiple: true;
showSelectedCount?: boolean;
onChange?: (
date: Date[] | null,
event?:
Expand Down Expand Up @@ -301,6 +304,7 @@ export default class DatePicker extends Component<
calendarStartDay: undefined,
toggleCalendarOnIconClick: false,
usePointerEvent: false,
showSelectedCount: true,
};
}

Expand Down Expand Up @@ -1337,8 +1341,11 @@ export default class DatePicker extends Component<

const customInput = this.props.customInput || <input type="text" />;
const customInputRef = this.props.customInputRef || "ref";
const { dateFormat = DatePicker.defaultProps.dateFormat, locale } =
this.props;
const {
dateFormat = DatePicker.defaultProps.dateFormat,
locale,
showSelectedCount = DatePicker.defaultProps.showSelectedCount,
} = this.props;
const inputValue =
typeof this.props.value === "string"
? this.props.value
Expand All @@ -1353,6 +1360,7 @@ export default class DatePicker extends Component<
? safeMultipleDatesFormat(this.props.selectedDates ?? [], {
dateFormat,
locale,
showSelectedCount,
})
: safeDateFormat(this.props.selected, {
dateFormat,
Expand Down
28 changes: 28 additions & 0 deletions src/test/date_utils_test.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
getMidnightDate,
registerLocale,
isMonthYearDisabled,
safeMultipleDatesFormat,
} from "../date_utils";

registerLocale("pt-BR", ptBR);
Expand Down Expand Up @@ -1230,6 +1231,33 @@ describe("date_utils", () => {
});
});

describe("safeMultipleDatesFormat", () => {
const props = {
dateFormat: "MM/dd/yyyy",
locale: "en",
};

const dates = [
new Date("2024-11-14 00:00:00"),
new Date("2024-11-15 00:00:00"),
new Date("2024-11-16 00:00:00"),
];

it("should return a blank string when the dates array is null", () => {
expect(safeMultipleDatesFormat([], props)).toBe("");
});

it("should return the correct count when multiple dates are selected", () => {
expect(safeMultipleDatesFormat(dates, props)).toBe("11/14/2024 (+2)");
});

it("should return each selected date when showSelectedCount is false", () => {
expect(
safeMultipleDatesFormat(dates, { ...props, showSelectedCount: false }),
).toBe("11/14/2024, 11/15/2024, 11/16/2024");
});
});

describe("getHolidaysMap", () => {
it("should return a map of dateClasses", () => {
const holidayDates = [
Expand Down
1 change: 1 addition & 0 deletions src/test/min_time_test.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const DatePickerWithState = (
| "dateFormat"
| "selectsRange"
| "selectsMultiple"
| "showSelectedCount"
| "onSelect"
>,
) => {
Expand Down
Loading