asb_cloud_front/src/components/Table/TimePickerWrapper.tsx

32 lines
1.0 KiB
TypeScript
Raw Normal View History

import { Moment } from 'moment'
import { TimePicker, TimePickerProps } from 'antd'
import { memo, useCallback, useMemo } from 'react'
2022-06-01 14:05:54 +05:00
import { defaultTimeFormat, momentToTime, timeToMoment } from '@utils/datetime'
import { TimeDto } from '@api'
export type TimePickerWrapperProps = Omit<Omit<TimePickerProps, 'value'>, 'onChange'> & {
2022-06-01 14:05:54 +05:00
value?: TimeDto,
onChange?: (date: TimeDto | null) => any
isUTC?: boolean
}
export const TimePickerWrapper = memo<TimePickerWrapperProps>(({ value, onChange, isUTC, ...other }) => {
2022-06-01 14:05:54 +05:00
const time = useMemo(() => value ? timeToMoment(value, isUTC) : null, [value, isUTC])
2022-06-01 14:05:54 +05:00
const onTimeChange = useCallback((time: Moment | null) => onChange?.(time ? momentToTime(time) : null), [onChange])
return (
<TimePicker
allowClear={false}
format={defaultTimeFormat}
2022-06-01 14:05:54 +05:00
defaultValue={timeToMoment()}
onChange={onTimeChange}
value={time}
{...other}
/>
)
})
export default TimePickerWrapper