-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
[typescript] createStyles and improved WithStyles helpers #11609
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
Changes from all commits
ed00c84
ea88ab8
cb6adb7
d033ba3
8094a7d
e51ee27
828e664
15490cd
e3d28e3
4c89da6
70c9c16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,7 @@ export { | |
Theme, | ||
withStyles, | ||
WithStyles, | ||
createStyles, | ||
withTheme, | ||
WithTheme, | ||
} from './styles'; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { CSSProperties, StyleRules } from './withStyles'; | ||
|
||
/** | ||
* This function doesn't really "do anything" at runtime, it's just the identity | ||
* function. Its only purpose is to defeat TypeScript's type widening when providing | ||
* style rules to `withStyles` which are a function of the `Theme`. | ||
* | ||
* @param styles a set of style mappings | ||
* @returns the same styles that were passed in | ||
*/ | ||
export default function createStyles<S extends StyleRules>(styles: S): S; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// @flow | ||
|
||
export default function createStyles(s: Object) { | ||
return s; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { assert } from 'chai'; | ||
import { createStyles } from '.'; | ||
|
||
describe('createStyles', () => { | ||
it('is the identity function', () => { | ||
const styles = {}; | ||
assert.strictEqual(createStyles(styles), styles); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a little nit-picking, but does If not, we could add another tests that just check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,17 +37,22 @@ export interface WithStylesOptions<ClassKey extends string = string> extends JSS | |
|
||
export type ClassNameMap<ClassKey extends string = string> = Record<ClassKey, string>; | ||
|
||
export interface WithStyles<ClassKey extends string = string> extends Partial<WithTheme> { | ||
classes: ClassNameMap<ClassKey>; | ||
} | ||
export type WithStyles<T extends string | StyleRules | StyleRulesCallback> = Partial<WithTheme> & { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is absolutely awesome and a good use case for conditional types 🤩 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @geirsagberg right you are. I've opened #11808 to fix that. |
||
classes: ClassNameMap< | ||
T extends string ? T : | ||
T extends StyleRulesCallback<infer K> ? K : | ||
T extends StyleRules<infer K> ? K : | ||
never | ||
>; | ||
}; | ||
|
||
export interface StyledComponentProps<ClassKey extends string = string> { | ||
classes?: Partial<ClassNameMap<ClassKey>>; | ||
innerRef?: React.Ref<any> | React.RefObject<any>; | ||
} | ||
|
||
export default function withStyles<ClassKey extends string>( | ||
style: StyleRules<ClassKey> | StyleRulesCallback<ClassKey>, | ||
style: StyleRulesCallback<ClassKey> | StyleRules<ClassKey>, | ||
options?: WithStylesOptions<ClassKey>, | ||
): { | ||
<P extends ConsistentWith<P, StyledComponentProps<ClassKey>>>( | ||
|
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 funny, life is a circle, we had this function in
v1.0.0-alpha.x
.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.
I didn't know that... did it serve the same purpose?
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.
Nop, we used it to perform some operations on the styles.