Skip to content

Commit

Permalink
fix(dateRangePicker): fix use controlled state (#1490)
Browse files Browse the repository at this point in the history
Co-authored-by: ZhaoChen <ittisennsinn@gmail.com>
  • Loading branch information
itiiss and itiisennsinn committed Nov 19, 2021
1 parent 2953c69 commit 890466e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/date-range-picker/demos/DateRangePicker.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import { Meta, Story } from '@storybook/react/types-6-0';
import { action } from '@storybook/addon-actions';
import { isBefore, startOfToday, subMonths } from 'date-fns';
Expand Down Expand Up @@ -33,6 +33,20 @@ const Template: Story<DateRangePickerProps> = (args) => (
</div>
);

const ControlledTemplate: Story<DateRangePickerProps> = (args) => {
const [TimeRange, setTimeRange] = useState([new Date(), new Date()] as [Date, Date]);
const onSelect = (timeRange: [Date, Date]) => {
setTimeRange(timeRange);
};
return (
<div style={{ width: 280 }}>
<DateRangePicker {...args} value={TimeRange} onSelect={onSelect} />
</div>
);
};

export const ControlledBasic = ControlledTemplate.bind({});

export const Basic = Template.bind({});
Basic.args = {
placeholder: defaultPlaceholder,
Expand Down
2 changes: 1 addition & 1 deletion src/static-date-range-picker/StaticDateRangePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function StaticDateRangePicker({
onSelect: (currentValue) => {
const newValue = mergeDates(selectedValue, currentValue, dateIndex);
onSelect?.(newValue as [Date, Date], dateIndex);
setSelectedValue(newValue);
setSelectedValue(newValue, true);
setDateIndex(1 - dateIndex);
},
}}
Expand Down

1 comment on commit 890466e

@vercel
Copy link

@vercel vercel bot commented on 890466e Nov 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.