Skip to content

Commit 840fad5

Browse files
[pickers] Adds new source property to onChange and onAccept context object (#20234)
Signed-off-by: michel <jsnerdic@gmail.com>
1 parent 19f0452 commit 840fad5

File tree

53 files changed

+339
-100
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+339
-100
lines changed

docs/data/date-pickers/lifecycle/lifecycle.md

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,16 @@ The `onAccept` callback lets you get the final value selected by the user withou
206206
<DatePicker onAccept={(value) => sendValueToServer(value)} />
207207
```
208208

209-
:::success
210-
You can use the second argument passed to the `onAccept` callback to get the validation error associated with the current value:
209+
The `onAccept` callback receives a second argument `context` with extra information about why and how the value was accepted.
210+
211+
- `validationError`: the validation result of the accepted value.
212+
- `source`: string that indicates where a change or acceptance originated from. The value is one of:
213+
- `'field'`: committed from the input field (typing, paste, arrow keys, clear, Enter, etc.).
214+
- `'view'`: any interaction inside the picker's view
215+
- `'unknown'`: unspecified or third‑party triggers.
216+
- `shortcut` (optional): the shortcut metadata when the value was accepted via a shortcut selection.
217+
218+
For custom error handling, you can use the `validationError` property of the `context` object.
211219

212220
```tsx
213221
<DatePicker
@@ -219,7 +227,32 @@ You can use the second argument passed to the `onAccept` callback to get the val
219227
/>
220228
```
221229

222-
:::
230+
The `source` property allows you to implement different behaviors depending on the source of the acceptance.
231+
232+
```tsx
233+
<DatePicker
234+
onAccept={(newValue, context) => {
235+
if (context.validationError != null) {
236+
return; // ignore invalid values
237+
}
238+
239+
switch (context.source) {
240+
case 'view':
241+
analytics.track('date_accepted_from_view', {
242+
value: newValue,
243+
shortcut: context.shortcut?.id,
244+
});
245+
break;
246+
case 'field':
247+
analytics.track('date_accepted_from_field', { value: newValue });
248+
break;
249+
case 'unknown':
250+
default:
251+
analytics.track('date_accepted_from_unknown', { value: newValue });
252+
}
253+
}}
254+
/>
255+
```
223256

224257
### When is "onAccept" called?
225258

@@ -399,3 +432,37 @@ The following demo shows how to extend the Date Field component by adding an `on
399432
You can find more information about the `onAccept` prop [in the dedicated doc section](/x/react-date-pickers/lifecycle/#lifecycle-on-pickers-quot-onaccept-quot).
400433

401434
{{"demo": "ServerInteraction.js"}}
435+
436+
### Source values in `context.source`
437+
438+
Pickers expose a simplified `context.source` string that indicates where a change or acceptance originated from.
439+
The value is one of:
440+
441+
- `'field'`: committed from the input field (typing, paste, arrow keys, clear, Enter, etc.).
442+
- `'view'`: any interaction inside the picker's view
443+
- `'unknown'`: unspecified or third‑party triggers.
444+
445+
Example usage:
446+
447+
```tsx
448+
<DatePicker
449+
onAccept={(value, context) => {
450+
if (context.validationError) return;
451+
452+
switch (context.source) {
453+
case 'picker':
454+
analytics.track('date_accept_from_picker', {
455+
value,
456+
shortcut: context.shortcut?.id,
457+
});
458+
break;
459+
case 'field':
460+
analytics.track('date_accept_from_field', { value });
461+
break;
462+
case 'unknown':
463+
default:
464+
analytics.track('date_accept_from_unknown', { value });
465+
}
466+
}}
467+
/>
468+
```

docs/translations/api-docs/date-pickers/date-picker/date-picker.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
},
7676
"context": {
7777
"name": "context",
78-
"description": "The context containing the validation result of the current value."
78+
"description": "<p>Context about this acceptance: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the acceptance. One of &#39;field&#39; | &#39;picker&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the value was accepted via a shortcut selection</p>\n"
7979
}
8080
}
8181
},
@@ -90,7 +90,7 @@
9090
},
9191
"context": {
9292
"name": "context",
93-
"description": "The context containing the validation result of the current value."
93+
"description": "<p>Context about this change: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the change. One of &#39;field&#39; | &#39;view&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the change was triggered by a shortcut selection</p>\n"
9494
}
9595
}
9696
},

docs/translations/api-docs/date-pickers/date-range-picker/date-range-picker.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
},
8888
"context": {
8989
"name": "context",
90-
"description": "The context containing the validation result of the current value."
90+
"description": "<p>Context about this acceptance: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the acceptance. One of &#39;field&#39; | &#39;picker&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the value was accepted via a shortcut selection</p>\n"
9191
}
9292
}
9393
},
@@ -102,7 +102,7 @@
102102
},
103103
"context": {
104104
"name": "context",
105-
"description": "The context containing the validation result of the current value."
105+
"description": "<p>Context about this change: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the change. One of &#39;field&#39; | &#39;view&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the change was triggered by a shortcut selection</p>\n"
106106
}
107107
}
108108
},

