Skip to content

Commit b49c2d1

Browse files
committed
fix: set locale on demos
1 parent 0ea6ec8 commit b49c2d1

6 files changed

Lines changed: 92 additions & 35 deletions

File tree

apps/www/src/components/demos.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Calendar } from "./ui/calendar.tsx";
2+
import "dayjs/locale/en-gb.js";
3+
import { MultiViewCalendar } from "./ui/calendar-multi-view.tsx";
4+
import { DatePicker } from "./ui/date-picker.tsx";
5+
import { CalendarStartEndSeparate } from "./ui/calendar-start-end-separate.tsx";
6+
7+
export function DemoCalendar() {
8+
return (
9+
<Calendar
10+
mode="single"
11+
locale="en-gb"
12+
className="grow rounded-md border border-border"
13+
/>
14+
);
15+
}
16+
17+
export function DemoDatePicker() {
18+
return (
19+
<DatePicker
20+
mode="single"
21+
locale="en-gb"
22+
className="grow rounded-md border border-border"
23+
/>
24+
);
25+
}
26+
27+
export function DemoMultiView() {
28+
return (
29+
<MultiViewCalendar
30+
locale="en-gb"
31+
mode="range"
32+
className="grow rounded-md border border-border"
33+
/>
34+
);
35+
}
36+
37+
export function DemoSeparateViews() {
38+
return (
39+
<CalendarStartEndSeparate
40+
locale="en-gb"
41+
mode="range"
42+
className="grow rounded-md border border-border"
43+
/>
44+
);
45+
}

apps/www/src/components/ui/calendar-start-end-separate.tsx

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,35 @@
11
"use client";
22

33
import { cn } from "@/lib/utils.ts";
4+
import dayjs from "dayjs";
45
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
56
import * as CalendarPrimitive from "react-composable-calendar";
6-
import {
7-
selectStartDateStrategy,
8-
selectEndDateStrategy,
9-
} from "react-composable-calendar/select-day-strategy";
7+
import { changeAtIndexStrategy } from "react-composable-calendar/select-day-strategy";
108
import { Button } from "./button.tsx";
119

12-
import dayjs from "dayjs";
13-
import "dayjs/locale/en-gb";
14-
15-
dayjs.locale("en-gb");
16-
17-
export type CalendarProps = CalendarPrimitive.RootProps;
18-
1910
export function range(length: number) {
2011
return [...new Array(length)].map((_, i) => i);
2112
}
2213

