Skip to content

Commit

Permalink
[pickers] Add optional id attribute on shortcut items (mui#12976)
Browse files Browse the repository at this point in the history
  • Loading branch information
noraleonte authored and DungTiger committed Jul 23, 2024
1 parent fe9b683 commit 1359339
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/pages/x/api/date-pickers/pickers-shortcuts.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"items": {
"type": {
"name": "arrayOf",
"description": "Array<{ getValue: func, label: string }>"
"description": "Array<{ getValue: func, id?: string, label: string }>"
},
"default": "[]"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ interface PickersShortcutsItemGetValueParams<TValue> {
export interface PickersShortcutsItem<TValue> {
label: string;
getValue: (params: PickersShortcutsItemGetValueParams<TValue>) => TValue;
/**
* Identifier of the shortcut.
* If provided, it will be used as the key of the shortcut.
*/
id?: string;
}

export type PickersShortcutsItemContext = Omit<PickersShortcutsItem<unknown>, 'getValue'>;
Expand Down Expand Up @@ -64,6 +69,7 @@ function PickersShortcuts<TValue>(props: PickersShortcutsProps<TValue>) {
const newValue = getValue({ isValid });

return {
...item,
label: item.label,
onClick: () => {
onChange(newValue, changeImportance, item);
Expand All @@ -87,7 +93,7 @@ function PickersShortcuts<TValue>(props: PickersShortcutsProps<TValue>) {
>
{resolvedItems.map((item) => {
return (
<ListItem key={item.label}>
<ListItem key={item.id ?? item.label}>
<Chip {...item} />
</ListItem>
);
Expand Down Expand Up @@ -132,6 +138,7 @@ PickersShortcuts.propTypes = {
items: PropTypes.arrayOf(
PropTypes.shape({
getValue: PropTypes.func.isRequired,
id: PropTypes.string,
label: PropTypes.string.isRequired,
}),
),
Expand Down

0 comments on commit 1359339

Please sign in to comment.