Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pickers] Add optional id attribute on shortcut items #12976

Merged
merged 3 commits into from
May 6, 2024

Conversation

noraleonte
Copy link
Contributor

closes #12968

@noraleonte noraleonte added component: pickers This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature labels May 2, 2024
@noraleonte noraleonte self-assigned this May 2, 2024
@mui-bot
Copy link

mui-bot commented May 2, 2024

Deploy preview: https://deploy-preview-12976--material-ui-x.netlify.app/

Generated by 🚫 dangerJS against b89a0b3

Copy link
Member

@michelengelen michelengelen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, a rushed request for changes. Will leave full feedback soon. 😉
Short version: it doesn't work and creates react errors

@michelengelen
Copy link
Member

Sorry, a rushed request for changes. Will leave full feedback soon. 😉 Short version: it doesn't work and creates react errors

Why is that? I tried it last time and just now again and it did not throw any errors for me.

Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about the following proposal:

diff --git a/packages/x-date-pickers/src/PickersShortcuts/PickersShortcuts.tsx b/packages/x-date-pickers/src/PickersShortcuts/PickersShortcuts.tsx
index 8e36ecf55..09baa499e 100644
--- a/packages/x-date-pickers/src/PickersShortcuts/PickersShortcuts.tsx
+++ b/packages/x-date-pickers/src/PickersShortcuts/PickersShortcuts.tsx
@@ -12,7 +12,11 @@ interface PickersShortcutsItemGetValueParams<TValue> {
 export interface PickersShortcutsItem<TValue> {
   label: string;
   getValue: (params: PickersShortcutsItemGetValueParams<TValue>) => TValue;
-  key?: string;
+  /**
+   * 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'>;
@@ -65,7 +69,7 @@ function PickersShortcuts<TValue>(props: PickersShortcutsProps<TValue>) {
     const newValue = getValue({ isValid });
 
     return {
-      key: item?.key || item.label,
+      ...item,
       label: item.label,
       onClick: () => {
         onChange(newValue, changeImportance, item);
@@ -89,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>
         );

I'm not a fan of exposing a prop that is directly targetted towards just being a React key—an implementation detail.
If we call the field id, then it can be used by users to identify the shortcut item that has been clicked and act accordingly.
Currently, they only receive label, which is a fragile piece of information to take for granted as unique. 🙈
Like, even if it's not currently typed, users could pass color: 'primary' and make such shortcut chip seem selected.

@LukasTy
Copy link
Member

LukasTy commented May 3, 2024

Why is that? I tried it last time and just now again and it did not throw any errors for me.

It doesn't do anything, because the key on Listitem is still left as is.
The error is thrown on CodeSandbox, locally I didn't see it as well. 🙈
Screenshot 2024-05-03 at 12 53 29

@noraleonte
Copy link
Contributor Author

@LukasTy What you are proposing is great, I think the main pain point here was that users couldn't give unique identifiers. id should solve that 👌

Like, even if it's not currently typed, users could pass color: 'primary' and make such shortcut chip seem selected.

Slightly unrelated, but circling back to this, we could maybe type it 🤷‍♀️ Technically, a shortcut item should accept all the props that a chip does

Copy link
Member

@LukasTy LukasTy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The solution look great! 👏

Slightly unrelated, but circling back to this, we could maybe type it 🤷‍♀️ Technically, a shortcut item should accept all the props that a chip does

Yes, that's a great observation.
By doing that we could potentially provide a solution for #10120 as a recipe because I'm not sure if we should be providing that behavior OOB as it is probably impossible without additional performance impact that everybody might not desire. 🙈

@LukasTy LukasTy changed the title [pickers] Add key on shortcut items [pickers] Add id on shortcut items May 3, 2024
@LukasTy LukasTy changed the title [pickers] Add id on shortcut items [pickers] Add optional id attribute on shortcut items May 3, 2024
@noraleonte noraleonte merged commit 4b062fb into mui:master May 6, 2024
17 checks passed
DungTiger pushed a commit to DungTiger/mui-x that referenced this pull request Jul 23, 2024
thomasmoon pushed a commit to thomasmoon/mui-x that referenced this pull request Sep 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: pickers This is the name of the generic UI component, not the React module! enhancement This is not a bug, nor a new feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[pickers][DateRangePicker] shortcuts should allow specifying React key
4 participants