23-
export function CalendarStartEndSeparate(props: CalendarProps) {
14+
export function CalendarStartEndSeparate(props: CalendarPrimitive.RootProps) {
2415
const { className, ...rest } = props;
2516

2617
return (
2718
<CalendarPrimitive.Root className={cn("max-w-lg p-3", className)} {...rest}>
2819
<div className="mb-2 flex items-center justify-end gap-2">
2920
<div className="grow" />
30-
<CalendarPrimitive.ValueLabel className="text-muted-foreground text-sm " />
21+
<CalendarPrimitive.ValueLabel
22+
fallback="No date selected"
23+
className="text-muted-foreground text-sm "
24+
/>
3125
</div>
3226

3327
<div className="grid grid-cols-2 gap-6">
3428
{range(2).map((index) => (
35-
<CalendarPrimitive.View key={index}>
29+
<CalendarPrimitive.View
30+
defaultValue={dayjs().add(index, "month")}
31+
key={index}
32+
>
3633
<div className="mb-4 flex items-center justify-between">
3734
<CalendarPrimitive.OffsetViewButton asChild offset={-1}>
3835
<Button size="icon" variant="outline" className="size-8">
@@ -52,9 +49,7 @@ export function CalendarStartEndSeparate(props: CalendarProps) {
5249

5350
<CalendarPrimitive.Days className="mb-1 grid grid-cols-7 gap-y-1">
5451
<CalendarPrimitive.Day
55-
selectDayStrategy={
56-
index === 0 ? selectStartDateStrategy : selectEndDateStrategy
57-
}
52+
selectDayStrategy={changeAtIndexStrategy(index)}
5853
className="group relative aspect-square w-full cursor-pointer data-[neighboring]:invisible"
5954
>
6055
<CalendarPrimitive.DayInRange className="absolute top-0 right-0 bottom-0 left-0 bg-foreground/10 data-end:rounded-r-lg data-start:rounded-l-lg" />
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { CodeBlock } from "@/components/ui/code-block.tsx"
2+
import { ExternalLink } from "@/components/ui/external-link"
3+
import { markdownComponents } from "@/components/markdown/components"
4+
import { separateViews } from "@/examples/code-examples.ts";
5+
6+
export const components = markdownComponents;
7+
8+
## Installation
9+
* Setup <ExternalLink text="shadcn/ui" href="https://ui.shadcn.com/docs/installation" />.
10+
* Add <ExternalLink text="Button" href="https://ui.shadcn.com/docs/components/button" />.
11+
* Copy paste this code block into `src/components/ui/calendar.tsx`.
12+
13+
<CodeBlock client:load code={separateViews} />

apps/www/src/pages/index.astro

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
---
22
import { ExternalLink } from "@/components/ui/external-link.tsx";
3-
import * as MultipleViewsDocs from "@/docs/multiple-views.mdx";
43
import * as BasicCalendarDocs from "@/docs/basic-calendar.mdx";
54
import * as DatePickerDocs from "@/docs/date-picker.mdx";
5+
import * as MultipleViewsDocs from "@/docs/multiple-views.mdx";
6+
import * as SeparateViewsDocs from "@/docs/separate-views.mdx";
67
import * as codeExamples from "@/examples/code-examples.ts";
78
import {
89
ExampleSection,
@@ -18,6 +19,7 @@ import { Container } from "../components/ui/container.tsx";
1819
import { DatePicker } from "../components/ui/date-picker.tsx";
1920
import { Typography } from "../components/ui/typography.tsx";
2021
import Layout from "../layouts/Layout.astro";
22+
import * as Demos from "@/components/demos.tsx";
2123
---
2224

2325
<Layout>
@@ -80,11 +82,7 @@ import Layout from "../layouts/Layout.astro";
8082
description="A simple calendar that accepts a single date or a range."
8183
>
8284
<ExampleSectionPreview>
83-
<Calendar
84-
mode="single"
85-
className="grow border border-border rounded-md"
86-
client:load
87-
/>
85+
<Demos.DemoCalendar client:load />
8886
</ExampleSectionPreview>
8987
<ExampleSectionCode>
9088
<BasicCalendarDocs.Content />
@@ -98,7 +96,7 @@ import Layout from "../layouts/Layout.astro";
9896
description="A Date Picker input that utilizes shadcn/ui's Button and Popover."
9997
>
10098
<ExampleSectionPreview>
101-
<DatePicker mode="single" client:load />
99+
<Demos.DemoDatePicker client:load />
102100
</ExampleSectionPreview>
103101
<ExampleSectionCode>
104102
<DatePickerDocs.Content />
@@ -112,11 +110,7 @@ import Layout from "../layouts/Layout.astro";
112110
description="You can have multiple views under the same calendar root."
113111
>
114112
<ExampleSectionPreview>
115-
<MultiViewCalendar
116-
mode="range"
117-
className="grow border border-border rounded-md"
118-
client:load
119-
/>
113+
<Demos.DemoMultiView client:load />
120114
</ExampleSectionPreview>
121115
<ExampleSectionCode>
122116
<MultipleViewsDocs.Content />
@@ -130,14 +124,10 @@ import Layout from "../layouts/Layout.astro";
130124
description="You can even have two views that work independently of each other."
131125
>
132126
<ExampleSectionPreview>
133-
<CalendarStartEndSeparate
134-
mode="range"
135-
className="grow border border-border rounded-md"
136-
client:load
137-
/>
127+
<Demos.DemoSeparateViews client:load />
138128
</ExampleSectionPreview>
139129
<ExampleSectionCode>
140-
<CodeBlock code={codeExamples.datePicker} />
130+
<SeparateViewsDocs.Content />
141131
</ExampleSectionCode>
142132
</ExampleSection>
143133
</div>

packages/react-composable-calendar/src/select-day-strategy.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ export const closestStrategy: SelectDayStrategy = (args) => {
3838
return [currentValue[0], clickedDate];
3939
};
4040

41+
export function changeAtIndexStrategy(index: number): SelectDayStrategy {
42+
return (args: SelectDayStrategyParams) => {
43+
const { currentValue, clickedDate, mode } = args;
44+
if (mode === "single") {
45+
return [clickedDate, null];
46+
}
47+
const [start, end] = currentValue;
48+
if (index === 0) {
49+
return [clickedDate, end];
50+
}
51+
return [start, clickedDate];
52+
};
53+
}
54+
4155
export const selectStartDateStrategy: SelectDayStrategy = (args) => {
4256
const { currentValue, clickedDate } = args;
4357
const [, endDate] = currentValue;

static/header.png

4.04 KB
Loading

0 commit comments

Comments
 (0)