本文へ移動
日本語

仕組み

For choosing a single day. The trigger is the same filled field as the text input: calendar button, a real <input>, and a clear button that only hides when empty; the panel is a floating sheet with round day cells and shortcut pills. Values are 'YYYY-MM-DD' strings computed in local time, never through UTC, so min and max are string comparisons. Typed dates in several formats are normalized on Enter or blur; invalid ones restore the old value with a short hint. The panel is absolutely positioned inside the root (no portal) and flips above when there is no room below.

向いている場面

  • Filling in a specific day in a form: departure, delivery or birthday.
  • Filtering by a single day in a filter bar (use size="sm").
  • Dates with an allowed range or blocked days: only the next 30 days, no delivery on holidays.

向かない場面

  • Choosing a span of dates: use a date range picker.
  • Only a month or year: the value here is always a full day.
  • Date and time together: this component has no time.
  • Dates far in the past that people already know, such as a birth year: typing or three dropdowns is faster on phones.

使い方

import { useState } from 'react'
import { DatePicker } from './DatePicker.jsx'

// Compute dates from the local year, month and day; toISOString() is UTC and can be a day off
const pad = (n) => String(n).padStart(2, '0')
const ymd = (d) => `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
const daysLater = (n) => {
  const d = new Date()
  d.setDate(d.getDate() + n)
  return ymd(d)
}

export function DepartureField() {
  const [date, setDate] = useState('')
  return (
    <div>
      <label htmlFor="departure">Departure date</label>
      <DatePicker
        value={date}
        onChange={setDate}
        min={daysLater(0)}
        max={daysLater(30)}
        shortcuts={[{ label: 'Tomorrow', value: () => daysLater(1) }]}
        inputProps={{ id: 'departure', name: 'departure', required: true }}
      />
    </div>
  )
}

プロパティ

プロパティ型既定値説明
valuestring—Controlled value, 'YYYY-MM-DD'. An empty string means none. An invalid date is treated as empty.
defaultValuestring''Initial value when uncontrolled.
onChange(value: string) => void—Called after a pick, a confirmed typed date or a clear. Clearing gives an empty string.
openboolean—Controlled: whether the panel is open.
defaultOpenbooleanfalseUncontrolled: whether it starts open. Starting open does not take focus.
onOpenChange(open: boolean) => void—Called when the panel opens or closes.
size'sm' | 'md' | 'lg''md'Trigger height 36 / 44 / 52px, radius 10 / 12 / 14px, font 14 / 15 / 16px. The panel size does not change.
disabledbooleanfalseDisabled: the field stays filled without its hairline, the text is muted, and it cannot open.
invalidbooleanfalseRed hairline, a faint red fill, a red halo while focused, and aria-invalid on the input. The error text belongs to the surrounding form field.
placeholderstring'YYYY-MM-DD'Placeholder; use it for the format example only.
clearablebooleantrueShows a clear button when there is a value. When false, emptying the input restores the previous value.
editablebooleantrueWhether the date can be typed. On touch screens, false avoids raising the keyboard.
minstring—Earliest selectable date (inclusive), 'YYYY-MM-DD'.
maxstring—Latest selectable date (inclusive), 'YYYY-MM-DD'.
disabledDate(date: string) => boolean—Dates that return true are unavailable: faded, not clickable or focusable, skipped by arrow keys, and announced as unavailable.
weekStartsOn0 | 11First day of the week: 1 Monday, 0 Sunday.
showTodaybooleantrueShows a Today pill in the footer. It is disabled when today is unavailable.
shortcuts{ label: string, value: string | (() => string) }[]—Quick dates as pills after Today in the footer. Write relative dates as functions; they are computed when the panel opens.
onInvalidInput(text, reason: 'format' | 'unavailable') => void—Called when typed text is not a valid date (format) or that day is unavailable. The previous value is already restored.
inputPropsInputHTMLAttributes—Passed to the real <input>: id (for <label htmlFor>), name, required, aria-label, aria-describedby and so on.
labelsobject—Replaces built-in text: weekdays, weekdayNames, monthNames, monthsShort, monthTitle, yearTitle, yearRangeTitle, dayLabel, todaySuffix, unavailableSuffix, chooseMonth, chooseYear, backToDates, monthGroup, previousMonth, nextMonth, previousYear, nextYear, previousYears, nextYears, today, dialog, openCalendar, openCalendarWith, clear, formatHint, minHint, maxHint, unavailableHint. Lists start on Sunday and January. Shallow-merged over the English defaults.
...restHTMLAttributes<HTMLDivElement>—className, style and other attributes go to the root. Width fills the container by default.

アクセシビリティ

  • The input is role="combobox" with aria-haspopup="dialog"; the panel is a dialog and the calendar a grid. Each cell is named with its full date, plus "today" or "unavailable" when it applies.
  • Down arrow in the input opens the panel and moves focus into the calendar; arrows move by day or week, Home and End go to the start and end of the week, PageUp and PageDown change month (with Shift, year), Enter or Space selects, Esc closes and returns focus.
  • Only one cell is in the Tab order; unavailable days cannot be focused and arrow keys skip them.
  • Today is bold with a dot, so it does not rely on colour alone. Unavailable days are faded, cannot be focused, and their cell name ends with "unavailable".
  • Typing errors are announced through role="alert"; the new month and year are announced after paging.
  • Pair the input with <label htmlFor> and inputProps.id, or pass an aria-label.

モーション

The field's focus halo grows from 0 to 4px over 220ms. Panel enter 220ms ease-out (moves 6px and scales 0.96 to 1 from the corner touching the trigger, with a fade), exit 140ms ease-in. The chosen day pops in (scale 0.6 to 1, 380ms spring). Changing month slides the new page in 12px over 280ms. With reduced motion the panel only fades and nothing else moves.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

DatePicker.jsxdate-picker.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フォーム入力
「空き時間グリッド」は Pro に含まれます。解除するとプレビューが表示されます。
「図形のブール演算」は Pro に含まれます。解除するとプレビューが表示されます。
「カスケード選択」は Pro に含まれます。解除するとプレビューが表示されます。