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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TextField] MuiOutlinedInput
callback in styleOverrides sometimes passes ownerState as undefined
#31982
Comments
MuiOutlinedInput
callback in styleOverrides sometimes passes ownerState as undefined
I got curious by these 3 logs, I was expecting only 1. It turns out 3 looks because there are 3 slots. Each slot tries to resolve the style of the Root, using its own props 馃檭. When we have: const ButtonRoot = styled('button')({
name: 'Button',
slot: 'Root',
overridesResolver: (props, styles) => styles.root,
})
const ButtonLabel = styled('button')({
name: 'Button',
slot: 'Label',
overridesResolver: (props, styles) => styles.label,
})
<ButtonRoot ownerState={ownerState} data-testid="root">
<ButtonLabel ownerState={ownerState} data-testid="label">Foo</ButtonLabel>
</ButtonRoot> I fail to envision how we could justify having I could land on this fix, which makes more sense to me: diff --git a/packages/mui-system/src/createStyled.js b/packages/mui-system/src/createStyled.js
index c7d3016d1b..f8cec2ad98 100644
--- a/packages/mui-system/src/createStyled.js
+++ b/packages/mui-system/src/createStyled.js
@@ -137,12 +137,17 @@ export default function createStyled(input = {}) {
const styleOverrides = getStyleOverrides(componentName, theme);
if (styleOverrides) {
- const resolvedStyleOverrides = {};
- Object.entries(styleOverrides).forEach(([slotKey, slotStyle]) => {
- resolvedStyleOverrides[slotKey] =
- typeof slotStyle === 'function' ? slotStyle(props) : slotStyle;
- });
- return overridesResolver(props, resolvedStyleOverrides);
+ if (componentSlot) {
+ const slotCamelCase = lowercaseFirstLetter(componentSlot);
+
+ if (styleOverrides[slotCamelCase] && typeof styleOverrides[slotCamelCase] === 'function') {
+ return overridesResolver(props, {
+ [slotCamelCase]: styleOverrides[slotCamelCase](props),
+ });
+ }
+ }
+
+ return overridesResolver(props, styleOverrides);
}
return null; |
I know this is closed but I didn't understand the solution. I'm using version 5.2.3 so that I didn't open a new issue so I want to ask is now the after the updates the ownerState will be received always because if so I will update to the latest version. Btw why when I console log the props from within styleOverrides it runs three times? |
I agree that the current implementation is not very intuitive, but also we cannot restrict each slot to run the style overrides only for that slot, because sometimes in one slot more style overrides are being added, like: https://github.com/mui/material-ui/blob/master/packages/mui-material/src/Button/Button.js#L259 I propose we go with @siriwatknp's fix for now, and revisit this in v6. I am adding it to the milestone. |
To keep track for v6: Faced similar issue for the |
Duplicates
Latest version
Current behavior 馃槸
The ownerState parameter in the example below is sometimes undefined:
Expected behavior 馃
The ownerState parameter should never be undefined.
Workaround
Return an empty object if ownerState is undefined.
Steps to reproduce 馃暪
Steps:
See https://codesandbox.io/s/ownerstate-undefined-muioutlinedinput-theme-forked-vv40l9?file=/demo.tsx
Context 馃敠
Trying to override border color on MuiOutlinedInput.
Your environment 馃寧
`npx @mui/envinfo`
Browser: Microsoft Edge Version 99.0.1150.46 (Officiell version) (64 bitar)
tsconfig.json
The text was updated successfully, but these errors were encountered: