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

Fix hardcoded TextField size prop #9554

Merged
merged 6 commits into from
Jan 23, 2024
Merged

Conversation

PedroPerpetua
Copy link
Contributor

@PedroPerpetua PedroPerpetua commented Jan 3, 2024

This fixes the issue the issue with the TextField size prop not being override-able by theme. RA's default theme actually already sets the MuiTextField size prop default value as "small", so it's redundant to have it here.

However, in a possibly unrelated note, it may be benefitial to extend RA's documentation and themeing options with a re-export of MUI's createTheme function, that allows the deep-merging of themes, or maybe recommend lodash.merge to set up themes.

With this PR, overriding the theme's TextField variant as per RA's docs will result in the size="small" property to be lost.

Closes #9546

Copy link
Collaborator

@djhi djhi left a comment

Choose a reason for hiding this comment

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

Thanks!

@adguernier adguernier self-requested a review January 4, 2024 09:16
Copy link
Contributor

@adguernier adguernier left a comment

Choose a reason for hiding this comment

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

I agree too since RA's defaultTheme sets the MuiTextField defaultProps to "small

Copy link
Member

@fzaninotto fzaninotto left a comment

Choose a reason for hiding this comment

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

This is the right code, thanks! However we need to document this change in the upgrade guide. People with a custom theme will see their inputs size change from small to medium, and this is a regression. We also need to document the custom size somewhere in the Inputs documentation.

We usually put the doc and the code in the same PR, would you mind doing these changes in your PR?

@PedroPerpetua
Copy link
Contributor Author

PedroPerpetua commented Jan 4, 2024

We usually put the doc and the code in the same PR, would you mind doing these changes in your PR?

Yeah, I can give it a spin later today!

@fzaninotto fzaninotto changed the title Closes #9546; fixes hardcoded TextField size prop Fix hardcoded TextField size prop Jan 4, 2024
@PedroPerpetua
Copy link
Contributor Author

Okay, I've updated the documentation, and took the liberty of changing the theme configurations to use deepmerge and createTheme from MUI instead of object deconstruction, as suggested by MUI's docs. I find this to be a much better way for users to set up their own themes when basing themselves on the default themes, and this easily solves the issue of overriding a couple deep properties of the theme.

For example, using deepmerge, you can easily override just the variant prop in a TextField while keeping the size prop the same.

It's a "big breaking change" (old way would still work). I hope this is fine.

Copy link
Member

@fzaninotto fzaninotto left a comment

Choose a reason for hiding this comment

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

You're still missing a section in the v5 upgrade guide for people with a custom theme in v4.

main: '#2196f3',
dark: '#0069c0',
contrastText: '#fff',
export const defaultLightTheme: RaThemeOptions = createTheme(
Copy link
Member

Choose a reason for hiding this comment

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

you shouldn't call createTheme here as our theme provider does it, too.

dark: '#0069c0',
contrastText: '#fff',
export const defaultLightTheme: RaThemeOptions = createTheme(
deepmerge(defaultThemeInvariants, {
Copy link
Member

Choose a reason for hiding this comment

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

We don't need the deepMerge here, as we know what we're merging. I prefer the old version.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

deepMerge here allows new changes to be made more easily in the future, in my opinion. Imagine, as a crude example, you want the darkTheme to have their textfields with a width of 50% by default.

To do this change without using deepmerge:

const defaultDarkTheme: RaThemeOptions = {
    ...defaultThemeInvariants,
    palette: {
        mode: 'dark',
        primary: {
            main: '#90caf9',
        },
        background: {
            default: '#313131',
        },
    },
+    components: {
+        ...defaultThemeInvariants.components,
+        MuiTextField: {
+            ...defaultThemeInvariants.components.MuiTextField,
+            defaultProps: {
+                ...defaultThemeInvariants.components.MuiTextField.defaultProps,
+                sx: {
                    /* 
                    If you later add a default sx to defaultThemeInvariants,
                    you need to remember to come here and add the following line:

+                    ...defaultThemeInvariants.components.MuiTextField.defaultProps.sx,
                    
                    */
+                    width: "50%"
+                }
+            },
+        },
+    }
}

With deepmerge:

export const defaultDarkTheme: RaThemeOptions = deepmerge(defaultThemeInvariants, {
        palette: {
            mode: 'dark',
            primary: {
                main: '#90caf9',
            },
            background: {
                default: '#313131',
            },
        },
+        components: {
+            MuiTextField: {
+                defaultProps: {
+                    sx: {
+                        width: "50%"
+                    }
+                }
+            }
+        }
    })
);

The retro-active update mentioned in the comment being specially worrying in my opinion.

@PedroPerpetua
Copy link
Contributor Author

Alright, I'll give the review a spin, hopefully monday.

Removed legacy ToggleTheme and useTheme code
@PedroPerpetua
Copy link
Contributor Author

Okay, I've added the useTheme changes to the migration guide; I also went ahead and removed the legacy code on useTheme and the toggleThemeButton fully; it's a bit out of the original scope of this PR, but given that the original issue was a 1 line bugfix, I figured they can go along and the PR can be renamed as you find appropriate.

Regarding this:

We don't need the deepMerge here, as we know what we're merging. I prefer the old version.

@fzaninotto

I left a response to your comment; waiting on a reply to know if I should revert the changes there or keep them.

@PedroPerpetua
Copy link
Contributor Author

BUMPing this since I'm still awaiting on a reply from @fzaninotto to know if it needs more changes or is ready 😉

@djhi djhi requested a review from fzaninotto January 19, 2024 09:55
Copy link
Member

@fzaninotto fzaninotto left a comment

Choose a reason for hiding this comment

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

We're getting close to the merge!

docs/AppTheme.md Outdated Show resolved Hide resolved
docs/AppTheme.md Outdated Show resolved Hide resolved
docs/AppTheme.md Outdated Show resolved Hide resolved
Removed deprecated code from ToggleThemeButton
@PedroPerpetua
Copy link
Contributor Author

Fixed the latest comments and the merge conflits

@fzaninotto fzaninotto linked an issue Jan 23, 2024 that may be closed by this pull request
@fzaninotto fzaninotto merged commit 0624af3 into marmelab:next Jan 23, 2024
7 of 8 checks passed
@fzaninotto
Copy link
Member

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TextField "size" prop is not overrideable by theme
4 participants