날짜 선택기

날짜 선택기 (Date Picker)

날짜 선택기는 팝오버 안에서 날짜를 고르는 컴포넌트예요. 단일 날짜, 기간, 프리셋까지 폭넓게 지원해서 폼에서 날짜를 입력받을 때 정말 편리하죠. <Popover />와 <Calendar />를 조합해서 만들어졌어요.

출처: 문서

본문

설치하기

날짜 선택기는 <Popover />와 <Calendar /> 컴포넌트를 조합해서 만들어졌어요.

Popover와 Calendar 컴포넌트의 설치 안내를 참고해 주세요.

사용하기

"use client"

import * as React from "react"
import { format } from "date-fns"
import { Calendar as CalendarIcon } from "lucide-react"

import { cn } from "@/lib/utils"
import { Button } from "@/components/ui/button"
import { Calendar } from "@/components/ui/calendar"
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover"

export function DatePickerDemo() {
  const [date, setDate] = React.useState<Date>()

  return (
    <Popover>
      <PopoverTrigger asChild>
        <Button
          variant={"outline"}
          className={cn(
            "w-[280px] justify-start text-left font-normal",
            !date && "text-muted-foreground"
          )}
        >
          <CalendarIcon className="mr-2 h-4 w-4" />
          {date ? format(date, "PPP") : <span>Pick a date</span>}
        </Button>
      </PopoverTrigger>
      <PopoverContent className="w-auto p-0">
        <Calendar
          mode="single"
          selected={date}
          onSelect={setDate}
          initialFocus
        />
      </PopoverContent>
    </Popover>
  )
}

더 자세한 내용은 React DayPicker 문서를 참고해 주세요.

예시

Date Picker (기본)

Date Range Picker (기간 선택)

With Presets (프리셋)

Form (폼)

더 알아보기 (Learn more)

  • React DayPicker — 날짜 선택기의 기반이 되는 캘린더/날짜 라이브러리예요.