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

Range DatePicker's startDate & endDateprops allow undefined #2334

Merged
merged 8 commits into from
Nov 13, 2024

Conversation

r100-stack
Copy link
Member

@r100-stack r100-stack commented Nov 8, 2024

Changes

Fixes #2333. Range DatePicker's startDate and endDate props allow undefined or not passing any value. Additionally, either both have to be passed or neither have to be passed. Passing just one of those two props is not allowed.

But as @mayank99 mentioned, we still recommend users to have two separate DatePickers instead of a range DatePicker. Similar: #1297 (comment).


After this change, I noticed that if a user passes one of the two, it could lead to some unexpected behavior. E.g. just passing startDate but no endDate may cause some unexpected issues:

Demo of the unexpected issues
Demo.mov
import * as React from 'react';
import { DatePicker } from '@itwin/itwinui-react';

interface DateRange {
  startDate: Date;
  endDate: Date;
}

export default function App() {
  const limits: DateRange = {
    startDate: new Date(2024, 10, 3, 15),
    endDate: new Date(2024, 10, 7, 17),
  };

  const [dateRange, setDateRange] = React.useState<DateRange>(limits);

  return (
    <DatePicker
      enableRangeSelect
      startDate={dateRange?.startDate}
      // endDate={dateRange?.endDate}
      showDatesOutsideMonth={false}
      onChange={(startDate, endDate) => {
        console.log('CHANGING', { startDate, endDate });
        setDateRange({ startDate, endDate });
      }}
    />
  );
}

I'm not sure if this behavior is expected. Experimenting with other changes to see how to not get these unexpected changes. Making this PR ready to review before I finish my experimentation to get some more thoughts on this behavior and because it's a small PR. Solved after #2334 (comment)

Testing

  • Confirmed that no errors come up after making these changes to the types.
  • Confirmed that no initial range is shown when no startDate or endDate is passed.
Testing code based on the user's example
import * as React from 'react';
import { DatePicker } from '@itwin/itwinui-react';

interface DateRange {
  startDate: Date;
  endDate: Date;
}

export default function App() {
  const limits: DateRange = {
    startDate: new Date(2024, 10, 3, 15),
    endDate: new Date(2024, 10, 7, 17),
  };

  const [dateRange, setDateRange] = React.useState<DateRange>(limits);

  return (
    <DatePicker
      enableRangeSelect
      startDate={dateRange?.startDate}
      endDate={dateRange?.endDate}
      showDatesOutsideMonth={false}
      onChange={(startDate, endDate) => {
        console.log('CHANGING', { startDate, endDate });
        setDateRange({ startDate, endDate });
      }}
    />
  );
}

Docs

Added changeset

@r100-stack r100-stack self-assigned this Nov 8, 2024
@r100-stack r100-stack marked this pull request as ready for review November 8, 2024 18:15
@r100-stack r100-stack requested a review from a team as a code owner November 8, 2024 18:15
@r100-stack r100-stack requested review from mayank99 and smmr-dn and removed request for a team November 8, 2024 18:15
@r100-stack r100-stack enabled auto-merge (squash) November 13, 2024 18:36
@r100-stack r100-stack merged commit 455994a into main Nov 13, 2024
18 checks passed
@r100-stack r100-stack deleted the r/range-date-picker-undefined-start-end-date branch November 13, 2024 18:37
@imodeljs-admin imodeljs-admin mentioned this pull request Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DataPicker] Allow for passing undefined startDate, endDate props when enableRangeSelect is set to true
3 participants