mirror of
https://github.com/shadcn-ui/ui.git
synced 2026-07-08 06:28:37 +00:00
86 lines
1.7 KiB
Plaintext
86 lines
1.7 KiB
Plaintext
---
|
|
title: Calendar
|
|
description: A date field component that allows users to enter and edit date.
|
|
component: true
|
|
---
|
|
|
|
<ComponentExample src="/components/examples/calendar/demo.tsx">
|
|
<CalendarDemo />
|
|
</ComponentExample>
|
|
|
|
## About
|
|
|
|
The `Calendar` component is built on top of [React DayPicker](https://react-day-picker.js.org).
|
|
|
|
## Installation
|
|
|
|
```bash
|
|
npx shadcn-ui add calendar
|
|
```
|
|
|
|
<Accordion type="single" collapsible>
|
|
<AccordionItem value="manual-installation">
|
|
<AccordionTrigger>Manual Installation</AccordionTrigger>
|
|
<AccordionContent>
|
|
|
|
1. Install the `react-day-picker` and `date-fns` packages:
|
|
|
|
```bash
|
|
npm install react-day-picker date-fns
|
|
```
|
|
|
|
2. Copy and paste the following code into your project.
|
|
|
|
<ComponentSource src="/components/ui/calendar.tsx" />
|
|
|
|
<Callout>
|
|
This is the `<Calendar />` primitive. You can place it in a file at
|
|
`components/ui/calendar.tsx`.
|
|
</Callout>
|
|
|
|
</AccordionContent>
|
|
|
|
</AccordionItem>
|
|
</Accordion>
|
|
|
|
## Usage
|
|
|
|
```tsx
|
|
import { Calendar } from "@/components/ui/calendar"
|
|
```
|
|
|
|
```tsx
|
|
const [date, setDate] = React.useState<Date | undefined>(new Date())
|
|
|
|
return (
|
|
<Calendar
|
|
mode="single"
|
|
selected={date}
|
|
onSelect={setDate}
|
|
className="rounded-md border"
|
|
/>
|
|
)
|
|
```
|
|
|
|
See the [React DayPicker](https://react-day-picker.js.org) documentation for more information.
|
|
|
|
## Examples
|
|
|
|
### Date Picker
|
|
|
|
<ComponentExample src="/components/examples/calendar/date-picker.tsx">
|
|
<CalendarDatePicker />
|
|
</ComponentExample>
|
|
|
|
### Date Range Picker
|
|
|
|
<ComponentExample src="/components/examples/calendar/date-range-picker.tsx">
|
|
<CalendarDateRangePicker />
|
|
</ComponentExample>
|
|
|
|
## With Presets
|
|
|
|
<ComponentExample src="/components/examples/calendar/with-presets.tsx">
|
|
<CalendarDatePickerWithPresets />
|
|
</ComponentExample>
|