For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/llms-full.txt, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/components/time-range-picker.md.

TimeRangePicker 时间范围

选择一天内的起止时间。

基本用法

09:00 ~ 18:00
basic.tsx
import { Space, Text, TimeRangePicker } from '@ifhover/leaf-ui';

import { useState } from 'react';

export function TimeRangePickerBasic() {
  const [value, setValue] = useState<import('@ifhover/leaf-ui').TimeRange | null>([
    '09:00',
    '18:00',
  ]);
  return (
    <Space direction="vertical" align="stretch">
      <TimeRangePicker value={value} onChange={setValue} />
      <output>
        <Text tone="muted">{value?.join(' ~ ') || '—'}</Text>
      </output>
    </Space>
  );
}

更多用法

12 小时制与秒
允许跨过午夜
advanced.tsx
import { Space, Text, TimeRangePicker } from '@ifhover/leaf-ui';

export function TimeRangePickerOptions() {
  return (
    <Space direction="vertical" align="stretch">
      <Text tone="muted">12 小时制与秒</Text>
      <TimeRangePicker use12Hours showSeconds defaultValue={['09:30:00', '17:45:00']} />
      <Text tone="muted">允许跨过午夜</Text>
      <TimeRangePicker allowOvernight defaultValue={['22:00', '06:00']} />
    </Space>
  );
}

使用说明

点击确定后提交草稿。可输入 09:00 ~ 18:00 并按 Enter。allowOvernight 允许结束时间早于开始时间,表示次日结束。

类型

组装数据、定义回调和使用 ref 时,可以导入这些类型。

import type { TimeRangePickerProps, TimeRange, TimeParts } from '@ifhover/leaf-ui';

导出的 TimeRange 为 readonly [string, string],使用 24 小时制时间字符串。

限制可选时间

开始与结束均限制在营业时间
capabilities.tsx
import { Space, Text, TimeRangePicker } from '@ifhover/leaf-ui';

export function Capabilities() {
  return (
    <Space direction="vertical" align="stretch">
      <Text>{'开始与结束均限制在营业时间'}</Text>
      <TimeRangePicker
        defaultValue={['09:00', '17:30']}
        disabledTime={(time) => time.hour < 9 || time.hour > 18}
        use12Hours
        minuteStep={15}
      />
    </Space>
  );
}

TimeRangePickerProps API

属性类型默认值说明
popupPlacement'top' | 'top-start' | 'top-end' | 'bottom' | 'bottom-start' | 'bottom-end' | 'left' | 'left-start' | 'left-end' | 'right' | 'right-start' | 'right-end'—浮层相对触发控件的位置。
popupClassNamestring—浮层的自定义类名。
popupStyleCSSProperties—浮层的自定义样式。
popupRender(content: React.ReactNode) => React.ReactNode—包装或补充面板内容,保留传入的内容以维持交互。
getPopupContainer() => Element | DocumentFragment—返回浮层的挂载容器,也可返回 ShadowRoot。
size'sm' | 'md' | 'lg''md'尺寸或间距预设
status'error' | 'warning'—显示状态
allowClearbooleantrue显示清除按钮
inputReadOnlyboolean—只禁止手动输入,仍可在面板中选择。
openboolean—控制面板是否展开。
defaultOpenboolean—面板初始展开状态。
valuereadonly [string, string] | null—受控值
defaultValuereadonly [string, string] | nullnull初始值;非受控表单 reset 时恢复
onChange(value: readonly [string, string] | null) => void—值变化回调
onOpenChange(open: boolean) => void—展开状态变化
showSecondsbooleanfalse显示秒
use12Hoursbooleanfalse12 小时制显示
minuteStepnumber5分钟选项间隔
secondStepnumber1秒选项间隔
allowOvernightbooleanfalse允许跨过午夜的时间范围
disabledTime(parts: TimeParts, position: 'start' | 'end') => boolean—分别限制开始与结束时间

TimeParts API

属性类型默认值说明
hournumber必填小时,0–23
minutenumber必填分钟,0–59
secondnumber必填秒,0–59