Skip to content

Commit

Permalink
Merge pull request #275 from shivbang/X2-6907
Browse files Browse the repository at this point in the history
X2-6907 Setting default as no date for DatePickerPopover
  • Loading branch information
ranjantanya authored Nov 28, 2023
2 parents e488200 + 602fad8 commit 9cc6943
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
45 changes: 28 additions & 17 deletions src/components/DatePicker/DatePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,27 @@ export const DatePicker = ({
timezoneName = null, // seller timezone (e.g. "America/Los_Angeles") to return correct today date
...rest
}) => {
const initialValue = variant === variants.single ? value : value?.from;
const [currentMonth, setCurrentMonth] = useState(initialValue);
const [startMonth, setStartMonth] = useState(value?.from);
const [endMonth, setEndMonth] = useState(
dayjs(value?.to).isSame(dayjs(value?.from), "month")
? dayjs(value?.from).add(1, "month").toDate()
: value?.to ?? dayjs(value?.from).add(1, "month").toDate(),
);
const initialValue = value ? (variant === variants.single ? value : value.from) : null;
const [currentMonth, setCurrentMonth] = useState(initialValue ?? dayjs().toDate());
const [startMonth, setStartMonth] = useState(() => {
if (!value || !value.from) {
return new Date();
}

return value.from;
});
const [endMonth, setEndMonth] = useState(() => {
if (!value || !value.to || !value.from) {
return dayjs(new Date()).add(1, "month").toDate();
}

return dayjs(value.to).isSame(dayjs(value.from), "month")
? dayjs(value.from).add(1, "month").toDate()
: value.to;
});
const [rangeName, setRangeName] = useState("");
const isRangeVariant = variant === variants.range;
const isValidValue = value && value.from && value.to;

// Sync internal month state with outside.
useEffect(() => {
Expand Down Expand Up @@ -115,11 +126,11 @@ export const DatePicker = ({

setRangeName("");
if (isRangeVariant) {
if (value?.from && value.to) {
if (isValidValue) {
// This allows us to easily select another date range,
// if both dates are selected.
onChange({ from: day, to: null }, options, event);
} else if ((value?.from || value.to).getTime() === day.getTime()) {
} else if (value && (value.from || value.to) && (value.from || value.to).getTime() === day.getTime()) {
const from = dayjs(day).startOf("day").toDate();
const to = dayjs(day).endOf("day").toDate();

Expand All @@ -132,9 +143,10 @@ export const DatePicker = ({
}
};

const CaptionElement = shouldShowYearPicker
? ({ date }) => <MonthYearSelector date={date} currentMonth={currentMonth} onChange={handleMonthChange} />
: undefined;
const CaptionElement =
shouldShowYearPicker && currentMonth
? ({ date }) => <MonthYearSelector date={date} currentMonth={currentMonth} onChange={handleMonthChange} />
: undefined;

const renderDay = (date) => {
const tooltipContent = getTooltip?.(date);
Expand All @@ -161,10 +173,9 @@ export const DatePicker = ({
);
};

const rangeModifier = isRangeVariant ? { start: value?.from, end: value.to } : null;

const rangeModifier = isRangeVariant && isValidValue ? { start: value.from, end: value.to } : null;
// Comparing `from` and `to` dates hides a weird CSS style when you select the same date twice in a date range.
const useDateRangeStyle = isRangeVariant && value?.from?.getTime() !== value.to?.getTime();
const useDateRangeStyle = isRangeVariant && isValidValue && value.from?.getTime() !== value.to?.getTime();
// Return the same value if it is already dayjs object or has range variant otherwise format it to dayJs object
const selectedDays = value && (dayjs.isDayjs(value) || isRangeVariant ? value : dayjs(value).toDate());

Expand Down Expand Up @@ -225,7 +236,7 @@ export const DatePicker = ({
{components.Footer ? <components.Footer /> : null}

{useDateRangeStyle && shouldShowRelativeRanges && (
<div className="ml-auto w-6/12 pl-5 pr-10 pb-5">
<div className="ml-auto w-6/12 pb-5 pl-5 pr-10">
<RelativeDateRange
value={rangeName}
ranges={ranges}
Expand Down
20 changes: 12 additions & 8 deletions src/components/DatePicker/RangeDatePicker.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ const RangeDatePicker = ({
}) => {
const isStartDateIsTheSameMonth = dayjs(value?.from).isSame(dayjs(value?.to), "month");

const CaptionStartElement = shouldShowYearPicker
? ({ date }) => <MonthYearSelector date={date} currentMonth={startMonth} onChange={handleStartMonthChange} />
: undefined;

const CaptionEndElement = shouldShowYearPicker
? ({ date }) => <MonthYearSelector date={date} currentMonth={endMonth} onChange={handleEndMonthChange} />
: undefined;
const CaptionStartElement =
shouldShowYearPicker && startMonth
? ({ date }) => (
<MonthYearSelector date={date} currentMonth={startMonth} onChange={handleStartMonthChange} />
)
: undefined;

const CaptionEndElement =
shouldShowYearPicker && endMonth
? ({ date }) => <MonthYearSelector date={date} currentMonth={endMonth} onChange={handleEndMonthChange} />
: undefined;

const isDisabledStartDays = (date) => {
if (isFunction(disabledDays)) {
Expand Down Expand Up @@ -133,7 +137,7 @@ const RangeDatePicker = ({
disabledDays={isDisabledStartDays}
navbarElement={NavbarElement}
captionElement={CaptionStartElement}
selectedDays={selectedDays}
selectedDays={[selectedDays?.from, selectedDays]}
renderDay={renderStartDay}
getTooltip={getTooltip}
onDayClick={(day, options, event) => handleDayClick(day, options, event, true)}
Expand Down
8 changes: 3 additions & 5 deletions src/stories/DataDisplay/DateRangePicker.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ export const RelativeDateRanges = () => {
export const DateRangeWithInput = ({ shouldShowRelativeRanges, ranges }) => {
const defaultValue = { from: today, to: dayjs(today).add(7, "days").toDate() };
const [value, setValue] = useState(defaultValue);
const [displayValue, setDisplayValue] = useState(defaultValue);

const handleChange = (newValue, displayValue) => {
const handleChange = (newValue) => {
setValue(newValue);
setDisplayValue(displayValue ?? newValue);
};

return (
Expand All @@ -81,9 +79,9 @@ export const DateRangeWithInput = ({ shouldShowRelativeRanges, ranges }) => {
onChange={handleChange}
>
<div className="w-75 cursor-pointer bg-gray-lighter p-3">
{dayjs(displayValue.from).format("LL")}
{value.from ? dayjs(value.from).format("LL") : "Pending"}
&nbsp;to&nbsp;
{displayValue.to ? dayjs(displayValue.to).format("LL") : "Pending"}
{value.to ? dayjs(value.to).format("LL") : "Pending"}
</div>
</DatePickerPopover>
</div>
Expand Down

0 comments on commit 9cc6943

Please sign in to comment.