1 | import React, { useCallback, useMemo } from 'react';
|
2 | import PickerView from '../picker-view';
|
3 | import { withNativeProps } from '../../utils/native-props';
|
4 | import { mergeProps } from '../../utils/with-default-props';
|
5 | import { usePropsValue } from '../../utils/use-props-value';
|
6 | import { generateDatePickerColumns, convertDateToStringArray, convertStringArrayToDate } from '../date-picker/date-picker-utils';
|
7 | import useRenderLabel from './useRenderLabel';
|
8 | import { TILL_NOW } from '../date-picker/util';
|
9 | const thisYear = new Date().getFullYear();
|
10 | const defaultProps = {
|
11 | min: new Date(new Date().setFullYear(thisYear - 10)),
|
12 | max: new Date(new Date().setFullYear(thisYear + 10)),
|
13 | precision: 'day'
|
14 | };
|
15 | export const DatePickerView = p => {
|
16 | var _a;
|
17 | const props = mergeProps(defaultProps, p);
|
18 | const {
|
19 | renderLabel
|
20 | } = props;
|
21 | const [value, setValue] = usePropsValue({
|
22 | value: props.value,
|
23 | defaultValue: (_a = props.defaultValue) !== null && _a !== void 0 ? _a : null
|
24 | });
|
25 | const mergedRenderLabel = useRenderLabel(renderLabel);
|
26 | const pickerValue = useMemo(() => {
|
27 | if (value === null || value === void 0 ? void 0 : value.tillNow) {
|
28 | return [TILL_NOW, null, null];
|
29 | }
|
30 | return convertDateToStringArray(value, props.precision);
|
31 | }, [value, props.precision]);
|
32 | const onChange = useCallback(val => {
|
33 | var _a;
|
34 | const date = convertStringArrayToDate(val, props.precision);
|
35 | if (date) {
|
36 | setValue(date);
|
37 | (_a = props.onChange) === null || _a === void 0 ? void 0 : _a.call(props, date);
|
38 | }
|
39 | }, [props.onChange, props.precision]);
|
40 | return withNativeProps(props, React.createElement(PickerView, {
|
41 | columns: selected => generateDatePickerColumns(selected, props.min, props.max, props.precision, mergedRenderLabel, props.filter, props.tillNow),
|
42 | loading: props.loading,
|
43 | loadingContent: props.loadingContent,
|
44 | value: pickerValue,
|
45 | mouseWheel: props.mouseWheel,
|
46 | onChange: onChange
|
47 | }));
|
48 | }; |
\ | No newline at end of file |