跳到主要内容

时间选择器 DateTimePicker

用于选择时间,支持日期、时分等时间维度,通常与弹出层组件配合使用

安装使用

import { TiDatetimePicker } from '@titian-design/mobile-react'

用法示例

基础用法

const App: React.FC = () => {
return (
<>
<TiPopup visible={visible} position="bottom">
<TiDatetimePicker value={ value } onConfirm={onConfirm} />
</TiPopup>
</>
)
}

不同时间类型

const App: React.FC = () => {
return (
<>
<TiPopup visible={visible} position="bottom">
// 年月日
<TiDatetimePicker type="date" />
// 时分
<TiDatetimePicker type="time" />
// 年月日 - 时分
<TiDatetimePicker type="datetime" />
// 年月
<TiDatetimePicker type="year-month" />
</TiPopup>
</>
)
}

选项格式化

const App: React.FC = () => {
enum columnType { YEAR = "year", MONTH = "month", DAY = "day", HOUR = "hour", MINUTE = "minute" }
const formatter = (type: columnType, value: number) => {
const pipe = {
year: `${value}`,
month: `${value}`,
day: `${value}`,
hour: `${value}`,
minute: `${value}`,
}
return pipe[type]
}
return (
<>
<TiPopup visible={visible} position="bottom">
<TiDatetimePicker formatter={ formatter } />
</TiPopup>
</>
)
}

选项过滤器

const App: React.FC = () => {
enum columnType { YEAR = "year", MONTH = "month", DAY = "day", HOUR = "hour", MINUTE = "minute" }
const filter = (type: columnType, options: Array<{value: number}>) => {
if (type === "year") {
return options.filter((options) => options.value % 2 === 0);
}
return options;
}
return (
<>
<TiPopup visible={visible} position="bottom">
<TiDatetimePicker filter={ filter } />
</TiPopup>
</>
)
}

设置边界

const App: React.FC = () => {
return (
<>
<TiPopup visible={visible} position="bottom">
<TiDatetimePicker value={ value } minDate={ minDate } maxDate={ maxDate } />
</TiPopup>
</>
)
}

时间项排序

sort项取值应该和type类型对应

const App: React.FC = () => {
return (
<>
<TiPopup visible={visible} position="bottom">
<TiDatetimePicker sort={['month', 'year']} type="year-month" />
</TiPopup>
</>
)
}

TiDatetimePicker API

属性 Properties

名称类型是否必填默认值说明备注
valuestring---
typestringdata年月日date 年月year-month 时分time 年月日 - 时分datetime-
minDatenumber十年前毫秒数最小边界-
maxDatenumber十年后毫秒数最大边界-
sortarray-sort 需要与 type 匹配,匹配关系-
loadingbooleanfalse加载动画-
formatterfunction-格式化内容-
filterfunction-过滤时间-
titlestring-标题-
subTitlestring-副标题-
confirmTextstring-确认按钮文案-
cancelTextstring-取消按钮文案-
optionItemHeightnumber | string108展示项高度-
visibleItemCountnumber5可见展示项数-
extStylestring-容器样式-
extOptionStylestring-子器样式-

sort排序的取值和type对应关系

typesort
date['year', 'month', 'day']
year-month['year', 'month']
time['hour', 'minute']
datetime['year', 'month', 'day', 'hour', 'minute']

事件 Events

名称参数列表描述备注
onConfirm(e: CustomEvent<{type: string, value: number}>) => void点击确定按钮后触发-
onChange(e: CustomEvent<{type: string, value: number}) => void列表变化是触发-
onCancel(e: CustomEvent<{type: string, value: number}) => void点击取消按钮后触发-

外部样式类 External Classes

名称说明备注
extHairlineClass时间选择器中间线样式-
extMaskClass时间选择器蒙层样式-
extOptionClass时间选择器纵列容器样式-
extOptionItemClass时间选择器纵列单项样式-
extClass时间选择容器样式-

CSS 变量 CSS Variable

变量默认值说明备注
--datetime-picker-mask-bg-image#ffffffpicker 组件 --picker-mask-bg-image-
--datetime-picker-loading-bg-color#ffffffpicker 组件 --picker-loading-bg-color-
--datetime-picker-row-color#212121picker-column 组件 --picker-column-row-color-
--datetime-picker-row-disabled-color#212121picker-column 组件 --picker-column-row-disabled-color-