Skip to content

Commit 67cb882

Browse files
committed
Change PlainDateRange shape
1 parent 2e2b3c5 commit 67cb882

File tree

9 files changed

+200
-144
lines changed

9 files changed

+200
-144
lines changed

.changeset/four-geckos-complain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sumup-oss/circuit-ui': major
3+
---
4+
5+
Changed the `PlainDateRange` type from a tuple to an object with `start` and `end` properties. This affects the Calendar component's `selection` prop. Use the new `updatePlainDateRange` helper function to update a date range when a user selects a date.

packages/circuit-ui/components/Calendar/Calendar.mdx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,11 @@ The Calendar component displays a monthly date grid. This is a low-level compone
1212
<Story of={Stories.Base} />
1313
<Props />
1414

15-
## Dependencies
16-
17-
The Calendar component uses the experimental [`Temporal` API](https://tc39.es/proposal-temporal/docs/) which has reached [stage 3](https://github.com/tc39/proposals#stage-3) in the [ECMAScript proposal](https://github.com/tc39/proposal-temporal) process but isn't implemented in [most browsers](https://caniuse.com/temporal) yet. Circuit UI depends on a [polyfill](https://github.com/fullcalendar/temporal-polyfill) which you need to install to use the component in your application:
18-
19-
```bash
20-
# npm
21-
npm install temporal-polyfill
22-
# yarn v1
23-
yarn add temporal-polyfill
24-
```
25-
2615
## Usage
2716

2817
### Selection
2918

30-
Use the `selection` prop to set the currently selected date or date range and the `onSelect` prop to update the selection when a user picks a different date. Use the `minDate` and `maxDate` props to restrict the available date range or use [modifiers](#modifiers) to disable individual days.
19+
Use the `selection` prop to set the currently selected date or date range and the `onSelect` prop to update the selection when a user picks a different date. Use the exported `updatePlainDateRange` function to update a date range when a user selects a date. Use the `minDate` and `maxDate` props to restrict the available date range or use [modifiers](#modifiers) to disable individual days.
3120

3221
<Story of={Stories.Range} />
3322

packages/circuit-ui/components/Calendar/Calendar.spec.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import {
2525
waitFor,
2626
act,
2727
} from '../../util/test-utils.js';
28-
import type { PlainDateRange } from '../../util/date.js';
2928

3029
import { Calendar } from './Calendar.js';
3130

@@ -224,10 +223,10 @@ describe('Calendar', () => {
224223
});
225224

226225
it('should mark the selected date range', () => {
227-
const selection = [
228-
new Temporal.PlainDate(2020, 3, 15),
229-
new Temporal.PlainDate(2020, 3, 25),
230-
] satisfies PlainDateRange;
226+
const selection = {
227+
start: new Temporal.PlainDate(2020, 3, 15),
228+
end: new Temporal.PlainDate(2020, 3, 25),
229+
};
231230
const { container } = render(
232231
<Calendar {...baseProps} selection={selection} />,
233232
);
@@ -236,8 +235,8 @@ describe('Calendar', () => {
236235
expect(selectedDays).toHaveLength(11);
237236

238237
for (
239-
let index = selection[0].day;
240-
index <= selection[1].day;
238+
let index = selection.start.day;
239+
index <= selection.end.day;
241240
index += 1
242241
) {
243242
const selectedDay = screen.getByRole('button', {

packages/circuit-ui/components/Calendar/Calendar.stories.tsx

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ import isChromatic from 'chromatic/isChromatic';
1818
import { Temporal } from 'temporal-polyfill';
1919

2020
import { Stack } from '../../../../.storybook/components/index.js';
21-
import { getTodaysDate, type PlainDateRange } from '../../util/date.js';
21+
import {
22+
getTodaysDate,
23+
updatePlainDateRange,
24+
type PlainDateRange,
25+
} from '../../util/date.js';
2226

2327
import { Calendar, type CalendarProps } from './Calendar.js';
2428

@@ -118,26 +122,17 @@ export const Range = (args: CalendarProps) => {
118122
const [selection, setSelection] = useState(args.selection as PlainDateRange);
119123

120124
const handleSelect = (date: Temporal.PlainDate) => {
121-
setSelection((prevSelection) => {
122-
if (
123-
// Nothing selected yet
124-
prevSelection.length === 0 ||
125-
// Full range already selected
126-
prevSelection.length === 2 ||
127-
// Selected date is before previous start date
128-
Temporal.PlainDate.compare(prevSelection[0], date) > 0
129-
) {
130-
return [date];
131-
}
132-
return [prevSelection[0], date];
133-
});
125+
setSelection((prevSelection) => updatePlainDateRange(prevSelection, date));
134126
};
135127

136128
return <Calendar {...args} selection={selection} onSelect={handleSelect} />;
137129
};
138130

139131
Range.args = {
140132
...Base.args,
141-
selection: [today.subtract({ days: 3 }), today.add({ days: 3 })],
133+
selection: {
134+
start: today.subtract({ days: 3 }),
135+
end: today.add({ days: 3 }),
136+
},
142137
numberOfMonths: 2,
143138
};

0 commit comments

Comments
 (0)