forked from ddrilling/asb_cloud_front
TimeOnly заменено на TimeDto
This commit is contained in:
parent
c768414421
commit
5b5d135bec
@ -2,25 +2,25 @@ import { Moment } from 'moment'
|
|||||||
import { TimePicker, TimePickerProps } from 'antd'
|
import { TimePicker, TimePickerProps } from 'antd'
|
||||||
import { memo, useCallback, useMemo } from 'react'
|
import { memo, useCallback, useMemo } from 'react'
|
||||||
|
|
||||||
import { defaultTimeFormat, momentToTimeOnly, timeOnlyToMoment } from '@utils/datetime'
|
import { defaultTimeFormat, momentToTime, timeToMoment } from '@utils/datetime'
|
||||||
import { TimeOnly } from '@api'
|
import { TimeDto } from '@api'
|
||||||
|
|
||||||
export type TimePickerWrapperProps = Omit<Omit<TimePickerProps, 'value'>, 'onChange'> & {
|
export type TimePickerWrapperProps = Omit<Omit<TimePickerProps, 'value'>, 'onChange'> & {
|
||||||
value?: TimeOnly,
|
value?: TimeDto,
|
||||||
onChange?: (date: TimeOnly | null) => any
|
onChange?: (date: TimeDto | null) => any
|
||||||
isUTC?: boolean
|
isUTC?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const TimePickerWrapper = memo<TimePickerWrapperProps>(({ value, onChange, isUTC, ...other }) => {
|
export const TimePickerWrapper = memo<TimePickerWrapperProps>(({ value, onChange, isUTC, ...other }) => {
|
||||||
const time = useMemo(() => value ? timeOnlyToMoment(value, isUTC) : null, [value, isUTC])
|
const time = useMemo(() => value ? timeToMoment(value, isUTC) : null, [value, isUTC])
|
||||||
|
|
||||||
const onTimeChange = useCallback((time: Moment | null) => onChange?.(time ? momentToTimeOnly(time) : null), [onChange])
|
const onTimeChange = useCallback((time: Moment | null) => onChange?.(time ? momentToTime(time) : null), [onChange])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TimePicker
|
<TimePicker
|
||||||
allowClear={false}
|
allowClear={false}
|
||||||
format={defaultTimeFormat}
|
format={defaultTimeFormat}
|
||||||
defaultValue={timeOnlyToMoment()}
|
defaultValue={timeToMoment()}
|
||||||
onChange={onTimeChange}
|
onChange={onTimeChange}
|
||||||
value={time}
|
value={time}
|
||||||
{...other}
|
{...other}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { timeOnlyToMoment } from '@utils/datetime'
|
import { timeToMoment } from '@utils/datetime'
|
||||||
import { isRawDate } from '@utils'
|
import { isRawDate } from '@utils'
|
||||||
import { TimeOnly } from '@api'
|
import { TimeDto } from '@api'
|
||||||
|
|
||||||
import { DataType } from './Columns'
|
import { DataType } from './Columns'
|
||||||
|
|
||||||
@ -26,7 +26,7 @@ export const makeDateSorter = <T extends unknown>(key: keyof DataType<T>) => (a:
|
|||||||
return date.getTime() - new Date(bdate).getTime()
|
return date.getTime() - new Date(bdate).getTime()
|
||||||
}
|
}
|
||||||
|
|
||||||
export const makeTimeSorter = <T extends TimeOnly>(key: keyof DataType<T>) => (a: DataType<T>, b: DataType<T>) => {
|
export const makeTimeSorter = <T extends TimeDto>(key: keyof DataType<T>) => (a: DataType<T>, b: DataType<T>) => {
|
||||||
const elma = a[key]
|
const elma = a[key]
|
||||||
const elmb = b[key]
|
const elmb = b[key]
|
||||||
|
|
||||||
@ -34,5 +34,5 @@ export const makeTimeSorter = <T extends TimeOnly>(key: keyof DataType<T>) => (a
|
|||||||
if (!elma) return 1
|
if (!elma) return 1
|
||||||
if (!elmb) return -1
|
if (!elmb) return -1
|
||||||
|
|
||||||
return timeOnlyToMoment(elma).diff(timeOnlyToMoment(elmb))
|
return timeToMoment(elma).diff(timeToMoment(elmb))
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import moment, { Moment } from 'moment'
|
import moment, { Moment } from 'moment'
|
||||||
|
|
||||||
import { SimpleTimezoneDto, TimeOnly } from '@api'
|
import { SimpleTimezoneDto, TimeDto } from '@api'
|
||||||
|
|
||||||
export type RawDate = number | string | Date
|
export type RawDate = number | string | Date
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ export function isRawDate(value: unknown): value is RawDate {
|
|||||||
return !isNaN(Date.parse(String(value)))
|
return !isNaN(Date.parse(String(value)))
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isTimeOnly(value: unknown): value is TimeOnly {
|
export function isTime(value: unknown): value is TimeDto {
|
||||||
if (!value || typeof value !== 'object')
|
if (!value || typeof value !== 'object')
|
||||||
return false
|
return false
|
||||||
const keys = Object.keys(value)
|
const keys = Object.keys(value)
|
||||||
@ -34,8 +34,8 @@ export const formatDate = (date: unknown, utc: boolean = false, format: string =
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const formatTime = (time: unknown, utc: boolean = false, format: string = defaultTimeFormat) => {
|
export const formatTime = (time: unknown, utc: boolean = false, format: string = defaultTimeFormat) => {
|
||||||
if(!isTimeOnly(time)) return
|
if(!isTime(time)) return
|
||||||
const out = timeOnlyToMoment(time, utc, format)
|
const out = timeToMoment(time, utc, format)
|
||||||
return out.format(format)
|
return out.format(format)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,15 +92,13 @@ export const findTimezoneId = (value: SimpleTimezoneDto): TimezoneId =>
|
|||||||
(isTimezoneId(value.timezoneId) && value.timezoneId) ||
|
(isTimezoneId(value.timezoneId) && value.timezoneId) ||
|
||||||
(Object.keys(rawTimezones) as TimezoneId[]).find(id => rawTimezones[id] === value.hours) as TimezoneId
|
(Object.keys(rawTimezones) as TimezoneId[]).find(id => rawTimezones[id] === value.hours) as TimezoneId
|
||||||
|
|
||||||
export const timeOnlyToMoment = (time?: TimeOnly | null, isUtc?: boolean, format: string = defaultTimeFormat): Moment => {
|
export const timeToMoment = (time?: TimeDto | null, isUtc?: boolean, format: string = defaultTimeFormat): Moment => {
|
||||||
const input = `${time?.hour ?? 0}:${time?.minute ?? 0}:${time?.second ?? 0}`
|
const input = `${time?.hour ?? 0}:${time?.minute ?? 0}:${time?.second ?? 0}`
|
||||||
return isUtc ? moment.utc(input, format).local() : moment(input, format)
|
return isUtc ? moment.utc(input, format).local() : moment(input, format)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const momentToTimeOnly = (time?: Moment | null): TimeOnly => ({
|
export const momentToTime = (time?: Moment | null): TimeDto => ({
|
||||||
hour: time?.hour() ?? 0,
|
hour: time?.hour() ?? 0,
|
||||||
minute: time?.minute() ?? 0,
|
minute: time?.minute() ?? 0,
|
||||||
second: time?.second() ?? 0,
|
second: time?.second() ?? 0,
|
||||||
millisecond: 0,
|
|
||||||
ticks: 0,
|
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user