-
-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[react-jss] Merge classes instead of overwriting (#946)
* Add forwardRef support * Add support for merging the classes * Fix path of test file * Remove jsdoc comment * Implement custom memoize-one * Convert function to arrow function
- Loading branch information
Henri
authored
Dec 31, 2018
1 parent
9784b93
commit 70a05ad
Showing
6 changed files
with
67 additions
and
74 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +0,0 @@ | ||
// @flow | ||
import type {Classes} from 'jss' | ||
/** | ||
* Adds `composes` property to each top level rule | ||
* in order to have a composed class name for dynamic style sheets. | ||
* | ||
* It relies on jss-plugin-compose and jss-plugin-extend plugins. | ||
* | ||
* Example: | ||
* classes: {left: 'a', button: 'b'} | ||
* styles: {button: {height: () => { ... }}} | ||
* composed: { | ||
* button: { | ||
* composes: 'b', | ||
* height: () => { ... } | ||
* }, | ||
* left: { | ||
* composes: 'a' | ||
* } | ||
* } | ||
*/ | ||
export default (staticClasses: Classes, dynamicClasses: Classes) => { | ||
const combinedClasses = {...staticClasses} | ||
|
||
for (const name in dynamicClasses) { | ||
combinedClasses[name] = staticClasses[name] | ||
? `${staticClasses[name]} ${dynamicClasses[name]}` | ||
: dynamicClasses[name] | ||
} | ||
|
||
return combinedClasses | ||
} | ||
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,17 @@ | ||
// @flow | ||
|
||
const memoize = <Arg, Return>(fn: (arg: Arg) => Return) => { | ||
let lastArg | ||
let lastResult | ||
|
||
return (arg: Arg): Return => { | ||
if (typeof lastArg === 'undefined' || lastArg !== arg) { | ||
lastArg = arg | ||
lastResult = fn(arg) | ||
} | ||
|
||
return lastResult | ||
} | ||
} | ||
|
||
export default memoize |
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,17 @@ | ||
// @flow | ||
import type {Classes} from 'jss' | ||
|
||
function mergeClasses(baseClasses: Classes, additionalClasses: Classes) { | ||
const combinedClasses = {...baseClasses} | ||
|
||
for (const name in additionalClasses) { | ||
combinedClasses[name] = | ||
name in combinedClasses | ||
? `${combinedClasses[name]} ${additionalClasses[name]}` | ||
: additionalClasses[name] | ||
} | ||
|
||
return combinedClasses | ||
} | ||
|
||
export default mergeClasses |
6 changes: 3 additions & 3 deletions
6
packages/react-jss/src/compose.test.js → packages/react-jss/src/merge-classes.test.js
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