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