Skip to content

Commit

Permalink
feat(theme): styled and theme components statics copy
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Yorsh authored and malashkevich committed Dec 10, 2018
1 parent d75cacb commit 07d4bcf
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@
"ts-jest": "^23.10.4",
"tslint": "^5.11.0",
"typescript": "^3.1.6"
},
"dependencies": {
"hoist-non-react-statics": "^3.2.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';
import {
ThemeType,
StyleType,
Expand Down Expand Up @@ -74,5 +75,8 @@ export const StyledComponent = <T extends React.Component, P extends object>(Com
);
};

return React.forwardRef<T, P>(RefForwardingFactory as any);
const RefForwardingComponent = React.forwardRef<T, P>(RefForwardingFactory as any);
hoistNonReactStatics(RefForwardingComponent, Component);

return RefForwardingComponent;
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { StyleSheet } from 'react-native';
import hoistNonReactStatics from 'hoist-non-react-statics';
import ThemeContext from './themeContext';
import {
ThemeType,
Expand Down Expand Up @@ -65,5 +65,8 @@ export const withStyles = <T extends React.Component, P extends object>(Componen
);
};

return React.forwardRef<T, P>(RefForwardingFactory as any);
const RefForwardingComponent = React.forwardRef<T, P>(RefForwardingFactory as any);
hoistNonReactStatics(RefForwardingComponent, Component);

return RefForwardingComponent;
};
9 changes: 9 additions & 0 deletions src/framework/theme/tests/style.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ describe('@style: style consumer checks', () => {
expect(styledComponent.props.themedStyle.textColor).toEqual(config.values.textSuccess);
});

it('static methods are copied over', async () => {
// @ts-ignore: test-case
Test.staticMethod = function() {};
const StyleConsumer = StyledComponent(Test);

// @ts-ignore: test-case
expect(StyleConsumer.staticMethod).not.toBeUndefined();
});

});

describe('@style: complex hierarchy checks', async () => {
Expand Down
9 changes: 9 additions & 0 deletions src/framework/theme/tests/theme.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ describe('@theme: theme consumer checks', () => {
expect(themedComponent1Color).not.toEqual(themedComponent2Color);
});

it('static methods are copied over', async () => {
// @ts-ignore: test-case
ThemedConsumer.staticMethod = function() {};
const ThemedComponent = withStyles(ThemedConsumer);

// @ts-ignore: test-case
expect(ThemedComponent.staticMethod).not.toBeUndefined();
});

});

describe('@theme: service methods checks', () => {
Expand Down
6 changes: 5 additions & 1 deletion src/playground/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ module.exports = {
watchFolders: [
path.resolve(__dirname, '../framework'),
path.resolve(__dirname, '../../node_modules/@babel'),

// FIXME(playground): unable to resolve hoist-non-react-statics
path.resolve(__dirname, '../../node_modules/hoist-non-react-statics'),
path.resolve(__dirname, '../../node_modules/react-is'),
],
};
};

0 comments on commit 07d4bcf

Please sign in to comment.