1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var useUtils_1 = require("./useUtils");
|
4 | var useOpenState_1 = require("./useOpenState");
|
5 | var text_field_helper_1 = require("../../_helpers/text-field-helper");
|
6 | var react_1 = require("react");
|
7 | var useValueToDate = function (utils, _a) {
|
8 | var value = _a.value, initialFocusedDate = _a.initialFocusedDate;
|
9 | var nowRef = react_1.useRef(utils.date());
|
10 | var date = utils.date(value || initialFocusedDate || nowRef.current);
|
11 | return date && utils.isValid(date) ? date : nowRef.current;
|
12 | };
|
13 | function useDateValues(props, options) {
|
14 | var utils = useUtils_1.useUtils();
|
15 | var date = useValueToDate(utils, props);
|
16 | var format = props.format || options.getDefaultFormat();
|
17 | return { date: date, format: format };
|
18 | }
|
19 | function usePickerState(props, options) {
|
20 | var autoOk = props.autoOk, disabled = props.disabled, readOnly = props.readOnly, onAccept = props.onAccept, onChange = props.onChange, onError = props.onError, value = props.value, variant = props.variant;
|
21 | var utils = useUtils_1.useUtils();
|
22 | var _a = useOpenState_1.useOpenState(props), isOpen = _a.isOpen, setIsOpen = _a.setIsOpen;
|
23 | var _b = useDateValues(props, options), date = _b.date, format = _b.format;
|
24 | var _c = react_1.useState(date), pickerDate = _c[0], setPickerDate = _c[1];
|
25 | react_1.useEffect(function () {
|
26 |
|
27 | if (!isOpen && !utils.isEqual(pickerDate, date)) {
|
28 | setPickerDate(date);
|
29 | }
|
30 | }, [date, isOpen, pickerDate, utils]);
|
31 | var acceptDate = react_1.useCallback(function (acceptedDate) {
|
32 | onChange(acceptedDate);
|
33 | if (onAccept) {
|
34 | onAccept(acceptedDate);
|
35 | }
|
36 | setIsOpen(false);
|
37 | }, [onAccept, onChange, setIsOpen]);
|
38 | var wrapperProps = react_1.useMemo(function () { return ({
|
39 | format: format,
|
40 | open: isOpen,
|
41 | onClear: function () { return acceptDate(null); },
|
42 | onAccept: function () { return acceptDate(pickerDate); },
|
43 | onSetToday: function () { return setPickerDate(utils.date()); },
|
44 | onDismiss: function () {
|
45 | setIsOpen(false);
|
46 | },
|
47 | }); }, [acceptDate, format, isOpen, pickerDate, setIsOpen, utils]);
|
48 | var pickerProps = react_1.useMemo(function () { return ({
|
49 | date: pickerDate,
|
50 | onChange: function (newDate, isFinish) {
|
51 | if (isFinish === void 0) { isFinish = true; }
|
52 | setPickerDate(newDate);
|
53 | if (isFinish && autoOk) {
|
54 | acceptDate(newDate);
|
55 | return;
|
56 | }
|
57 |
|
58 | if (variant === 'inline' || variant === 'static') {
|
59 | onChange(newDate);
|
60 | onAccept && onAccept(newDate);
|
61 | }
|
62 | },
|
63 | }); }, [acceptDate, autoOk, onAccept, onChange, pickerDate, variant]);
|
64 | var validationError = text_field_helper_1.validate(value, utils, props);
|
65 | react_1.useEffect(function () {
|
66 | if (onError) {
|
67 | onError(validationError, value);
|
68 | }
|
69 | }, [onError, validationError, value]);
|
70 | var inputValue = text_field_helper_1.getDisplayDate(date, format, utils, value === null, props);
|
71 | var inputProps = react_1.useMemo(function () { return ({
|
72 | inputValue: inputValue,
|
73 | validationError: validationError,
|
74 | openPicker: function () { return !readOnly && !disabled && setIsOpen(true); },
|
75 | }); }, [disabled, inputValue, readOnly, setIsOpen, validationError]);
|
76 | var pickerState = { pickerProps: pickerProps, inputProps: inputProps, wrapperProps: wrapperProps };
|
77 | react_1.useDebugValue(pickerState);
|
78 | return pickerState;
|
79 | }
|
80 | exports.usePickerState = usePickerState;
|