docs/translations/api-docs/date-pickers/date-time-picker/date-time-picker.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
},
9696
"context": {
9797
"name": "context",
98-
"description": "The context containing the validation result of the current value."
98+
"description": "<p>Context about this acceptance: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the acceptance. One of &#39;field&#39; | &#39;picker&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the value was accepted via a shortcut selection</p>\n"
9999
}
100100
}
101101
},
@@ -110,7 +110,7 @@
110110
},
111111
"context": {
112112
"name": "context",
113-
"description": "The context containing the validation result of the current value."
113+
"description": "<p>Context about this change: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the change. One of &#39;field&#39; | &#39;view&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the change was triggered by a shortcut selection</p>\n"
114114
}
115115
}
116116
},

docs/translations/api-docs/date-pickers/date-time-range-picker/date-time-range-picker.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
},
105105
"context": {
106106
"name": "context",
107-
"description": "The context containing the validation result of the current value."
107+
"description": "<p>Context about this acceptance: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the acceptance. One of &#39;field&#39; | &#39;picker&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the value was accepted via a shortcut selection</p>\n"
108108
}
109109
}
110110
},
@@ -119,7 +119,7 @@
119119
},
120120
"context": {
121121
"name": "context",
122-
"description": "The context containing the validation result of the current value."
122+
"description": "<p>Context about this change: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the change. One of &#39;field&#39; | &#39;view&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the change was triggered by a shortcut selection</p>\n"
123123
}
124124
}
125125
},

docs/translations/api-docs/date-pickers/desktop-date-picker/desktop-date-picker.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
},
7373
"context": {
7474
"name": "context",
75-
"description": "The context containing the validation result of the current value."
75+
"description": "<p>Context about this acceptance: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the acceptance. One of &#39;field&#39; | &#39;picker&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the value was accepted via a shortcut selection</p>\n"
7676
}
7777
}
7878
},
@@ -87,7 +87,7 @@
8787
},
8888
"context": {
8989
"name": "context",
90-
"description": "The context containing the validation result of the current value."
90+
"description": "<p>Context about this change: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the change. One of &#39;field&#39; | &#39;view&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the change was triggered by a shortcut selection</p>\n"
9191
}
9292
}
9393
},

docs/translations/api-docs/date-pickers/desktop-date-range-picker/desktop-date-range-picker.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
},
8585
"context": {
8686
"name": "context",
87-
"description": "The context containing the validation result of the current value."
87+
"description": "<p>Context about this acceptance: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the acceptance. One of &#39;field&#39; | &#39;picker&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the value was accepted via a shortcut selection</p>\n"
8888
}
8989
}
9090
},
@@ -99,7 +99,7 @@
9999
},
100100
"context": {
101101
"name": "context",
102-
"description": "The context containing the validation result of the current value."
102+
"description": "<p>Context about this change: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the change. One of &#39;field&#39; | &#39;view&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the change was triggered by a shortcut selection</p>\n"
103103
}
104104
}
105105
},

docs/translations/api-docs/date-pickers/desktop-date-time-picker/desktop-date-time-picker.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
},
9393
"context": {
9494
"name": "context",
95-
"description": "The context containing the validation result of the current value."
95+
"description": "<p>Context about this acceptance: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the acceptance. One of &#39;field&#39; | &#39;picker&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the value was accepted via a shortcut selection</p>\n"
9696
}
9797
}
9898
},
@@ -107,7 +107,7 @@
107107
},
108108
"context": {
109109
"name": "context",
110-
"description": "The context containing the validation result of the current value."
110+
"description": "<p>Context about this change: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the change. One of &#39;field&#39; | &#39;view&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the change was triggered by a shortcut selection</p>\n"
111111
}
112112
}
113113
},

docs/translations/api-docs/date-pickers/desktop-date-time-range-picker/desktop-date-time-range-picker.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
},
102102
"context": {
103103
"name": "context",
104-
"description": "The context containing the validation result of the current value."
104+
"description": "<p>Context about this acceptance: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the acceptance. One of &#39;field&#39; | &#39;picker&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the value was accepted via a shortcut selection</p>\n"
105105
}
106106
}
107107
},
@@ -116,7 +116,7 @@
116116
},
117117
"context": {
118118
"name": "context",
119-
"description": "The context containing the validation result of the current value."
119+
"description": "<p>Context about this change: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the change. One of &#39;field&#39; | &#39;view&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the change was triggered by a shortcut selection</p>\n"
120120
}
121121
}
122122
},

docs/translations/api-docs/date-pickers/desktop-time-picker/desktop-time-picker.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
},
6262
"context": {
6363
"name": "context",
64-
"description": "The context containing the validation result of the current value."
64+
"description": "<p>Context about this acceptance: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the acceptance. One of &#39;field&#39; | &#39;picker&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the value was accepted via a shortcut selection</p>\n"
6565
}
6666
}
6767
},
@@ -76,7 +76,7 @@
7676
},
7777
"context": {
7878
"name": "context",
79-
"description": "The context containing the validation result of the current value."
79+
"description": "<p>Context about this change: - <code>validationError</code>: validation result of the current value - <code>source</code>: source of the change. One of &#39;field&#39; | &#39;view&#39; | &#39;unknown&#39; - <code>shortcut</code> (optional): the shortcut metadata if the change was triggered by a shortcut selection</p>\n"
8080
}
8181
}
8282
},

0 commit comments

Comments
 (0)