Эта страница переведена машинным способом и может содержать ошибки. Читать оригинал на английском

Элементы

Calendar

Calendar выбирает дату или, с range, диапазон дат из месяцев, разложенных по неделям: срок в форме, ночи проживания, дни отчёта. Он показывает один месяц или несколько рядом, листает месяцы кнопками «Назад» и «Вперёд» и переключается на вид месяцев года или страницу лет, чтобы перейти далеко. Это тот же календарь, что во всплывающем окне DatePicker, только отдельно на странице.

el-date-picker.tsx
el-date-picker.tsxLive
import { type JSX, mount, signal, Text, View, windowSize } from "nsh";
import { type CalendarEntry, DatePicker, type DatePickerValue, type DateRangePreset, type HourCycle, type OpenChangeReason, type TimeFieldSegmentEntry } from "nsh/base";
import { button, desktop, Icon, Log, Panel, Segmented, size, Split, ui, wide } from "./palette";

// Three date pickers in a card: a due date (weekends disabled, presets
// beside the calendar, a clear button), a stay picked as a range over two
// months in a wide window, and a meeting's date and time with the hours
// on the clock the inspector picks. The trigger is the shared button look
// with the value and a chevron that turns while the calendar is open; the
// calendar is a popup at the trigger, a sheet on a touch screen. Every
// item is drawn by `renderItem` (month names, weekday initials, today on a
// quiet fill, the selected days filled, a range's band) and every segment
// of the time by `renderSegment`; the time's label starts its row and the
// field, a well as the number inputs', ends it.

const MONTHS = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const WEEKDAYS = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];

