Skip to content

Commit 1a20fcf

Browse files
committed
[add] StyleSheet.compose
As per the recent addition to React Native.
1 parent ed1e45a commit 1a20fcf

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

benchmarks/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const fastestTests = [
2929
() => renderDeepTree('styletron', styletron),
3030
() => renderWideTree('styletron', styletron),
3131
() => renderDeepTree('aphrodite', aphrodite),
32-
() => renderWideTree('aphrodite', aphrodite),
32+
() => renderWideTree('aphrodite', aphrodite)
3333
];
3434

3535
/**
@@ -47,7 +47,7 @@ const restTests = [
4747
() => renderDeepTree('reactxp', xp),
4848
() => renderWideTree('reactxp', xp),
4949
() => renderDeepTree('radium', radium),
50-
() => renderWideTree('radium', radium),
50+
() => renderWideTree('radium', radium)
5151
];
5252

5353
const tests = [...coreTests];

docs/storybook/2-apis/StyleSheet/StyleSheetScreen.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ const StyleSheetScreen = () => (
2121
</Description>
2222

2323
<Section title="Methods">
24+
<DocItem
25+
description={
26+
<AppText>
27+
Combines two styles such that <Code>style2</Code> will override any styles in{' '}
28+
<Code>style1</Code>. If either style is falsy, the other one is returned without
29+
allocating an array, saving allocations and maintaining reference equality for{' '}
30+
<Code>PureComponent</Code> checks.
31+
</AppText>
32+
}
33+
example={{
34+
code: 'StyleSheet.compose(style1, style2);'
35+
}}
36+
name="compose"
37+
typeInfo="(style1, style2) => style"
38+
/>
39+
2440
<DocItem
2541
description="Each key of the object passed to `create` must define a style object. The returned object replaces style objects with IDs"
2642
example={{

src/apis/StyleSheet/__tests__/index-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ describe('apis/StyleSheet', () => {
2222
expect(isPlainObject(StyleSheet.absoluteFillObject) === true).toBeTruthy();
2323
});
2424

25+
describe('compose', () => {
26+
test('returns array when neither style is falsey', () => {
27+
expect(StyleSheet.compose(1, 2)).toEqual([1, 2]);
28+
});
29+
test('returns style1 when style2 is falsey', () => {
30+
expect(StyleSheet.compose(1, null)).toBe(1);
31+
});
32+
test('returns style2 when style1 is falsey', () => {
33+
expect(StyleSheet.compose(null, 2)).toBe(2);
34+
});
35+
});
36+
2537
describe('create', () => {
2638
test('replaces styles with numbers', () => {
2739
const style = StyleSheet.create({ root: { position: 'absolute' } });

src/apis/StyleSheet/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ const absoluteFill = StyleRegistry.register(absoluteFillObject);
3636
const StyleSheet = {
3737
absoluteFill,
3838
absoluteFillObject,
39+
compose(style1, style2) {
40+
if (style1 && style2) {
41+
return [style1, style2];
42+
} else {
43+
return style1 || style2;
44+
}
45+
},
3946
create(styles) {
4047
const result = {};
4148
Object.keys(styles).forEach(key => {

0 commit comments

Comments
 (0)