Skip to content

Commit 1251f90

Browse files
authored
Change PlainDateRange to an object (#2741)
1 parent 4f11234 commit 1251f90

File tree

11 files changed

+211
-166
lines changed

11 files changed

+211
-166
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.

docs/introduction/2-getting-started.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ npm install @sumup-oss/circuit-ui
3030
yarn add @sumup-oss/circuit-ui
3131
```
3232

33-
Circuit UI relies on some mandatory peer dependencies, namely [@sumup-oss/design-tokens](https://www.npmjs.com/package/@sumup-oss/design-tokens), [@sumup-oss/icons](https://www.npmjs.com/package/@sumup-oss/icons), [@sumup-oss/intl](https://www.npmjs.com/package/@sumup-oss/intl), and [React](https://reactjs.org/). You should install them with the following command:
33+
Circuit UI relies on some mandatory peer dependencies, namely [@sumup-oss/design-tokens](https://www.npmjs.com/package/@sumup-oss/design-tokens), [@sumup-oss/icons](https://www.npmjs.com/package/@sumup-oss/icons), [@sumup-oss/intl](https://www.npmjs.com/package/@sumup-oss/intl), [React](https://reactjs.org/), and [temporal-polyfill](https://www.npmjs.com/package/temporal-polyfill). You should install them with the following command:
3434

3535
```sh
3636
# With npm:
37-
npm install --save @sumup-oss/design-tokens @sumup-oss/icons @sumup-oss/intl react react-dom
37+
npm install --save @sumup-oss/design-tokens @sumup-oss/icons @sumup-oss/intl react react-dom temporal-polyfill
3838
# With yarn v1
39-
yarn add @sumup-oss/design-tokens @sumup-oss/icons @sumup-oss/intl react react-dom
39+
yarn add @sumup-oss/design-tokens @sumup-oss/icons @sumup-oss/intl react react-dom temporal-polyfill
4040
```
4141

4242
We also recommend installing and configuring [`@sumup-oss/eslint-plugin-circuit-ui`](Packages/eslint-plugin-circuit-ui/Docs) and [`@sumup-oss/stylelint-plugin-circuit-ui`](Packages/stylelint-plugin-circuit-ui/Docs). The plugins will lint [Circuit UI custom properties](Features/Theme/Docs) and include codemods for Circuit UI breaking changes.

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.module.css

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
.months {
2121
display: flex;
22+
isolation: isolate;
2223
}
2324

2425
.month:not(:last-child) {
@@ -97,7 +98,7 @@
9798
touch-action: manipulation;
9899
cursor: pointer;
99100
background: none;
100-
border: 0;
101+
border: 1px solid transparent;
101102
border-radius: var(--cui-border-radius-circle);
102103
}
103104

@@ -114,17 +115,17 @@
114115
}
115116

116117
.day[aria-current="date"] {
117-
border: 1px solid var(--cui-border-normal);
118+
border-color: var(--cui-border-normal);
118119
}
119120

120121
.day:hover {
121122
background: var(--cui-bg-normal-hovered);
122-
border: 1px solid var(--cui-border-strong-hovered);
123+
border-color: var(--cui-border-strong-hovered);
123124
}
124125

125126
.day:active {
126127
background: var(--cui-bg-normal-pressed);
127-
border: 1px solid var(--cui-border-strong-pressed);
128+
border-color: var(--cui-border-strong-pressed);
128129
}
129130

130131
/* Selected */
@@ -203,7 +204,7 @@ td:not(:last-of-type) .range-end.last-day::before {
203204
}
204205

205206
.day[aria-current="date"][aria-disabled="true"] {
206-
border: 1px solid var(--cui-border-normal-disabled);
207+
border-color: var(--cui-border-normal-disabled);
207208
}
208209

209210
.day[aria-disabled="true"].selected,

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)