-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[Select] Fix classes merge issue #11904
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import warning from 'warning'; | ||
import getDisplayName from 'recompose/getDisplayName'; | ||
|
||
function mergeClasses(options = {}) { | ||
const { baseClasses, newClasses, Component, noBase = false } = options; | ||
|
||
if (!newClasses) { | ||
return baseClasses; | ||
} | ||
|
||
return { | ||
...baseClasses, | ||
...Object.keys(newClasses).reduce((accumulator, key) => { | ||
warning( | ||
baseClasses[key] || noBase, | ||
[ | ||
`Material-UI: the key \`${key}\` ` + | ||
`provided to the classes property is not implemented in ${getDisplayName(Component)}.`, | ||
`You can only override one of the following: ${Object.keys(baseClasses).join(',')}`, | ||
].join('\n'), | ||
); | ||
|
||
warning( | ||
!newClasses[key] || typeof newClasses[key] === 'string', | ||
[ | ||
`Material-UI: the key \`${key}\` ` + | ||
`provided to the classes property is not valid for ${getDisplayName(Component)}.`, | ||
`You need to provide a non empty string instead of: ${newClasses[key]}.`, | ||
].join('\n'), | ||
); | ||
|
||
if (newClasses[key]) { | ||
accumulator[key] = `${baseClasses[key]} ${newClasses[key]}`; | ||
} | ||
|
||
return accumulator; | ||
}, {}), | ||
}; | ||
} | ||
|
||
export default mergeClasses; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oliviertassinari Sorry to bother you here but I think this may be an issue...
I see the condition
|| typeof newClasses[key] === 'string'
on line 24 and then this message:...You need to provide a non empty string instead of...
I'm confused... is this right? I'm getting this warning but it works (the class is applied...
newClasses[key]
is a non empty string).That's the message I get
Should this
||
be a&&
? Or should the message be slightly different? Or... I'm completely wrong, of course...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nicosommi Do you have a reproduction example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@oliviertassinari I found what is happening. As I suspected, it is not an issue with MUI :)
I was passing what the glamor
css
function returns (implementing my own override instead of usingwithStyles
HOC), which is not a string but an object with atoString
method on it. So it goes wrong through this validation, but when converted to string, it works. I solved by usingtoString()
when passing it to MUI. So now no warning, and it works!Sorry for the delayed response, I was digging into it and I just found the real cause. It may help for future reference if someone is trying to take a similar approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's good to now. I guess it's better to be explicit about it. I have seen too many people try crazy stuff with this API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
In my case I'm trying to avoid using the HOC mainly because it is not friendly with the new React Context API (correct me if I'm wrong), and thus I created a standard function that process the style objects when I call it and pass the theme with a custom provider I created using this new context API with the render prop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does it mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That it is using this instead of this
In practical terms the new context is used in a declarative way by rendering a consumer and using the children on the default render prop.
So my component wrapper looks like this:
Instead of creating the HOC based on the legacy context API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's an implementation detail. It doesn't matter to end users. You can very well using recompose toRenderProps helper with withTheme HOC. Or use the 11 lines render prop wrapper we document for withStyles.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, it is.
😲 Thank you for the tip! I didn't know about that helper
Do you have a link? I can't find that example (I'm reading here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.