@mui/x-date-pickers 8.0.0-alpha.13 → 8.0.0-alpha.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +131 -0
- package/DatePicker/shared.d.ts +1 -1
- package/DateTimePicker/DateTimePicker.types.d.ts +2 -2
- package/DateTimePicker/DateTimePickerToolbar.d.ts +1 -1
- package/DateTimePicker/DateTimePickerToolbar.js +1 -1
- package/DateTimePicker/shared.d.ts +18 -10
- package/DateTimePicker/shared.js +23 -2
- package/DesktopDateTimePicker/DesktopDateTimePicker.js +2 -11
- package/DesktopDateTimePicker/DesktopDateTimePicker.types.d.ts +4 -7
- package/DesktopTimePicker/DesktopTimePicker.types.d.ts +2 -2
- package/MobileDateTimePicker/MobileDateTimePicker.d.ts +1 -2
- package/MobileDateTimePicker/MobileDateTimePicker.js +61 -6
- package/MobileDateTimePicker/MobileDateTimePicker.types.d.ts +1 -3
- package/PickersTextField/PickersTextField.js +3 -1
- package/StaticDateTimePicker/StaticDateTimePicker.js +60 -6
- package/StaticDateTimePicker/StaticDateTimePicker.types.d.ts +1 -2
- package/TimePicker/shared.d.ts +1 -1
- package/esm/DatePicker/shared.d.ts +1 -1
- package/esm/DateTimePicker/DateTimePicker.types.d.ts +2 -2
- package/esm/DateTimePicker/DateTimePickerToolbar.d.ts +1 -1
- package/esm/DateTimePicker/DateTimePickerToolbar.js +1 -1
- package/esm/DateTimePicker/shared.d.ts +18 -10
- package/esm/DateTimePicker/shared.js +23 -2
- package/esm/DesktopDateTimePicker/DesktopDateTimePicker.js +3 -12
- package/esm/DesktopDateTimePicker/DesktopDateTimePicker.types.d.ts +4 -7
- package/esm/DesktopTimePicker/DesktopTimePicker.types.d.ts +2 -2
- package/esm/MobileDateTimePicker/MobileDateTimePicker.d.ts +1 -2
- package/esm/MobileDateTimePicker/MobileDateTimePicker.js +62 -7
- package/esm/MobileDateTimePicker/MobileDateTimePicker.types.d.ts +1 -3
- package/esm/PickersTextField/PickersTextField.js +3 -1
- package/esm/StaticDateTimePicker/StaticDateTimePicker.js +61 -7
- package/esm/StaticDateTimePicker/StaticDateTimePicker.types.d.ts +1 -2
- package/esm/TimePicker/shared.d.ts +1 -1
- package/esm/index.js +1 -1
- package/esm/internals/components/PickerFieldUI.js +6 -2
- package/esm/internals/components/PickerPopper/PickerPopper.js +3 -3
- package/esm/internals/components/PickerProvider.d.ts +137 -6
- package/esm/internals/components/PickerProvider.js +1 -1
- package/esm/internals/hooks/useDesktopPicker/useDesktopPicker.types.d.ts +4 -7
- package/esm/internals/hooks/useField/useField.js +7 -8
- package/esm/internals/hooks/useField/useField.types.d.ts +46 -43
- package/esm/internals/hooks/useField/useField.utils.js +4 -1
- package/esm/internals/hooks/useField/useFieldCharacterEditing.js +2 -2
- package/esm/internals/hooks/useField/useFieldState.d.ts +2 -1
- package/esm/internals/hooks/useField/useFieldState.js +136 -83
- package/esm/internals/hooks/useField/useFieldV6TextField.js +1 -1
- package/esm/internals/hooks/useField/useFieldV7TextField.js +2 -1
- package/esm/internals/hooks/useMobilePicker/useMobilePicker.types.d.ts +4 -7
- package/esm/internals/hooks/useNullableFieldPrivateContext.d.ts +5 -2
- package/esm/internals/hooks/usePicker/hooks/useOrientation.d.ts +2 -0
- package/esm/internals/hooks/usePicker/hooks/useOrientation.js +34 -0
- package/esm/internals/hooks/usePicker/hooks/useValueAndOpenStates.d.ts +19 -0
- package/esm/internals/hooks/usePicker/hooks/useValueAndOpenStates.js +210 -0
- package/esm/internals/hooks/usePicker/index.d.ts +1 -3
- package/esm/internals/hooks/usePicker/usePicker.d.ts +5 -5
- package/esm/internals/hooks/usePicker/usePicker.js +276 -34
- package/esm/internals/hooks/usePicker/usePicker.types.d.ts +181 -9
- package/esm/internals/hooks/useStaticPicker/useStaticPicker.types.d.ts +3 -4
- package/esm/internals/hooks/useValueWithTimezone.d.ts +1 -1
- package/esm/internals/index.d.ts +3 -6
- package/esm/internals/models/manager.d.ts +108 -4
- package/esm/internals/models/props/basePickerProps.d.ts +2 -2
- package/esm/internals/models/props/time.d.ts +1 -1
- package/esm/internals/utils/date-time-utils.d.ts +2 -2
- package/esm/internals/utils/valueManagers.d.ts +1 -1
- package/esm/internals/utils/valueManagers.js +10 -18
- package/esm/managers/useDateManager.d.ts +3 -4
- package/esm/managers/useDateTimeManager.d.ts +3 -4
- package/esm/managers/useTimeManager.d.ts +3 -4
- package/esm/models/adapters.d.ts +2 -1
- package/esm/models/manager.d.ts +4 -5
- package/esm/validation/useValidation.d.ts +1 -1
- package/index.js +1 -1
- package/internals/components/PickerFieldUI.js +6 -2
- package/internals/components/PickerPopper/PickerPopper.js +3 -3
- package/internals/components/PickerProvider.d.ts +137 -6
- package/internals/components/PickerProvider.js +1 -1
- package/internals/hooks/useDesktopPicker/useDesktopPicker.types.d.ts +4 -7
- package/internals/hooks/useField/useField.js +7 -8
- package/internals/hooks/useField/useField.types.d.ts +46 -43
- package/internals/hooks/useField/useField.utils.js +4 -1
- package/internals/hooks/useField/useFieldCharacterEditing.js +2 -2
- package/internals/hooks/useField/useFieldState.d.ts +2 -1
- package/internals/hooks/useField/useFieldState.js +135 -82
- package/internals/hooks/useField/useFieldV6TextField.js +1 -1
- package/internals/hooks/useField/useFieldV7TextField.js +2 -1
- package/internals/hooks/useMobilePicker/useMobilePicker.types.d.ts +4 -7
- package/internals/hooks/useNullableFieldPrivateContext.d.ts +5 -2
- package/internals/hooks/usePicker/hooks/useOrientation.d.ts +2 -0
- package/internals/hooks/usePicker/hooks/useOrientation.js +42 -0
- package/internals/hooks/usePicker/hooks/useValueAndOpenStates.d.ts +19 -0
- package/internals/hooks/usePicker/{usePickerValue.js → hooks/useValueAndOpenStates.js} +97 -114
- package/internals/hooks/usePicker/index.d.ts +1 -3
- package/internals/hooks/usePicker/usePicker.d.ts +5 -5
- package/internals/hooks/usePicker/usePicker.js +278 -34
- package/internals/hooks/usePicker/usePicker.types.d.ts +181 -9
- package/internals/hooks/useStaticPicker/useStaticPicker.types.d.ts +3 -4
- package/internals/hooks/useValueWithTimezone.d.ts +1 -1
- package/internals/index.d.ts +3 -6
- package/internals/models/manager.d.ts +108 -4
- package/internals/models/props/basePickerProps.d.ts +2 -2
- package/internals/models/props/time.d.ts +1 -1
- package/internals/utils/date-time-utils.d.ts +2 -2
- package/internals/utils/valueManagers.d.ts +1 -1
- package/internals/utils/valueManagers.js +10 -18
- package/managers/useDateManager.d.ts +3 -4
- package/managers/useDateTimeManager.d.ts +3 -4
- package/managers/useTimeManager.d.ts +3 -4
- package/models/adapters.d.ts +2 -1
- package/models/manager.d.ts +4 -5
- package/modern/DatePicker/shared.d.ts +1 -1
- package/modern/DateTimePicker/DateTimePicker.types.d.ts +2 -2
- package/modern/DateTimePicker/DateTimePickerToolbar.d.ts +1 -1
- package/modern/DateTimePicker/DateTimePickerToolbar.js +1 -1
- package/modern/DateTimePicker/shared.d.ts +18 -10
- package/modern/DateTimePicker/shared.js +23 -2
- package/modern/DesktopDateTimePicker/DesktopDateTimePicker.js +3 -12
- package/modern/DesktopDateTimePicker/DesktopDateTimePicker.types.d.ts +4 -7
- package/modern/DesktopTimePicker/DesktopTimePicker.types.d.ts +2 -2
- package/modern/MobileDateTimePicker/MobileDateTimePicker.d.ts +1 -2
- package/modern/MobileDateTimePicker/MobileDateTimePicker.js +62 -7
- package/modern/MobileDateTimePicker/MobileDateTimePicker.types.d.ts +1 -3
- package/modern/PickersTextField/PickersTextField.js +3 -1
- package/modern/StaticDateTimePicker/StaticDateTimePicker.js +61 -7
- package/modern/StaticDateTimePicker/StaticDateTimePicker.types.d.ts +1 -2
- package/modern/TimePicker/shared.d.ts +1 -1
- package/modern/index.js +1 -1
- package/modern/internals/components/PickerFieldUI.js +6 -2
- package/modern/internals/components/PickerPopper/PickerPopper.js +3 -3
- package/modern/internals/components/PickerProvider.d.ts +137 -6
- package/modern/internals/components/PickerProvider.js +1 -1
- package/modern/internals/hooks/useDesktopPicker/useDesktopPicker.types.d.ts +4 -7
- package/modern/internals/hooks/useField/useField.js +7 -8
- package/modern/internals/hooks/useField/useField.types.d.ts +46 -43
- package/modern/internals/hooks/useField/useField.utils.js +4 -1
- package/modern/internals/hooks/useField/useFieldCharacterEditing.js +2 -2
- package/modern/internals/hooks/useField/useFieldState.d.ts +2 -1
- package/modern/internals/hooks/useField/useFieldState.js +136 -83
- package/modern/internals/hooks/useField/useFieldV6TextField.js +1 -1
- package/modern/internals/hooks/useField/useFieldV7TextField.js +2 -1
- package/modern/internals/hooks/useMobilePicker/useMobilePicker.types.d.ts +4 -7
- package/modern/internals/hooks/useNullableFieldPrivateContext.d.ts +5 -2
- package/modern/internals/hooks/usePicker/hooks/useOrientation.d.ts +2 -0
- package/modern/internals/hooks/usePicker/hooks/useOrientation.js +34 -0
- package/modern/internals/hooks/usePicker/hooks/useValueAndOpenStates.d.ts +19 -0
- package/modern/internals/hooks/usePicker/hooks/useValueAndOpenStates.js +210 -0
- package/modern/internals/hooks/usePicker/index.d.ts +1 -3
- package/modern/internals/hooks/usePicker/usePicker.d.ts +5 -5
- package/modern/internals/hooks/usePicker/usePicker.js +276 -34
- package/modern/internals/hooks/usePicker/usePicker.types.d.ts +181 -9
- package/modern/internals/hooks/useStaticPicker/useStaticPicker.types.d.ts +3 -4
- package/modern/internals/hooks/useValueWithTimezone.d.ts +1 -1
- package/modern/internals/index.d.ts +3 -6
- package/modern/internals/models/manager.d.ts +108 -4
- package/modern/internals/models/props/basePickerProps.d.ts +2 -2
- package/modern/internals/models/props/time.d.ts +1 -1
- package/modern/internals/utils/date-time-utils.d.ts +2 -2
- package/modern/internals/utils/valueManagers.d.ts +1 -1
- package/modern/internals/utils/valueManagers.js +10 -18
- package/modern/managers/useDateManager.d.ts +3 -4
- package/modern/managers/useDateTimeManager.d.ts +3 -4
- package/modern/managers/useTimeManager.d.ts +3 -4
- package/modern/models/adapters.d.ts +2 -1
- package/modern/models/manager.d.ts +4 -5
- package/modern/validation/useValidation.d.ts +1 -1
- package/package.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/validation/useValidation.d.ts +1 -1
- package/esm/internals/hooks/useOpenState.d.ts +0 -14
- package/esm/internals/hooks/useOpenState.js +0 -37
- package/esm/internals/hooks/usePicker/usePickerProvider.d.ts +0 -65
- package/esm/internals/hooks/usePicker/usePickerProvider.js +0 -125
- package/esm/internals/hooks/usePicker/usePickerValue.d.ts +0 -12
- package/esm/internals/hooks/usePicker/usePickerValue.js +0 -226
- package/esm/internals/hooks/usePicker/usePickerValue.types.d.ts +0 -327
- package/esm/internals/hooks/usePicker/usePickerValue.types.js +0 -1
- package/esm/internals/hooks/usePicker/usePickerViews.d.ts +0 -137
- package/esm/internals/hooks/usePicker/usePickerViews.js +0 -188
- package/internals/hooks/useOpenState.d.ts +0 -14
- package/internals/hooks/useOpenState.js +0 -46
- package/internals/hooks/usePicker/usePickerProvider.d.ts +0 -65
- package/internals/hooks/usePicker/usePickerProvider.js +0 -135
- package/internals/hooks/usePicker/usePickerValue.d.ts +0 -12
- package/internals/hooks/usePicker/usePickerValue.types.d.ts +0 -327
- package/internals/hooks/usePicker/usePickerValue.types.js +0 -5
- package/internals/hooks/usePicker/usePickerViews.d.ts +0 -137
- package/internals/hooks/usePicker/usePickerViews.js +0 -195
- package/modern/internals/hooks/useOpenState.d.ts +0 -14
- package/modern/internals/hooks/useOpenState.js +0 -37
- package/modern/internals/hooks/usePicker/usePickerProvider.d.ts +0 -65
- package/modern/internals/hooks/usePicker/usePickerProvider.js +0 -125
- package/modern/internals/hooks/usePicker/usePickerValue.d.ts +0 -12
- package/modern/internals/hooks/usePicker/usePickerValue.js +0 -226
- package/modern/internals/hooks/usePicker/usePickerValue.types.d.ts +0 -327
- package/modern/internals/hooks/usePicker/usePickerValue.types.js +0 -1
- package/modern/internals/hooks/usePicker/usePickerViews.d.ts +0 -137
- package/modern/internals/hooks/usePicker/usePickerViews.js +0 -188
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { warnOnce } from '@mui/x-internals/warning';
|
|
4
|
+
import useEventCallback from '@mui/utils/useEventCallback';
|
|
5
|
+
import { useValueWithTimezone } from "../../useValueWithTimezone.js";
|
|
6
|
+
import { useUtils } from "../../useUtils.js";
|
|
7
|
+
import { useValidation } from "../../../../validation/index.js";
|
|
8
|
+
export function useValueAndOpenStates(parameters) {
|
|
9
|
+
const {
|
|
10
|
+
props,
|
|
11
|
+
valueManager,
|
|
12
|
+
validator
|
|
13
|
+
} = parameters;
|
|
14
|
+
const {
|
|
15
|
+
value: valueProp,
|
|
16
|
+
defaultValue: defaultValueProp,
|
|
17
|
+
onChange,
|
|
18
|
+
referenceDate,
|
|
19
|
+
timezone: timezoneProp,
|
|
20
|
+
onAccept,
|
|
21
|
+
closeOnSelect,
|
|
22
|
+
open: openProp,
|
|
23
|
+
onOpen,
|
|
24
|
+
onClose
|
|
25
|
+
} = props;
|
|
26
|
+
const {
|
|
27
|
+
current: defaultValue
|
|
28
|
+
} = React.useRef(defaultValueProp);
|
|
29
|
+
const {
|
|
30
|
+
current: isValueControlled
|
|
31
|
+
} = React.useRef(valueProp !== undefined);
|
|
32
|
+
const {
|
|
33
|
+
current: isOpenControlled
|
|
34
|
+
} = React.useRef(openProp !== undefined);
|
|
35
|
+
const [previousTimezoneProp, setPreviousTimezoneProp] = React.useState(timezoneProp);
|
|
36
|
+
const utils = useUtils();
|
|
37
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
38
|
+
if (props.renderInput != null) {
|
|
39
|
+
warnOnce(['MUI X: The `renderInput` prop has been removed in version 6.0 of the Date and Time Pickers.', 'You can replace it with the `textField` component slot in most cases.', 'For more information, please have a look at the migration guide (https://mui.com/x/migration/migration-pickers-v5/#input-renderer-required-in-v5).']);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/* eslint-disable react-hooks/rules-of-hooks, react-hooks/exhaustive-deps */
|
|
44
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
45
|
+
React.useEffect(() => {
|
|
46
|
+
if (isValueControlled !== (valueProp !== undefined)) {
|
|
47
|
+
console.error([`MUI X: A component is changing the ${isValueControlled ? '' : 'un'}controlled value of a picker to be ${isValueControlled ? 'un' : ''}controlled.`, 'Elements should not switch from uncontrolled to controlled (or vice versa).', `Decide between using a controlled or uncontrolled value` + 'for the lifetime of the component.', "The nature of the state is determined during the first render. It's considered controlled if the value is not `undefined`.", 'More info: https://fb.me/react-controlled-components'].join('\n'));
|
|
48
|
+
}
|
|
49
|
+
}, [valueProp]);
|
|
50
|
+
React.useEffect(() => {
|
|
51
|
+
if (!isValueControlled && defaultValue !== defaultValueProp) {
|
|
52
|
+
console.error([`MUI X: A component is changing the defaultValue of an uncontrolled picker after being initialized. ` + `To suppress this warning opt to use a controlled value.`].join('\n'));
|
|
53
|
+
}
|
|
54
|
+
}, [JSON.stringify(defaultValue)]);
|
|
55
|
+
}
|
|
56
|
+
/* eslint-enable react-hooks/rules-of-hooks, react-hooks/exhaustive-deps */
|
|
57
|
+
|
|
58
|
+
const {
|
|
59
|
+
timezone,
|
|
60
|
+
value: valuePropWithTimezoneToRender,
|
|
61
|
+
handleValueChange
|
|
62
|
+
} = useValueWithTimezone({
|
|
63
|
+
timezone: timezoneProp,
|
|
64
|
+
value: valueProp,
|
|
65
|
+
defaultValue,
|
|
66
|
+
referenceDate,
|
|
67
|
+
onChange,
|
|
68
|
+
valueManager
|
|
69
|
+
});
|
|
70
|
+
const [state, setState] = React.useState(() => {
|
|
71
|
+
let initialValue;
|
|
72
|
+
if (valuePropWithTimezoneToRender !== undefined) {
|
|
73
|
+
initialValue = valuePropWithTimezoneToRender;
|
|
74
|
+
} else if (defaultValue !== undefined) {
|
|
75
|
+
initialValue = defaultValue;
|
|
76
|
+
} else {
|
|
77
|
+
initialValue = valueManager.emptyValue;
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
open: false,
|
|
81
|
+
draft: initialValue,
|
|
82
|
+
lastPublishedValue: initialValue,
|
|
83
|
+
lastCommittedValue: initialValue,
|
|
84
|
+
lastControlledValue: valueProp,
|
|
85
|
+
hasBeenModifiedSinceMount: false
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
const {
|
|
89
|
+
getValidationErrorForNewValue
|
|
90
|
+
} = useValidation({
|
|
91
|
+
props,
|
|
92
|
+
validator,
|
|
93
|
+
timezone,
|
|
94
|
+
value: state.draft,
|
|
95
|
+
onError: props.onError
|
|
96
|
+
});
|
|
97
|
+
const timezoneFromDraftValue = valueManager.getTimezone(utils, state.draft);
|
|
98
|
+
if (previousTimezoneProp !== timezoneProp) {
|
|
99
|
+
setPreviousTimezoneProp(timezoneProp);
|
|
100
|
+
if (timezoneProp && timezoneFromDraftValue && timezoneProp !== timezoneFromDraftValue) {
|
|
101
|
+
setState(prev => _extends({}, prev, {
|
|
102
|
+
draft: valueManager.setTimezone(utils, timezoneProp, prev.draft)
|
|
103
|
+
}));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const setOpen = useEventCallback(action => {
|
|
107
|
+
const newOpen = typeof action === 'function' ? action(state.open) : action;
|
|
108
|
+
if (!isOpenControlled) {
|
|
109
|
+
setState(prevState => _extends({}, prevState, {
|
|
110
|
+
open: newOpen
|
|
111
|
+
}));
|
|
112
|
+
}
|
|
113
|
+
if (newOpen && onOpen) {
|
|
114
|
+
onOpen();
|
|
115
|
+
}
|
|
116
|
+
if (!newOpen && onClose) {
|
|
117
|
+
onClose();
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
const setValue = useEventCallback((newValue, options) => {
|
|
121
|
+
const {
|
|
122
|
+
changeImportance = 'accept',
|
|
123
|
+
skipPublicationIfPristine = false,
|
|
124
|
+
validationError,
|
|
125
|
+
shortcut,
|
|
126
|
+
shouldClose = changeImportance === 'accept'
|
|
127
|
+
} = options ?? {};
|
|
128
|
+
let shouldPublish;
|
|
129
|
+
let shouldCommit;
|
|
130
|
+
if (!skipPublicationIfPristine && !isValueControlled && !state.hasBeenModifiedSinceMount) {
|
|
131
|
+
// If the value is not controlled and the value has never been modified before,
|
|
132
|
+
// Then clicking on any value (including the one equal to `defaultValue`) should call `onChange` and `onAccept`
|
|
133
|
+
shouldPublish = true;
|
|
134
|
+
shouldCommit = changeImportance === 'accept';
|
|
135
|
+
} else {
|
|
136
|
+
shouldPublish = !valueManager.areValuesEqual(utils, newValue, state.lastPublishedValue);
|
|
137
|
+
shouldCommit = changeImportance === 'accept' && !valueManager.areValuesEqual(utils, newValue, state.lastCommittedValue);
|
|
138
|
+
}
|
|
139
|
+
setState(prev => _extends({}, prev, {
|
|
140
|
+
draft: newValue,
|
|
141
|
+
lastPublishedValue: shouldPublish ? newValue : prev.lastPublishedValue,
|
|
142
|
+
lastCommittedValue: shouldCommit ? newValue : prev.lastCommittedValue,
|
|
143
|
+
hasBeenModifiedSinceMount: true
|
|
144
|
+
}));
|
|
145
|
+
let cachedContext = null;
|
|
146
|
+
const getContext = () => {
|
|
147
|
+
if (!cachedContext) {
|
|
148
|
+
cachedContext = {
|
|
149
|
+
validationError: validationError == null ? getValidationErrorForNewValue(newValue) : validationError
|
|
150
|
+
};
|
|
151
|
+
if (shortcut) {
|
|
152
|
+
cachedContext.shortcut = shortcut;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return cachedContext;
|
|
156
|
+
};
|
|
157
|
+
if (shouldPublish) {
|
|
158
|
+
handleValueChange(newValue, getContext());
|
|
159
|
+
}
|
|
160
|
+
if (shouldCommit && onAccept) {
|
|
161
|
+
onAccept(newValue, getContext());
|
|
162
|
+
}
|
|
163
|
+
if (shouldClose) {
|
|
164
|
+
setOpen(false);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
if (state.lastControlledValue !== valueProp) {
|
|
168
|
+
const isUpdateComingFromPicker = valueManager.areValuesEqual(utils, state.draft, valuePropWithTimezoneToRender);
|
|
169
|
+
setState(prev => _extends({}, prev, {
|
|
170
|
+
lastControlledValue: valueProp
|
|
171
|
+
}, isUpdateComingFromPicker ? {} : {
|
|
172
|
+
lastCommittedValue: valuePropWithTimezoneToRender,
|
|
173
|
+
lastPublishedValue: valuePropWithTimezoneToRender,
|
|
174
|
+
draft: valuePropWithTimezoneToRender,
|
|
175
|
+
hasBeenModifiedSinceMount: true
|
|
176
|
+
}));
|
|
177
|
+
}
|
|
178
|
+
const setValueFromView = useEventCallback((newValue, selectionState = 'partial') => {
|
|
179
|
+
// TODO: Expose a new method (private?) like `setView` that only updates the draft value.
|
|
180
|
+
if (selectionState === 'shallow') {
|
|
181
|
+
setState(prev => _extends({}, prev, {
|
|
182
|
+
draft: newValue,
|
|
183
|
+
hasBeenModifiedSinceMount: true
|
|
184
|
+
}));
|
|
185
|
+
}
|
|
186
|
+
setValue(newValue, {
|
|
187
|
+
changeImportance: selectionState === 'finish' && closeOnSelect ? 'accept' : 'set'
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// It is required to update inner state in useEffect in order to avoid situation when
|
|
192
|
+
// Our component is not mounted yet, but `open` state is set to `true` (for example initially opened)
|
|
193
|
+
React.useEffect(() => {
|
|
194
|
+
if (isOpenControlled) {
|
|
195
|
+
if (openProp === undefined) {
|
|
196
|
+
throw new Error('You must not mix controlling and uncontrolled mode for `open` prop');
|
|
197
|
+
}
|
|
198
|
+
setState(prevState => _extends({}, prevState, {
|
|
199
|
+
open: openProp
|
|
200
|
+
}));
|
|
201
|
+
}
|
|
202
|
+
}, [isOpenControlled, openProp]);
|
|
203
|
+
return {
|
|
204
|
+
timezone,
|
|
205
|
+
state,
|
|
206
|
+
setValue,
|
|
207
|
+
setValueFromView,
|
|
208
|
+
setOpen
|
|
209
|
+
};
|
|
210
|
+
}
|
|
@@ -1,4 +1,2 @@
|
|
|
1
1
|
export { usePicker } from "./usePicker.js";
|
|
2
|
-
export type { UsePickerProps, UsePickerBaseProps,
|
|
3
|
-
export type { PickerValueManager, PickerSelectionState, UsePickerValueFieldResponse } from './usePickerValue.types';
|
|
4
|
-
export type { PickerViewsRendererProps } from './usePickerViews';
|
|
2
|
+
export type { UsePickerProps, UsePickerBaseProps, UsePickerParameters, PickerSelectionState, PickerViewsRendererProps, PickerViewRendererLookup, PickerRendererInterceptorProps, PickerViewRenderer, UsePickerNonStaticProps } from './usePicker.types';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { UsePickerParameters, UsePickerProps, UsePickerReturnValue } from "./usePicker.types.js";
|
|
2
2
|
import { DateOrTimeViewWithMeridiem, PickerValidValue } from "../../models/index.js";
|
|
3
3
|
export declare const usePicker: <TValue extends PickerValidValue, TView extends DateOrTimeViewWithMeridiem, TExternalProps extends UsePickerProps<TValue, TView, any, any>>({
|
|
4
4
|
ref,
|
|
@@ -8,7 +8,7 @@ export declare const usePicker: <TValue extends PickerValidValue, TView extends
|
|
|
8
8
|
variant,
|
|
9
9
|
validator,
|
|
10
10
|
autoFocusView,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}:
|
|
11
|
+
rendererInterceptor: RendererInterceptor,
|
|
12
|
+
localeText,
|
|
13
|
+
viewContainerRole
|
|
14
|
+
}: UsePickerParameters<TValue, TView, TExternalProps>) => UsePickerReturnValue<TValue>;
|
|
@@ -1,7 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
|
|
3
|
+
const _excluded = ["className", "sx"];
|
|
4
|
+
import * as React from 'react';
|
|
5
|
+
import useEnhancedEffect from '@mui/utils/useEnhancedEffect';
|
|
6
|
+
import useEventCallback from '@mui/utils/useEventCallback';
|
|
7
|
+
import useForkRef from '@mui/utils/useForkRef';
|
|
8
|
+
import useId from '@mui/utils/useId';
|
|
9
|
+
import { useLocalizationContext, useUtils } from "../useUtils.js";
|
|
10
|
+
import { useReduceAnimations } from "../useReduceAnimations.js";
|
|
11
|
+
import { isTimeView } from "../../utils/time-utils.js";
|
|
12
|
+
import { useViews } from "../useViews.js";
|
|
13
|
+
import { useOrientation } from "./hooks/useOrientation.js";
|
|
14
|
+
import { useValueAndOpenStates } from "./hooks/useValueAndOpenStates.js";
|
|
15
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
5
16
|
export const usePicker = ({
|
|
6
17
|
ref,
|
|
7
18
|
props,
|
|
@@ -10,43 +21,274 @@ export const usePicker = ({
|
|
|
10
21
|
variant,
|
|
11
22
|
validator,
|
|
12
23
|
autoFocusView,
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
24
|
+
rendererInterceptor: RendererInterceptor,
|
|
25
|
+
localeText,
|
|
26
|
+
viewContainerRole
|
|
16
27
|
}) => {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
28
|
+
const {
|
|
29
|
+
// View props
|
|
30
|
+
views,
|
|
31
|
+
view: viewProp,
|
|
32
|
+
openTo,
|
|
33
|
+
onViewChange,
|
|
34
|
+
viewRenderers,
|
|
35
|
+
reduceAnimations: reduceAnimationsProp,
|
|
36
|
+
orientation: orientationProp,
|
|
37
|
+
disableOpenPicker,
|
|
38
|
+
// Form props
|
|
39
|
+
disabled,
|
|
40
|
+
readOnly,
|
|
41
|
+
// Field props
|
|
42
|
+
formatDensity,
|
|
43
|
+
enableAccessibleFieldDOMStructure,
|
|
44
|
+
selectedSections,
|
|
45
|
+
onSelectedSectionsChange,
|
|
46
|
+
format,
|
|
47
|
+
label,
|
|
48
|
+
// Other props
|
|
49
|
+
autoFocus,
|
|
50
|
+
name
|
|
51
|
+
} = props;
|
|
52
|
+
const {
|
|
53
|
+
className,
|
|
54
|
+
sx
|
|
55
|
+
} = props,
|
|
56
|
+
propsToForwardToView = _objectWithoutPropertiesLoose(props, _excluded);
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* TODO: Improve how we generate the aria-label and aria-labelledby attributes.
|
|
60
|
+
*/
|
|
61
|
+
const labelId = useId();
|
|
62
|
+
const utils = useUtils();
|
|
63
|
+
const adapter = useLocalizationContext();
|
|
64
|
+
const reduceAnimations = useReduceAnimations(reduceAnimationsProp);
|
|
65
|
+
const orientation = useOrientation(views, orientationProp);
|
|
66
|
+
const {
|
|
67
|
+
current: initialView
|
|
68
|
+
} = React.useRef(openTo ?? null);
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Refs
|
|
72
|
+
*/
|
|
73
|
+
const triggerRef = React.useRef(null);
|
|
74
|
+
const popupRef = React.useRef(null);
|
|
75
|
+
const fieldRef = React.useRef(null);
|
|
76
|
+
const rootRefObject = React.useRef(null);
|
|
77
|
+
const rootRef = useForkRef(ref, rootRefObject);
|
|
78
|
+
const {
|
|
79
|
+
timezone,
|
|
80
|
+
state,
|
|
81
|
+
setOpen,
|
|
82
|
+
setValue,
|
|
83
|
+
setValueFromView
|
|
84
|
+
} = useValueAndOpenStates({
|
|
23
85
|
props,
|
|
24
86
|
valueManager,
|
|
25
|
-
valueType,
|
|
26
87
|
validator
|
|
27
88
|
});
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
89
|
+
const {
|
|
90
|
+
view,
|
|
91
|
+
setView,
|
|
92
|
+
defaultView,
|
|
93
|
+
focusedView,
|
|
94
|
+
setFocusedView,
|
|
95
|
+
setValueAndGoToNextView
|
|
96
|
+
} = useViews({
|
|
97
|
+
view: viewProp,
|
|
98
|
+
views,
|
|
99
|
+
openTo,
|
|
100
|
+
onChange: setValueFromView,
|
|
101
|
+
onViewChange,
|
|
102
|
+
autoFocus: autoFocusView
|
|
34
103
|
});
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
104
|
+
const clearValue = useEventCallback(() => setValue(valueManager.emptyValue));
|
|
105
|
+
const setValueToToday = useEventCallback(() => setValue(valueManager.getTodayValue(utils, timezone, valueType)));
|
|
106
|
+
const acceptValueChanges = useEventCallback(() => setValue(state.lastPublishedValue));
|
|
107
|
+
const cancelValueChanges = useEventCallback(() => setValue(state.lastCommittedValue, {
|
|
108
|
+
skipPublicationIfPristine: true
|
|
109
|
+
}));
|
|
110
|
+
const dismissViews = useEventCallback(() => {
|
|
111
|
+
setValue(state.lastPublishedValue, {
|
|
112
|
+
skipPublicationIfPristine: true
|
|
113
|
+
});
|
|
43
114
|
});
|
|
115
|
+
const valueWithoutError = React.useMemo(() => valueManager.cleanValue(utils, state.draft), [utils, valueManager, state.draft]);
|
|
116
|
+
const {
|
|
117
|
+
hasUIView,
|
|
118
|
+
viewModeLookup,
|
|
119
|
+
timeViewsCount
|
|
120
|
+
} = React.useMemo(() => views.reduce((acc, viewForReduce) => {
|
|
121
|
+
const viewMode = viewRenderers[viewForReduce] == null ? 'field' : 'UI';
|
|
122
|
+
acc.viewModeLookup[viewForReduce] = viewMode;
|
|
123
|
+
if (viewMode === 'UI') {
|
|
124
|
+
acc.hasUIView = true;
|
|
125
|
+
if (isTimeView(viewForReduce)) {
|
|
126
|
+
acc.timeViewsCount += 1;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return acc;
|
|
130
|
+
}, {
|
|
131
|
+
hasUIView: false,
|
|
132
|
+
viewModeLookup: {},
|
|
133
|
+
timeViewsCount: 0
|
|
134
|
+
}), [viewRenderers, views]);
|
|
135
|
+
const currentViewMode = viewModeLookup[view];
|
|
136
|
+
const getCurrentViewMode = useEventCallback(() => currentViewMode);
|
|
137
|
+
const [popperView, setPopperView] = React.useState(currentViewMode === 'UI' ? view : null);
|
|
138
|
+
if (popperView !== view && viewModeLookup[view] === 'UI') {
|
|
139
|
+
setPopperView(view);
|
|
140
|
+
}
|
|
141
|
+
useEnhancedEffect(() => {
|
|
142
|
+
// Handle case of Date Time Picker without time renderers
|
|
143
|
+
if (currentViewMode === 'field' && state.open) {
|
|
144
|
+
setOpen(false);
|
|
145
|
+
setTimeout(() => {
|
|
146
|
+
fieldRef?.current?.setSelectedSections(view);
|
|
147
|
+
// focusing the input before the range selection is done
|
|
148
|
+
// calling it outside of timeout results in an inconsistent behavior between Safari And Chrome
|
|
149
|
+
fieldRef?.current?.focusField(view);
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}, [view]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
153
|
+
|
|
154
|
+
useEnhancedEffect(() => {
|
|
155
|
+
if (!state.open) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
let newView = view;
|
|
159
|
+
|
|
160
|
+
// If the current view is a field view, go to the last popper view
|
|
161
|
+
if (currentViewMode === 'field' && popperView != null) {
|
|
162
|
+
newView = popperView;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// If the current view is not the default view and both are UI views
|
|
166
|
+
if (newView !== defaultView && viewModeLookup[newView] === 'UI' && viewModeLookup[defaultView] === 'UI') {
|
|
167
|
+
newView = defaultView;
|
|
168
|
+
}
|
|
169
|
+
if (newView !== view) {
|
|
170
|
+
setView(newView);
|
|
171
|
+
}
|
|
172
|
+
setFocusedView(newView, true);
|
|
173
|
+
}, [state.open]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
174
|
+
|
|
175
|
+
const ownerState = React.useMemo(() => ({
|
|
176
|
+
isPickerValueEmpty: valueManager.areValuesEqual(utils, state.draft, valueManager.emptyValue),
|
|
177
|
+
isPickerOpen: state.open,
|
|
178
|
+
isPickerDisabled: props.disabled ?? false,
|
|
179
|
+
isPickerReadOnly: props.readOnly ?? false,
|
|
180
|
+
pickerOrientation: orientation,
|
|
181
|
+
pickerVariant: variant
|
|
182
|
+
}), [utils, valueManager, state.draft, state.open, orientation, variant, props.disabled, props.readOnly]);
|
|
183
|
+
const triggerStatus = React.useMemo(() => {
|
|
184
|
+
if (disableOpenPicker || !hasUIView) {
|
|
185
|
+
return 'hidden';
|
|
186
|
+
}
|
|
187
|
+
if (disabled || readOnly) {
|
|
188
|
+
return 'disabled';
|
|
189
|
+
}
|
|
190
|
+
return 'enabled';
|
|
191
|
+
}, [disableOpenPicker, hasUIView, disabled, readOnly]);
|
|
192
|
+
const actionsContextValue = React.useMemo(() => ({
|
|
193
|
+
setValue,
|
|
194
|
+
setOpen,
|
|
195
|
+
clearValue,
|
|
196
|
+
setValueToToday,
|
|
197
|
+
acceptValueChanges,
|
|
198
|
+
cancelValueChanges,
|
|
199
|
+
setView
|
|
200
|
+
}), [setValue, setOpen, clearValue, setValueToToday, acceptValueChanges, cancelValueChanges, setView]);
|
|
201
|
+
const contextValue = React.useMemo(() => _extends({}, actionsContextValue, {
|
|
202
|
+
value: state.draft,
|
|
203
|
+
timezone,
|
|
204
|
+
open: state.open,
|
|
205
|
+
views,
|
|
206
|
+
view: popperView,
|
|
207
|
+
initialView,
|
|
208
|
+
disabled: disabled ?? false,
|
|
209
|
+
readOnly: readOnly ?? false,
|
|
210
|
+
autoFocus: autoFocus ?? false,
|
|
211
|
+
variant,
|
|
212
|
+
orientation,
|
|
213
|
+
popupRef,
|
|
214
|
+
reduceAnimations,
|
|
215
|
+
triggerRef,
|
|
216
|
+
triggerStatus,
|
|
217
|
+
fieldFormat: format ?? '',
|
|
218
|
+
name,
|
|
219
|
+
label,
|
|
220
|
+
rootSx: sx,
|
|
221
|
+
rootRef,
|
|
222
|
+
rootClassName: className
|
|
223
|
+
}), [actionsContextValue, state.draft, rootRef, variant, orientation, reduceAnimations, disabled, readOnly, format, className, name, label, sx, triggerRef, triggerStatus, timezone, state.open, popperView, views, initialView, autoFocus]);
|
|
224
|
+
const privateContextValue = React.useMemo(() => ({
|
|
225
|
+
dismissViews,
|
|
226
|
+
hasUIView,
|
|
227
|
+
getCurrentViewMode,
|
|
228
|
+
ownerState,
|
|
229
|
+
rootRefObject,
|
|
230
|
+
labelId,
|
|
231
|
+
viewContainerRole
|
|
232
|
+
}), [dismissViews, hasUIView, getCurrentViewMode, ownerState, rootRefObject, labelId, viewContainerRole]);
|
|
233
|
+
const fieldPrivateContextValue = React.useMemo(() => ({
|
|
234
|
+
formatDensity,
|
|
235
|
+
enableAccessibleFieldDOMStructure,
|
|
236
|
+
selectedSections,
|
|
237
|
+
onSelectedSectionsChange,
|
|
238
|
+
fieldRef
|
|
239
|
+
}), [formatDensity, enableAccessibleFieldDOMStructure, selectedSections, onSelectedSectionsChange, fieldRef]);
|
|
240
|
+
const isValidContextValue = testedValue => {
|
|
241
|
+
const error = validator({
|
|
242
|
+
adapter,
|
|
243
|
+
value: testedValue,
|
|
244
|
+
timezone,
|
|
245
|
+
props
|
|
246
|
+
});
|
|
247
|
+
return !valueManager.hasError(error);
|
|
248
|
+
};
|
|
249
|
+
const renderCurrentView = () => {
|
|
250
|
+
if (popperView == null) {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
const renderer = viewRenderers[popperView];
|
|
254
|
+
if (renderer == null) {
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
const rendererProps = _extends({}, propsToForwardToView, {
|
|
258
|
+
views,
|
|
259
|
+
timezone,
|
|
260
|
+
value: valueWithoutError,
|
|
261
|
+
onChange: setValueAndGoToNextView,
|
|
262
|
+
view: popperView,
|
|
263
|
+
onViewChange: setView,
|
|
264
|
+
showViewSwitcher: timeViewsCount > 1,
|
|
265
|
+
timeViewsCount
|
|
266
|
+
}, viewContainerRole === 'tooltip' ? {
|
|
267
|
+
focusedView: null,
|
|
268
|
+
onFocusedViewChange: () => {}
|
|
269
|
+
} : {
|
|
270
|
+
focusedView,
|
|
271
|
+
onFocusedViewChange: setFocusedView
|
|
272
|
+
});
|
|
273
|
+
if (RendererInterceptor) {
|
|
274
|
+
return /*#__PURE__*/_jsx(RendererInterceptor, {
|
|
275
|
+
viewRenderers: viewRenderers,
|
|
276
|
+
popperView: popperView,
|
|
277
|
+
rendererProps: rendererProps
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
return renderer(rendererProps);
|
|
281
|
+
};
|
|
44
282
|
return {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
283
|
+
providerProps: {
|
|
284
|
+
localeText,
|
|
285
|
+
contextValue,
|
|
286
|
+
privateContextValue,
|
|
287
|
+
actionsContextValue,
|
|
288
|
+
fieldPrivateContextValue,
|
|
289
|
+
isValidContextValue
|
|
290
|
+
},
|
|
291
|
+
renderCurrentView,
|
|
292
|
+
ownerState
|
|
51
293
|
};
|
|
52
294
|
};
|