@@ -2,7 +2,7 @@ import * as React from 'react';
2
2
import { SxProps } from '@mui/system' ;
3
3
import { Theme } from '@mui/material/styles' ;
4
4
5
- import { PickerOwnerState } from '../../models' ;
5
+ import { PickerChangeImportance , PickerOwnerState , PickersTimezone } from '../../models' ;
6
6
import { PickersInputLocaleText } from '../../locales' ;
7
7
import { LocalizationProvider } from '../../LocalizationProvider' ;
8
8
import {
@@ -11,22 +11,13 @@ import {
11
11
PickerValidValue ,
12
12
PickerVariant ,
13
13
} from '../models' ;
14
- import type {
15
- UsePickerValueActionsContextValue ,
16
- UsePickerValueContextValue ,
17
- UsePickerValuePrivateContextValue ,
18
- } from '../hooks/usePicker/usePickerValue.types' ;
19
- import {
20
- UsePickerViewsActionsContextValue ,
21
- UsePickerViewsContextValue ,
22
- UsePickerViewsPrivateContextValue ,
23
- } from '../hooks/usePicker/usePickerViews' ;
24
14
import { IsValidValueContext } from '../../hooks/useIsValidValue' ;
25
15
import {
26
16
PickerFieldPrivateContext ,
27
17
PickerFieldPrivateContextValue ,
28
18
} from '../hooks/useNullableFieldPrivateContext' ;
29
19
import { PickerContext } from '../../hooks/usePickerContext' ;
20
+ import type { PickersShortcutsItemContext } from '../../PickersShortcuts' ;
30
21
31
22
export const PickerActionsContext = React . createContext < PickerActionsContextValue <
32
23
any ,
@@ -45,7 +36,7 @@ export const PickerPrivateContext = React.createContext<PickerPrivateContextValu
45
36
} ,
46
37
dismissViews : ( ) => { } ,
47
38
hasUIView : true ,
48
- doesTheCurrentViewHasAnUI : ( ) => true ,
39
+ getCurrentViewMode : ( ) => 'UI' ,
49
40
rootRefObject : { current : null } ,
50
41
viewContainerRole : null ,
51
42
labelId : undefined ,
@@ -100,8 +91,24 @@ export interface PickerContextValue<
100
91
TValue extends PickerValidValue ,
101
92
TView extends DateOrTimeViewWithMeridiem ,
102
93
TError ,
103
- > extends UsePickerValueContextValue < TValue , TError > ,
104
- UsePickerViewsContextValue < TView > {
94
+ > extends PickerActionsContextValue < TValue , TView , TError > {
95
+ /**
96
+ * The current value of the picker.
97
+ */
98
+ value : TValue ;
99
+ /**
100
+ * The timezone to use when rendering the dates.
101
+ * If a `timezone` prop is provided, it will be used.
102
+ * If the `value` prop contains a valid date, its timezone will be used.
103
+ * If no `value` prop is provided, but the `defaultValue` contains a valid date, its timezone will be used.
104
+ * If no `value` or `defaultValue` is provided, but the `referenceDate` is provided, its timezone will be used.
105
+ * Otherwise, the timezone will be the default one of your date library.
106
+ */
107
+ timezone : PickersTimezone ;
108
+ /**
109
+ * Whether the picker is open.
110
+ */
111
+ open : boolean ;
105
112
/**
106
113
* Whether the picker is disabled.
107
114
*/
@@ -116,6 +123,25 @@ export interface PickerContextValue<
116
123
* If the picker does not have a field (if it is a static picker) or is not open, the view should be focused.
117
124
*/
118
125
autoFocus : boolean ;
126
+ /**
127
+ * The views that the picker has to render.
128
+ * It is equal to the picker `views` prop—if defined.
129
+ * Otherwise, a default set of views is provided based on the component you are using:
130
+ * - Date Pickers: ['year', 'day']
131
+ * - Time Pickers: ['hours', 'minutes']
132
+ * - Date Time Pickers: ['year', 'day', 'hours', 'minutes']
133
+ * - Date Range Pickers: ['day']
134
+ * - Date Time Range Pickers: ['day', 'hours', 'minutes']
135
+ */
136
+ views : readonly TView [ ] ;
137
+ /**
138
+ * The currently rendered view.
139
+ */
140
+ view : TView | null ;
141
+ /**
142
+ * The view showed when first opening the picker.
143
+ */
144
+ initialView : TView | null ;
119
145
/**
120
146
* The responsive variant of the picker.
121
147
* It is equal to "desktop" when using a desktop picker (like <DesktopDatePicker />).
@@ -202,12 +228,87 @@ export interface PickerActionsContextValue<
202
228
TValue extends PickerValidValue ,
203
229
TView extends DateOrTimeViewWithMeridiem ,
204
230
TError = string | null ,
205
- > extends UsePickerValueActionsContextValue < TValue , TError > ,
206
- UsePickerViewsActionsContextValue < TView > { }
231
+ > {
232
+ /**
233
+ * Set the current value of the picker.
234
+ * @param {TValue } value The new value of the picker.
235
+ * @param {SetValueActionOptions<TError> } options The options to customize the behavior of this update.
236
+ */
237
+ setValue : ( value : TValue , options ?: SetValueActionOptions < TError > ) => void ;
238
+ /**
239
+ * Set the current open state of the Picker.
240
+ * It can be a function that will receive the current open state as parameter.
241
+ * ```ts
242
+ * setOpen(true); // Opens the picker.
243
+ * setOpen(false); // Closes the picker.
244
+ * setOpen((prevOpen) => !prevOpen); // Toggles the open state.
245
+ * ```
246
+ * @param {React.SetStateAction<boolean> } action The new open state of the Picker.
247
+ */
248
+ setOpen : React . Dispatch < React . SetStateAction < boolean > > ;
249
+ /**
250
+ * Set the current view.
251
+ * @template TView
252
+ * @param {TView } view The view to render
253
+ */
254
+ setView : ( view : TView ) => void ;
255
+ /**
256
+ * Set the current value of the picker to be empty.
257
+ * The value will be `null` on single pickers and `[null, null]` on range pickers.
258
+ */
259
+ clearValue : ( ) => void ;
260
+ /**
261
+ * Set the current value of the picker to be the current date.
262
+ * The value will be `today` on single pickers and `[today, today]` on range pickers.
263
+ * With `today` being the current date, with its time set to `00:00:00` on Date Pickers and its time set to the current time on Time and Date Pickers.
264
+ */
265
+ setValueToToday : ( ) => void ;
266
+ /**
267
+ * Accept the current value of the picker.
268
+ * Will call `onAccept` if defined.
269
+ * If the picker is re-opened, this value will be the one used to initialize the views.
270
+ */
271
+ acceptValueChanges : ( ) => void ;
272
+ /**
273
+ * Cancel the changes made to the current value of the picker.
274
+ * The value will be reset to the last accepted value.
275
+ */
276
+ cancelValueChanges : ( ) => void ;
277
+ }
207
278
208
- export interface PickerPrivateContextValue
209
- extends UsePickerValuePrivateContextValue ,
210
- UsePickerViewsPrivateContextValue {
279
+ export interface SetValueActionOptions < TError = string | null > {
280
+ /**
281
+ * The importance of the change when picking a value:
282
+ * - "accept": fires `onChange`, fires `onAccept` and closes the picker.
283
+ * - "set": fires `onChange` but do not fire `onAccept` and does not close the picker.
284
+ * @default "accept"
285
+ */
286
+ changeImportance ?: PickerChangeImportance ;
287
+ /**
288
+ * The validation error associated to the current value.
289
+ * If not defined, the validation will be computed by the picker.
290
+ */
291
+ validationError ?: TError ;
292
+ /**
293
+ * The shortcut that triggered this change.
294
+ * It should not be defined if the change does not come from a shortcut.
295
+ */
296
+ shortcut ?: PickersShortcutsItemContext ;
297
+ /**
298
+ * Whether the value should call `onChange` and `onAccept` when the value is not controlled and has never been modified.
299
+ * If `true`, the `onChange` and `onAccept` callback will only be fired if the value has been modified (and is not equal to the last published value).
300
+ * If `false`, the `onChange` and `onAccept` callback will be fired when the value has never been modified (`onAccept` only if `changeImportance` is set to "accept").
301
+ * @default false
302
+ */
303
+ skipPublicationIfPristine ?: boolean ;
304
+ /**
305
+ * Whether the picker should close.
306
+ * @default changeImportance === "accept"
307
+ */
308
+ shouldClose ?: boolean ;
309
+ }
310
+
311
+ export interface PickerPrivateContextValue {
211
312
/**
212
313
* The ownerState of the picker.
213
314
*/
@@ -221,4 +322,26 @@ export interface PickerPrivateContextValue
221
322
* The id of the label element.
222
323
*/
223
324
labelId : string | undefined ;
325
+ /*
326
+ * Close the picker and accepts the current value if it is not equal to the last accepted value.
327
+ */
328
+ dismissViews : ( ) => void ;
329
+ /**
330
+ * Whether at least one view has an UI (it has a view renderer associated).
331
+ */
332
+ hasUIView : boolean ;
333
+ /**
334
+ * Return the mode of the current view.
335
+ * @returns {boolean } The mode of the current view.
336
+ */
337
+ getCurrentViewMode : ( ) => 'UI' | 'field' ;
338
+ /**
339
+ * The aria role associated with the view container.
340
+ * It is equal to "dialog" when the view is rendered inside a `@mui/material/Dialog`.
341
+ * It is equal to "dialog" when the view is rendered inside a `@mui/material/Popper` and the focus is trapped inside the view.
342
+ * It is equal to "tooltip" when the view is rendered inside a `@mui/material/Popper` and the focus remains inside the field.
343
+ * It is always equal to null if the picker does not have a field (static pickers).
344
+ * It is always equal to null if the component you are accessing the context from is not wrapped by a picker.
345
+ */
346
+ viewContainerRole : 'dialog' | 'tooltip' | null ;
224
347
}
0 commit comments