Skip to content

Commit

Permalink
Range DatePicker's startDate & endDateprops allow undefined (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
r100-stack authored Nov 13, 2024
1 parent 400c14c commit 455994a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/wet-ears-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@itwin/itwinui-react': patch
---

`DatePicker` with `enableRangeSelect` now allows `startDate` and `endDate` to *both* be `undefined` (e.g. when there is no initial range). Passing `Date` to just *one* of them is not allowed.
17 changes: 15 additions & 2 deletions packages/itwinui-react/src/core/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Box,
useId,
useLayoutEffect,
useWarningLogger,
} from '../../utils/index.js';
import type { PolymorphicForwardRefComponent } from '../../utils/index.js';
import { IconButton } from '../Buttons/IconButton.js';
Expand Down Expand Up @@ -166,8 +167,8 @@ export type DateRangePickerProps =
}
| {
enableRangeSelect: true;
startDate: Date;
endDate: Date;
startDate?: Date;
endDate?: Date;
onChange?: (startDate: Date, endDate: Date) => void;
};

Expand Down Expand Up @@ -285,6 +286,18 @@ export const DatePicker = React.forwardRef((props, forwardedRef) => {
...rest
} = props;

const logWarning = useWarningLogger();

if (process.env.NODE_ENV === 'development') {
const onlyOneRangePropPassed =
(!!startDate ? 1 : 0) + (!!endDate ? 1 : 0) === 1;
if (enableRangeSelect && onlyOneRangePropPassed) {
logWarning(
'`DatePicker` with `enableRangeSelect` needs *both* `startDate` and `endDate` to either be `Date` or `undefined`. Passing `Date` to just one of them is not allowed.',
);
}
}

const monthNames = localizedNames?.months ?? defaultMonths;
const shortDays = localizedNames?.shortDays ?? defaultShortDays;
const longDays = localizedNames?.days ?? defaultLongDays;
Expand Down

0 comments on commit 455994a

Please sign in to comment.