This repository has been archived by the owner on Jan 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
DatePickerField.tsx
141 lines (128 loc) · 4.4 KB
/
DatePickerField.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React, { RefAttributes } from 'react'
import moment, { Moment } from 'moment'
import { DatePicker } from 'antd'
import { DatePickerProps } from 'antd/es/date-picker/interface'
import * as styles from './DatePickerField.less'
import cn from 'classnames'
import ReadOnlyField from '../ReadOnlyField/ReadOnlyField'
import { BaseFieldProps } from '../../Field/Field'
export interface IDatePickerFieldProps extends BaseFieldProps {
value?: string | null
onChange?: (date: string | null) => void
showToday?: boolean
allowClear?: boolean
onOpenChange?: (status: boolean) => void
disabledDate?: (current: moment.Moment) => boolean
showTime?: boolean
monthYear?: boolean
showSeconds?: boolean
resetForceFocus?: () => void
dateFormatter?: (date: moment.Moment) => string
calendarContainer?: HTMLElement
filterValue?: string
}
const dateFormat = moment.ISO_8601
const outputMonthYearFormat = 'MMMM YYYY'
const outputDateFormat = 'DD.MM.YYYY'
const outputDateTimeFormat = 'DD.MM.YYYY HH:mm'
const outputDateTimeWithSecondsFormat = 'DD.MM.YYYY HH:mm:ss'
const isoLocalFormatter = (date: Moment) => date.format('YYYY-MM-DD[T]HH:mm:ss')
/**
*
* @param props
* @category Components
*/
const DatePickerField: React.FunctionComponent<IDatePickerFieldProps> = ({
disabled,
value,
showTime,
showSeconds,
monthYear,
widgetName,
meta,
className,
backgroundColor,
readOnly,
onDrillDown,
onChange,
...props
}) => {
const dateFormatter = props.dateFormatter ? props.dateFormatter : isoLocalFormatter
const datePickerRef = React.useRef(null)
const handleChange = React.useCallback(
(date: moment.Moment) => {
if (onChange) {
if (monthYear) {
onChange(date ? dateFormatter(date.startOf('month')) : null)
} else {
onChange(date ? dateFormatter(date) : null)
}
}
},
[onChange, monthYear, dateFormatter]
)
const getCalendarContainer = React.useCallback((triggerNode: Element) => props.calendarContainer, [props.calendarContainer])
let momentObject
if (value) {
momentObject = monthYear ? moment(value, dateFormat, true).startOf('month') : moment(value, dateFormat, true)
}
const format = getFormat(showTime, showSeconds, monthYear)
const timeOptions = showTime ? { format: showSeconds ? 'HH:mm:ss' : 'HH:mm' } : null
const extendedProps: DatePickerProps & RefAttributes<any> = {
...props,
className: cn(styles.datePicker, className),
value: momentObject,
disabled: disabled,
format,
onChange: handleChange,
showTime: timeOptions,
style: {
backgroundColor
},
getCalendarContainer: props.calendarContainer ? getCalendarContainer : null,
ref: datePickerRef
}
if (disabled) {
extendedProps.open = false
}
if (readOnly) {
const datePickerFormat = DatePickerFieldFormat(value, showTime, showSeconds, monthYear)
return (
<ReadOnlyField
widgetName={widgetName}
meta={meta}
className={className}
backgroundColor={backgroundColor}
onDrillDown={onDrillDown}
>
{datePickerFormat}
</ReadOnlyField>
)
}
return monthYear ? <DatePicker.MonthPicker {...extendedProps} /> : <DatePicker {...extendedProps} />
}
export const getFormat = (showTime?: boolean, showSeconds?: boolean, monthYear?: boolean) => {
if (showSeconds) {
return outputDateTimeWithSecondsFormat
} else if (showTime) {
return outputDateTimeFormat
} else if (monthYear) {
return outputMonthYearFormat
} else {
return outputDateFormat
}
}
export const DatePickerFieldFormat = (date: string | null, withTime?: boolean, withSeconds?: boolean, monthYear?: boolean): string => {
if (monthYear) {
moment.locale('ru')
}
if (!date) {
return ''
}
return moment(date, dateFormat).format(getFormat(withTime, withSeconds, monthYear))
}
/**
* @category Components
*/
const MemoizedDatePickerField = React.memo(DatePickerField)
export default MemoizedDatePickerField