import React from 'react' import { run, isExist } from '@fexd/tools' import { Tooltip, Hook, dayjsTZ } from '@fexd/pro-utils' import { useProContext } from '@fexd/pro-provider' import useLocales from '../../locales' import { defineTypes } from '../types-define' import useDayjsLocale from './useDayjsLocale' import { formatDateValue, toValidMomentValue, normalizeMomentValue } from './time-utils' import { TZ_DatePicker, TZ_TimePicker } from './fixAntdTimezone' import FromNowTooltip from './FromNowTooltip' const types = defineTypes({ // 日期 date: { renderField: ({ fieldProps: props = {} } = {}) => ( {(props) => { const { t } = useLocales(({ t }) => [t]) const { dayFormat: ctxDayFormat } = useProContext() const format = props?.format ?? ctxDayFormat ?? 'YYYY-MM-DD' return }} ), normalizeValue: (value) => normalizeMomentValue(value), normalizeFieldValue: (value) => toValidMomentValue(value), renderView: (value, config = {}) => !isExist(value) ? ( '--' ) : ( {() => { const { dayFormat: ctxDayFormat } = useProContext() const format = config?.format ?? ctxDayFormat ?? 'YYYY-MM-DD' return }} ), }, // 日期时间 dateTime: { renderField: ({ fieldProps: props = {} } = {}) => ( {(props) => { const { t } = useLocales(({ t }) => [t]) const { dayFormat: ctxDayFormat } = useProContext() const format = props?.format ?? `${ctxDayFormat ?? 'YYYY-MM-DD'} HH:mm:ss` return }} ), normalizeValue: (value) => normalizeMomentValue(value), normalizeFieldValue: (value) => toValidMomentValue(value), renderView: (value, config = {}) => !isExist(value) ? ( '--' ) : ( {() => { const { dayFormat: ctxDayFormat } = useProContext() const format = config?.format ?? `${ctxDayFormat ?? 'YYYY-MM-DD'} HH:mm:ss` return }} ), }, // 周 dateWeek: { renderField: ({ fieldProps: props = {} } = {}) => ( {(props) => { const { t } = useLocales(({ t }) => [t]) return }} ), normalizeValue: (value) => normalizeMomentValue(value), normalizeFieldValue: (value) => toValidMomentValue(value), renderView: (value, config = {}) => !isExist(value) ? ( '--' ) : ( ), }, // 月 dateMonth: { renderField: ({ fieldProps: props = {} } = {}) => ( {(props) => { const { t } = useLocales(({ t }) => [t]) return }} ), normalizeValue: (value) => normalizeMomentValue(value), normalizeFieldValue: (value) => toValidMomentValue(value), renderView: (value, config = {}) => !isExist(value) ? ( '--' ) : ( ), }, // 季度输入 dateQuarter: { renderField: ({ fieldProps: props = {} } = {}) => ( {(props) => { const { t } = useLocales(({ t }) => [t]) return }} ), normalizeValue: (value) => normalizeMomentValue(value), normalizeFieldValue: (value) => toValidMomentValue(value), renderView: (value, config = {}) => !isExist(value) ? ( '--' ) : ( ), }, // 年份输入 dateYear: { renderField: ({ fieldProps: props = {} } = {}) => ( {(props) => { const { t } = useLocales(({ t }) => [t]) return }} ), normalizeValue: (value) => normalizeMomentValue(value), normalizeFieldValue: (value) => toValidMomentValue(value), renderView: (value, config = {}) => !isExist(value) ? ( '--' ) : ( ), }, // 时间 time: { renderField: ({ fieldProps: props = {} } = {}) => ( {(props) => { const { t } = useLocales(({ t }) => [t]) return }} ), normalizeValue: (value) => normalizeMomentValue(value), normalizeFieldValue: (value) => toValidMomentValue(value), renderView: (value, config = {}) => !isExist(value) ? ( '--' ) : ( ), }, // 相对于当前时间 fromNow: { renderField: ({ fieldProps: props = {} } = {}) => ( {(props) => { const { t } = useLocales(({ t }) => [t]) const { dayFormat: ctxDayFormat } = useProContext() const format = props?.format ?? `${ctxDayFormat ?? 'YYYY-MM-DD'} HH:mm:ss` return }} ), normalizeValue: (value) => normalizeMomentValue(value), normalizeFieldValue: (value) => toValidMomentValue(value), renderView: (value, config = {}) => !isExist(value) ? ( '--' ) : ( {() => { const localeKey = useDayjsLocale() return ( {() => { const { dayFormat: ctxDayFormat } = useProContext() const format = config?.format ?? `${ctxDayFormat ?? 'YYYY-MM-DD'} HH:mm:ss` return formatDateValue(value, format, localeKey) }} } > {run(dayjsTZ(value).locale(localeKey), 'fromNow')} ) }} ), }, }) export default types export { formatDateValue, toValidMomentValue, normalizeMomentValue } from './time-utils'