/** A day's ISO date (`"2026-10-28"`), in local time as the calendar's days are. */
const iso = (d: Date) => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`;
const addDays = (d: Date, days: number) => new Date(d.getFullYear(), d.getMonth(), d.getDate() + days);
/** An ISO date's month and day: `Oct 28`. */
const monthDay = (value: string) => `${MONTHS[Number(value.slice(5, 7)) - 1].slice(0, 3)} ${Number(value.slice(8, 10))}`;

/** A time `"HH:MM"` on the picked clock: `14:30`, or `2:30 PM`. */
function clock(time: string, cycle: HourCycle) {
  if (cycle === "h23") return time;
  const hour = Number(time.slice(0, 2));
  return `${hour % 12 === 0 ? 12 : hour % 12}:${time.slice(3, 5)} ${hour < 12 ? "AM" : "PM"}`;
}

/** The text of a control: 13 with a pointer, the touch label size otherwise. */
const controlText = desktop ? 13 : size.label;
/** A day's cell: seven across the popup with a pointer, across a phone's sheet otherwise. */
const cell = desktop ? 32 : 42;
/** The gap between the months of a calendar of two. */
const monthGap = 16;
/**
 * On a touch screen the calendar spans the sheet (the window less the sheet's padding), its columns sharing it: the
 * header's titles split what Previous and Next leave, so the month and the year meet in the middle, and each month's
 * title of two is over its month.
 */
const toggleWidth = () => Math.floor((windowSize().width - 2 * 10) / 2 - cell);

/** A popup at the trigger with a pointer, a sheet at the bottom on a touch screen. */
const surface = () =>
  desktop
    ? { ...ui().card, borderRadius: 12, padding: 8 }
    : { ...ui().card, borderTopLeftRadius: 20, borderTopRightRadius: 20, paddingHorizontal: 10, paddingTop: 14, paddingBottom: 30 };

const backdrop = { backgroundColor: "rgba(0,0,0,0.45)" };

/** The popup's layout: presets in a column beside the calendar with a pointer, a row of chips above it on a phone. */
const content = desktop ? { flexDirection: "row" as const, gap: 10 } : { gap: 12 };
const presets = () =>
  desktop
    ? { gap: 2, minWidth: 104, paddingRight: 10, borderRightWidth: 1, borderColor: ui().border }
    : { flexDirection: "row" as const, flexWrap: "wrap" as const, gap: 6 };
const preset = () =>
  desktop
    ? { height: 28, paddingHorizontal: 8, borderRadius: 6, justifyContent: "center" as const, fontSize: 13, color: ui().text }
    : { height: 34, paddingHorizontal: 12, borderRadius: 17, borderWidth: 1, borderColor: ui().border, justifyContent: "center" as const, fontSize: 14, color: ui().text };
/** The calendar: in the popup with a pointer; across the sheet on a touch screen, as the time's row under it. */
const calendar = desktop ? { padding: 2 } : {};
/** The time's row under a hairline, its label at the start and the field at the end, on the calendar's edges. */
const time = () => ({
  gap: 10,
  marginTop: 8,
  paddingTop: 8,
  borderTopWidth: 1,
  borderColor: ui().border,
  ...(desktop ? { paddingHorizontal: 2 } : {}),
});
/** The time's field: a recessed well as the number inputs', with their hover and the keyboard's ring while it edits. */
const timeField = [
  desktop ? "h-8 rounded-lg px-2" : "h-11 rounded-xl px-3",
  "items-center border border-input bg-field",
  "enabled:hover:border-faint focus-within:outline-2 focus-within:outline-info",
].join(" ");

/** The trigger: the shared button's look, the value at the start and a chevron in a lane as wide as it. */
function Trigger(props: { title?: string; placeholder: string; open: boolean; clearable?: boolean }) {
  const look = button("secondary", {
    style: () => ({
      flexDirection: "row",
      justifyContent: "space-between",
      gap: 8,
      ...(desktop ? { width: 260 } : {}),
      ...(props.open ? { borderColor: ui().faint } : {}),
    }),
  });
  return (
    <View {...look}>
      <Text style={{ numberOfLines: 1, flex: 1, fontSize: controlText, color: props.title ? ui().text : ui().muted }}>
        {props.title ?? props.placeholder}
      </Text>
      {/* With a clear button over the end (`cleanable`), an empty lane as wide as the button in place of the chevron. */}
      <View
        class="transition-transform duration-150 motion-reduce:transition-none"
        style={{
          width: props.clearable ? 20 : 10,
          height: 20,
          alignItems: "center",
          justifyContent: "center",
          opacity: props.clearable ? 0 : 1,
          transform: [{ rotate: props.open ? "270deg" : "90deg" }],
        }}
      >
        <Text style={{ fontSize: 18, color: ui().muted }}>›</Text>
      </View>
    </View>
  );
}

/**
 * The calendar's items (`itemClass`): pressed at 70%; `group`, so the
 * drawing inside follows the item's states.
 */
const itemClass = "group active:opacity-70";

/**
 * An item of the calendar, drawn once from what it is, its states the
 * item's classes: Previous and Next as arrows (faint while they take no
 * press), the month and year toggles (or each month's title, with `months`
 * over one) by name, the open view's toggle in the accent, the weekdays'
 * initials, and the days: today on the theme's quiet `accent` fill
 * (gpui-component's), the selected date and a range's ends filled, the days
 * of a complete range on a band from end to end (`data-range-*`), the other
 * months' days and the disabled ones faint, the keyboard's day ringed.
 */
function Item(props: { item: CalendarEntry; months?: number }) {
  const kind = props.item.kind;
  if (kind === "previous" || kind === "next") {
    return (
      <View class="group-disabled:opacity-30" style={{ width: cell, height: cell, alignItems: "center", justifyContent: "center" }}>
        <Text style={{ fontSize: 20, color: ui().muted }}>{kind === "previous" ? "‹" : "›"}</Text>
      </View>
    );
  }
  if (kind === "monthToggle" || kind === "yearToggle") {
    // A month's title in a calendar of several takes no press: the month and its year, over the month.
    const title = kind === "monthToggle" && (props.months ?? 1) > 1;
    return (
      <Text
        class="text-foreground group-selected:text-brand"
        style={{
          width: !desktop ? toggleWidth() : title ? 6 * cell + monthGap / 2 : kind === "monthToggle" ? 3 * cell : 2 * cell,
          paddingHorizontal: 3,
          textAlign: title ? "center" : kind === "monthToggle" ? "right" : "left",
          fontSize: size.label,
          fontWeight: "600",
        }}
      >
        {title ? `${MONTHS[props.item.value - 1]} ${props.item.year}` : kind === "monthToggle" ? MONTHS[props.item.value - 1] : String(props.item.value)}
      </Text>
    );
  }
  if (kind === "weekday") {
    return <Text style={{ minWidth: cell, paddingVertical: 4, textAlign: "center", fontSize: 11, color: ui().faint }}>{WEEKDAYS[props.item.value]}</Text>;
  }
  if (kind === "month" || kind === "year") {
    // The month view's months (rows of 3) and the year view's years (rows of 5), as wide as a month; the shown one
    // filled.
    return (
      <View
        class="rounded-lg group-selected:bg-brand-fill"
        style={{ minWidth: (7 * cell) / (kind === "month" ? 3 : 5), height: cell + 6, alignItems: "center", justifyContent: "center" }}
      >
        <Text class="text-foreground group-selected:text-on-brand" style={{ fontSize: controlText }}>
          {kind === "month" ? MONTHS[props.item.value - 1].slice(0, 3) : String(props.item.value)}
        </Text>
      </View>
    );
  }
  // A day, as wide as its column (the calendar's columns share its width). The band spans a range's middle days and
  // reaches from its ends' middles; a range of one day has none. The circle fills for the selected date and a
  // range's ends (not its middle days), else for today.
  const own = props.item.outside ? "text-faint" : "text-foreground";
  return (
    <View style={{ minWidth: cell, height: cell, alignItems: "center", justifyContent: "center" }}>
      <View
        class={
          "absolute inset-y-0.5 left-0 right-0 hidden bg-brand-tint group-data-range-middle:flex group-data-range-start:flex " +
          "group-data-range-start:left-1/2 group-data-range-end:flex group-data-range-end:right-1/2 group-data-range-start:group-data-range-end:hidden"
        }
      />
      <View
        class={
          `${props.item.today ? "bg-accent" : ""} border-[1.5px] border-transparent group-focus:border-info ` +
          "group-selected:bg-brand-fill group-data-range-middle:bg-transparent"
        }
        style={{ width: cell - 4, height: cell - 4, borderRadius: (cell - 4) / 2, alignItems: "center", justifyContent: "center" }}
      >
        <Text
          class={
            `${own} ${props.item.today ? "font-semibold" : "font-normal"} group-selected:font-semibold group-selected:text-on-brand ` +
            `group-data-range-middle:${own} group-disabled:text-faint group-disabled:opacity-50`
          }
          style={{ fontSize: controlText, fontVariant: ["tabular-nums"] }}
        >
          {String(props.item.value)}
        </Text>
      </View>
    </View>
  );
}

/**
 * A segment of the time: filled while it has the keys (`focus:` on the
 * segment, `segmentClass`), its digits or AM / PM in the color on the fill
 * (`group-focus:`), so moving between the segments draws nothing.
 */
const segmentClass = "group rounded active:opacity-70 focus:bg-brand-fill";
function Segment(props: { segment: TimeFieldSegmentEntry }) {
  return (
    <Text class="text-foreground group-focus:text-on-brand" style={{ paddingHorizontal: 3, fontSize: controlText, fontVariant: ["tabular-nums"] }}>
      {props.segment.label}
    </Text>
  );
}

/** A label over its control, a quieter line under the label. */
function Field(props: { label: string; detail: string; children: JSX.Element }) {
  return (
    <View style={{ gap: 8 }}>
      <View style={{ gap: 2 }}>
        <Text style={{ fontSize: size.label, fontWeight: "500", color: ui().text }}>{props.label}</Text>
        <Text style={{ fontSize: size.detail, color: ui().muted }}>{props.detail}</Text>
      </View>
      {props.children}
    </View>
  );
}

function App() {
  const due = signal<string | null>(null);
  const stay = signal<DatePickerValue>(null);
  const meeting = signal<string | null>(null);
  const cycle = signal<HourCycle>("h12");
  const dueOpen = signal(false);
  const stayOpen = signal(false);
  const meetingOpen = signal(false);
  const events = signal<string[]>([]);
  const log = (line: string) => events.set([line, ...events()].slice(0, 8));
  const opened = (open: (next: boolean) => void) => ({ open: next }: { open: boolean; reason: OpenChangeReason }) => open(next);
  // The presets from today.
  const today = new Date();
  const monday = addDays(today, ((8 - today.getDay()) % 7) || 7);
  const dues: DateRangePreset[] = [
    { label: "Today", value: iso(today) },
    { label: "Tomorrow", value: iso(addDays(today, 1)) },
    { label: "Next Monday", value: iso(monday) },
  ];
  const stays: DateRangePreset[] = [
    { label: "This week", value: [iso(today), iso(addDays(today, (7 - today.getDay()) % 7))] },
    { label: "Next 7 nights", value: [iso(today), iso(addDays(today, 7))] },
  ];
  const stayTitle = () => {
    const r = stay();
    return Array.isArray(r) && r[0] && r[1] ? `${monthDay(r[0])} – ${monthDay(r[1])}, ${r[1].slice(0, 4)}` : undefined;
  };
  const meetingTitle = () => {
    const v = meeting();
    return v ? `${monthDay(v)}, ${v.slice(0, 4)} · ${clock(v.slice(11, 16), cycle())}` : undefined;
  };
  return (
    <View style={{ flex: 1, backgroundColor: ui().page, padding: 20, paddingTop: 24, gap: 14 }}>
      <Text style={{ fontSize: 22, fontWeight: "700", color: ui().text }}>date-picker</Text>
      <Text style={{ color: ui().muted }}>
        {desktop ? "Arrows move between days, Return picks; the trigger takes Delete to clear" : "Tap a day; the meeting's time takes digits"}
      </Text>

      <Split
        gap={14}
        inspector={
          <>
            <Panel title="value">
              <Text style={{ fontSize: 13, color: ui().text }}>{`due ${JSON.stringify(due())}`}</Text>
              <Text style={{ fontSize: 13, color: ui().text }}>{`stay ${JSON.stringify(stay())}`}</Text>
              <Text style={{ fontSize: 13, color: ui().text }}>{`meeting ${JSON.stringify(meeting())}`}</Text>
            </Panel>
            <Segmented label="hourCycle" options={["h12", "h23"] as const} value={cycle()} onChange={(v) => cycle.set(v)} />
            <Log title="onChange" lines={events()} minHeight={120} />
          </>
        }
      >
        <View style={{ ...ui().card, borderRadius: 16, padding: 16, gap: 20 }}>
          <Field label="Due" detail="Weekdays only, presets beside the calendar, a clear button">
            <DatePicker
              value={due()}
              onChange={({ value }) => {
                due.set(typeof value === "string" ? value : null);
                log(`Due → ${JSON.stringify(value)}`);
              }}
              onOpenChange={opened((next) => dueOpen.set(next))}
              accessibilityLabel="Due"
              disabledMatcher={[0, 6]}
              presets={dues}
              offset={4}
              margin={8}
              popupStyle={surface()}
              backdropStyle={backdrop}
              contentStyle={content}
              presetsStyle={presets()}
              presetStyle={preset()}
              presetClass="active:opacity-70"
              calendarStyle={calendar}
              headerStyle={{ marginBottom: 4 }}
              itemClass={itemClass}
              renderItem={(item) => <Item item={item} />}
              style={desktop ? { width: 260 } : undefined}
              cleanable
              clearButtonAccessibilityLabel="Clear the due date"
              clearButtonClass="absolute items-center justify-center rounded-full enabled:hover:opacity-70"
              clearButtonStyle={{ top: (size.control - 20) / 2, right: 10, width: 20, height: 20 }}
              clearButton={<Icon name="close" size={14} tint={ui().muted} />}
              trigger={<Trigger title={due() ? `${monthDay(due()!)}, ${due()!.slice(0, 4)}` : undefined} placeholder="No due date" open={dueOpen()} clearable={due() !== null} />}
            />
          </Field>
          <Field label="Stay" detail={wide() ? "A range over two months: the first press starts it" : "A range: the first press starts it, the second ends it"}>
            <DatePicker
              range
              value={stay()}
              onChange={({ value }) => {
                stay.set(value);
                log(`Stay → ${JSON.stringify(value)}`);
              }}
              onOpenChange={opened((next) => stayOpen.set(next))}
              accessibilityLabel="Stay"
              numberOfMonths={wide() ? 2 : 1}
              firstDayOfWeek={1}
              presets={stays}
              offset={4}
              margin={8}
              popupStyle={surface()}
              backdropStyle={backdrop}
              contentStyle={content}
              presetsStyle={presets()}
              presetStyle={preset()}
              presetClass="active:opacity-70"
              calendarStyle={calendar}
              headerStyle={{ marginBottom: 4 }}
              bodyStyle={{ gap: monthGap }}
              itemClass={itemClass}
              renderItem={(item) => <Item item={item} months={wide() ? 2 : 1} />}
              trigger={<Trigger title={stayTitle()} placeholder="Check-in – check-out" open={stayOpen()} />}
            />
          </Field>
          <Field label="Meeting" detail={desktop ? "A day, then its time; the day again closes the calendar" : "A tap picks the day; open it again for the time"}>
            <DatePicker
              value={meeting()}
              onChange={({ value }) => {
                meeting.set(typeof value === "string" ? value : null);
                log(`Meeting → ${JSON.stringify(value)}`);
              }}
              onOpenChange={opened((next) => meetingOpen.set(next))}
              accessibilityLabel="Meeting"
              timePrecision="minute"
              hourCycle={cycle()}
              defaultTime="09:00"
              offset={4}
              margin={8}
              popupStyle={surface()}
              backdropStyle={backdrop}
              calendarStyle={calendar}
              headerStyle={{ marginBottom: 4 }}
              itemClass={itemClass}
              renderItem={(item) => <Item item={item} />}
              timeStyle={time()}
              timeLabel={<Text style={{ fontSize: controlText, color: ui().muted }}>Time</Text>}
              timeAccessibilityLabel="Meeting time"
              timeFieldClass={timeField}
              segmentClass={segmentClass}
              separatorStyle={{ fontSize: controlText, color: ui().faint }}
              renderSegment={(segment) => <Segment segment={segment} />}
              trigger={<Trigger title={meetingTitle()} placeholder="Not scheduled" open={meetingOpen()} />}
            />
          </Field>
        </View>
      </Split>
    </View>
  );
}

mount(App);

Календарь отвечает за поведение: значение (принадлежащее приложению или собственное), показанный месяц, виды, дни, которые нельзя выбрать, клавиатуру и то, что слышат скринридеры. Внешний вид задаёт приложение: оно стилизует заголовок, тело, каждый месяц, каждую строку и каждый элемент и рисует содержимое элементов (названия месяцев и дней недели, отметку сегодняшнего дня, полосу диапазона). Сетка занимает всю отведённую ей ширину, как столбцы таблицы: месяцы делят её между собой, а элементы строки делят строку поровну. Поэтому календарь в шторке телефона растягивается на весь экран, а календарь во всплывающем окне, размер которого определяется содержимым, сохраняет ширину своих элементов (flex-none в itemClass или monthClass оставляет элементу или месяцу его собственную ширину).

use nsh_base::{CalendarEntry, CalendarItemKind, Date, Matcher, calendar};
use nsh_core::prelude::*;

const MONTHS: [&str; 12] = [
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December",
];
const WEEKDAYS: [&str; 7] = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];

fn due_date() -> impl IntoElement {
    let due = signal(Date::default());
    calendar()
        .value(move || due.get())
        .on_change(move |next: &Date| due.set(*next))
        .disabled_matcher(Matcher::DayOfWeek(vec![0, 6]))
        .previous_accessibility_label("Previous month")
        .next_accessibility_label("Next month")
        .item_class("rounded-md selected:bg-primary selected:text-primary-foreground disabled:opacity-50")
        .render_item(|item: &CalendarEntry| {
            let label = match item.kind {
                CalendarItemKind::Previous => "‹".to_string(),
                CalendarItemKind::Next => "›".to_string(),
                CalendarItemKind::Weekday => WEEKDAYS[item.value as usize].to_string(),
                CalendarItemKind::MonthToggle | CalendarItemKind::Month => {
                    MONTHS[item.value as usize - 1].to_string()
                }
                _ => item.value.to_string(),
            };
            view()
                .w_8()
                .h_8()
                .items_center()
                .justify_center()
                .child(text(label).text_sm())
        })
}

Calendar поставляется из nsh/base — крейта nsh-base для Rust. В Rust calendar() создаёт его с сеттером для каждого свойства (.value(..), .range(..), .number_of_months(..)) и слушателем для каждого события (.on_change(..)). Его значение — Date (Date::Single, Date::Range, из chrono::NaiveDate или пары таких значений), а отключаемые им дни — Matcher.

#Значение

Значение — это ISO-дата "2026-10-28", как у HTML <input type="date">; с range это [start, end], а null означает отсутствие даты. С value значением владеет приложение: календарь показывает его как выбранное и сообщает о выборе через onChange({ value }); чтобы следовать за выбором, приложение задаёт value. Если свойство не указано, календарь хранит собственное значение, вначале берёт его из defaultValue.

onChange сообщает только о полных значениях, как Selected у gpui-base: о нажатом дне или о конце диапазона.

  • Без range нажатие на день выбирает его.
  • С range первое нажатие начинает диапазон, а второе завершает его; дни между ними входят в диапазон (каждый selected, включая концы, см. Элементы). Второе нажатие на более ранний день начинает диапазон заново с него, так же как и нажатие после того, как диапазон завершён. Неполный диапазон отображается, пока не будет завершён, и только тогда о нём сообщается.

Календарь открывается на месяце значения (для диапазона — на месяце его начала), а если значения нет — на текущем месяце.

#Месяцы и виды

numberOfMonths показывает столько месяцев рядом, начиная с показанного; «Назад» и «Вперёд» листают их по одному месяцу. firstDayOfWeek — день, с которого начинаются недели: от 0 (воскресенье, по умолчанию) до 6 (суббота).

В заголовке находятся «Назад», два переключателя и «Вперёд»:

  • Вид дней показывает дни месяцев: каждый месяц — это столбец из строки дней недели и строк недель; дни предыдущего и следующего месяцев заполняют недели (outside, data-outside).
  • Переключатель месяца (monthToggle) открывает вид месяцев: двенадцать месяцев по три в строке. Выбранный месяц показывается в виде дней.
  • Переключатель года (yearToggle) открывает вид лет: страницу из 20 лет по пять в строке, «Назад» и «Вперёд» листают страницы. Выбранный год показывается в виде дней. yearRange — годы, по которым идут страницы, [start, end) (по умолчанию: от 50 лет до текущего года до 50 лет после него).

Если месяцев несколько, вместо переключателей заголовок показывает название каждого месяца (элемент monthToggle, не принимающий нажатий), как у gpui-base.

#Недоступные даты

disabledMatcher — это дни, которые пользователь не может выбрать, Matcher у gpui-base, в одной из четырёх форм:

ФормаЧто отключает
[0, 6]Указанные дни недели, 0 — воскресенье, 6 — суббота.
{ before, after }Дни до before и после after (любую границу можно опустить).
{ from, to }Дни от from до to включительно (опущенная граница означает открытый конец).
(date) => booleanДни, для которых функция возвращает true, получив ISO-дату каждого показанного дня.

Функция вызывается один раз для каждого показанного дня, в следующем такте JS, и повторно для дня, на который уже ответила, не вызывается: при листании месяцев она запрашивается только для новых дней, поэтому её ответ должен быть чистой функцией даты. Если передать другую функцию, она будет вызвана заново. Недоступный день — disabled:: он не принимает нажатий, а Return и Space его не выбирают.

#Элементы

Каждая цель нажатия и каждая подпись календаря — это элемент: «Назад», переключатели, «Вперёд», дни недели, дни, месяцы и годы. Каждый рисуется в части, стилизуемой через itemClass и itemStyle, а её варианты следуют состоянию элемента:

ВариантЭлемент
selected:Выбран: дата, каждый день полного диапазона (включая концы, как в range calendar из React Aria), начало неполного диапазона, показанный месяц или год, переключатель открытого вида. Скринридеры озвучивают его как выбранный.
disabled:Не принимает нажатий: недоступная дата, день недели, название месяца, «Назад» и «Вперёд» в виде месяцев или на границах лет.
focus:Имеет фокус клавиатуры.
data-range-start:, data-range-end:Первый или последний день полного диапазона (диапазон из одного дня — и тот и другой).
data-range-middle:День внутри полного диапазона, не его конец.
data-today:Сегодняшний день.
data-outside:День предыдущего или следующего месяца, заполняющий недели.

Варианты data-* — это варианты Tailwind (data-range-start: соответствует [data-range-start]), так же как календарь shadcn стилизует свои дни; при group в itemClass содержимое следует им с помощью group-selected:, group-data-range-middle: и так далее. selected: означает каждый день диапазона, поэтому концы диапазона получают свой вид поверх него (побеждают более поздние варианты: data-* идёт после selected:), а средние дни — свой:

<Calendar
  range
  itemClass="group rounded-md selected:bg-primary selected:text-primary-foreground data-range-middle:rounded-none data-range-middle:bg-accent data-range-middle:text-accent-foreground data-range-start:rounded-r-none data-range-end:rounded-l-none data-range-start:data-range-end:rounded-md"
/>

renderItem рисует содержимое элемента по тому, чем он является, CalendarEntry, и никогда по его состоянию: отрисовка выполняется один раз и сохраняется, пока элемент выбран, находится в фокусе или входит в диапазон, поэтому клавиши, выбор и фокус ничего не перерисовывают, а при листании месяцев каждый новый день рисуется по одному разу (день сохраняет свою отрисовку по месяцу и дате: при нескольких месяцах те, что остаются на экране, сохраняют свою).

ПолеЗначение
kind"previous", "monthToggle", "yearToggle", "next", "weekday", "day", "month" или "year".
valueЕго номер: день месяца, месяц (от 1 до 12), год, день недели (от 0 — воскресенье до 6); 0 для «Назад» и «Вперёд».
dateISO-дата дня.
year, monthГод и месяц, которым он принадлежит: у дня — месяц, в чьих неделях он показан; у переключателя месяца или названия месяца — свои; у месяца в виде месяцев — свои; у переключателя года и у года — собственный год. 0 для «Назад», «Вперёд» и дней недели (и month для переключателя года и лет).
todayДень является сегодняшним.
outsideДень относится к предыдущему или следующему месяцу.

Без renderItem элемент показывает свой номер, а «Назад» и «Вперёд» показывают ‹ и ›, как у gpui-base; день недели тоже показывает свой номер, от 0 до 6, поскольку названия месяцев и дней недели — слова приложения на языке приложения.

#Свойства

СвойствоRustТипПо умолчаниюОписание
value.value(..)CalendarValueнетЗначение, которым владеет приложение.
defaultValue.default_value(..)CalendarValuenullНачальное значение, без value.
range.range(..)booleanfalseВыбор диапазона: первое нажатие начинает его, второе завершает.
numberOfMonths.number_of_months(..)number1Сколько месяцев показывается рядом.
firstDayOfWeek.first_day_of_week(..)number0День, с которого начинаются недели: от 0 (воскресенье) до 6 (суббота).
disabledMatcher.disabled_matcher(..)DateMatcherнетДаты, которые нельзя выбрать.
yearRange.year_range(..)[number, number]50 лет в обе стороныГоды, по которым листает вид лет, [start, end).
previousAccessibilityLabel.previous_accessibility_label(..)stringнетЧто скринридеры произносят для «Назад».
nextAccessibilityLabel.next_accessibility_label(..)stringнетЧто скринридеры произносят для «Вперёд».
headerClass, headerStyle.header_class(..), .header_style(..)string, StyleProp<ViewStyle>нетЗаголовок: «Назад», переключатели (или названия месяцев), «Вперёд».
bodyClass, bodyStyle.body_class(..), .body_style(..)string, StyleProp<ViewStyle>нетТело: месяцы рядом или строки вида месяцев либо лет.
monthClass, monthStyle.month_class(..), .month_style(..)string, StyleProp<ViewStyle>нетКаждый месяц вида дней.
rowClass, rowStyle.row_class(..), .row_style(..)string, StyleProp<ViewStyle>нетКаждая строка: дни недели, неделя, строка месяцев или лет.
itemClass, itemStyle.item_class(..), .item_style(..)string, StyleProp<TextStyle>нетКаждый элемент: selected:, disabled:, focus:; у дня — data-range-start:, data-range-end:, data-range-middle:, data-today:, data-outside: (Элементы).
renderItem.render_item(..)(item: CalendarEntry) => JSX.ElementнетСодержимое элемента, которое приложение рисует по тому, чем является элемент; иначе — его номер.
class, styleметоды стилейstring, StyleProp<ViewStyle>нетКалендарь: заголовок над телом.

#События

СобытиеRustОписание
onChange.on_change(|value| ..)Пользователь выбрал полное значение: { value }, день или диапазон. Задайте value, чтобы следовать за ним. О value, переданном приложением, никогда не сообщается.

#Части

ЧастьГде находитсяСтилизуется через
Календарькореньclass, style
Заголовокв календаре, первымheaderClass, headerStyle
Телов календаре, после заголовкаbodyClass, bodyStyle
Месяцв теле (вид дней)monthClass, monthStyle
Строкав месяце или в теле (виды месяцев и лет)rowClass, rowStyle
Элементв заголовке и строкахitemClass, itemStyle, renderItem (внутри group-selected:, group-data-range-middle:, … под group в itemClass)

#Клавиатура

Дни образуют одну остановку Tab: фокус попадает на выбранный день, иначе на сегодняшний, иначе на первый показанный день. Далее:

КлавишаДействие
Left / RightДень до или после.
Up / DownТот же день неделей раньше или позже.
Home / EndПервый или последний день недели.
Page Up / Page DownТот же день месяцем раньше или позже.
Shift+Page Up / Shift+Page DownТот же день годом раньше или позже.
Return, SpaceВыбирает день.

В раскладке справа налево недели идут справа, поэтому Left — это следующий день, а Right — предыдущий; Up и Down сохраняют недели. При выходе за пределы показанных месяцев они листаются. «Назад», переключатели, «Вперёд», месяцы и годы принимают нажатия. В gpui-base дни принимают только нажатия; клавиши — это клавиши NSH, по образцу диалога выбора даты из WAI-ARIA.

#Доступность

  • Каждый день — это кнопка, названная по его ISO-дате; она выбрана, пока это дата или день диапазона (включая концы, как в range calendar из React Aria), и затемнена, пока недоступна.
  • «Назад» и «Вперёд» — это кнопки с названиями previousAccessibilityLabel и nextAccessibilityLabel; переключатели, месяцы и годы — кнопки, названные по тому, что они показывают.

Общие сведения о подписях, ролях и объявлениях см. в разделе Доступность.