-
Notifications
You must be signed in to change notification settings - Fork 960
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(theme): themed-style component (#176)
* chore(playground): themed component example * refactor(theme): module structure * feat(theme): themed component props forward * feat(theme): themed-styles component * refactor(theme): themed-styles component to separate file * test(theme): themed-styles component tests * test(theme): describe themed component and styled themed component tests * chore(playground): remove themed component example * chore(lint): update tslint to ignore arrow-functions semicolon * refactor(theme): fix code style issues * test(theme): add theme change test * refactor(theme): fix code style issues * test(theme): add theme provider overrides parent theme test * refactor(theme): remove untracked files
- Loading branch information
Showing
14 changed files
with
371 additions
and
152 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
export { ThemeProvider } from './theme-provider.component'; | ||
export { | ||
withTheme, | ||
ThemedComponentProps, | ||
} from './theme-consumer.component'; | ||
export { ThemeProvider } from './themeProvider.component'; | ||
export { withTheme } from './themeConsumer.component'; | ||
export { withThemedStyles } from './themeConsumerStyled.component'; | ||
export { ThemeType } from './type'; |
56 changes: 0 additions & 56 deletions
56
src/framework/theme/component/theme-consumer.component.tsx
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
src/framework/theme/component/theme-provider.component.tsx
This file was deleted.
Oops, something went wrong.
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,48 @@ | ||
import React, { ComponentType } from 'react'; | ||
import { ThemeType } from './type'; | ||
import { | ||
Consumer, | ||
forwardProps, | ||
} from '../service'; | ||
|
||
export interface Props<P> extends React.ClassAttributes<P> { | ||
theme: ThemeType; | ||
} | ||
|
||
export function withTheme<P extends Props<P>>(Component: ComponentType<P>) { | ||
type TExcept = Exclude<keyof P, keyof Props<P>>; | ||
type ForwardedProps = Pick<P, TExcept>; | ||
|
||
class Shadow extends React.Component<ForwardedProps> { | ||
wrappedComponentRef = undefined; | ||
getWrappedInstance = undefined; | ||
|
||
setWrappedComponentRef = (ref) => { | ||
this.wrappedComponentRef = ref; | ||
}; | ||
|
||
renderWrappedComponent = (theme: ThemeType) => ( | ||
<Component | ||
ref={this.setWrappedComponentRef} | ||
theme={theme} | ||
{...this.props} | ||
/> | ||
); | ||
|
||
render() { | ||
return ( | ||
<Consumer> | ||
{this.renderWrappedComponent} | ||
</Consumer> | ||
); | ||
} | ||
} | ||
|
||
const Result = Shadow; | ||
Result.prototype.getWrappedInstance = function getWrappedInstance() { | ||
const hasWrappedInstance = this.wrappedComponentRef && this.wrappedComponentRef.getWrappedInstance; | ||
return hasWrappedInstance ? this.wrappedComponentRef.getWrappedInstance() : this.wrappedComponentRef; | ||
}; | ||
|
||
return forwardProps(Component, Result); | ||
} |
61 changes: 61 additions & 0 deletions
61
src/framework/theme/component/themeConsumerStyled.component.tsx
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,61 @@ | ||
import React from 'react'; | ||
import { StyleSheet } from 'react-native'; | ||
import { | ||
ThemeType, | ||
ThemedStyleType, | ||
StyleSheetType, | ||
} from './type'; | ||
import { | ||
Consumer, | ||
forwardProps, | ||
} from '../service'; | ||
|
||
export interface Props<P> extends React.ClassAttributes<P> { | ||
theme: ThemeType; | ||
themedStyle: ThemedStyleType; | ||
} | ||
|
||
export function withThemedStyles<P extends Props<P>>( | ||
Component: React.ComponentType<P>, | ||
createStyles: (theme: ThemeType) => StyleSheetType, | ||
) { | ||
type TExcept = Exclude<keyof P, keyof Props<P>>; | ||
type ForwardedProps = Pick<P, TExcept>; | ||
|
||
class Shadow extends React.Component<ForwardedProps> { | ||
wrappedComponentRef = undefined; | ||
getWrappedInstance = undefined; | ||
|
||
setWrappedComponentRef = (ref) => { | ||
this.wrappedComponentRef = ref; | ||
}; | ||
|
||
renderWrappedComponent = (theme: ThemeType) => { | ||
const styles = StyleSheet.create(createStyles(theme)); | ||
return ( | ||
<Component | ||
ref={this.setWrappedComponentRef} | ||
theme={theme} | ||
themedStyle={styles} | ||
{...this.props} | ||
/> | ||
); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<Consumer> | ||
{this.renderWrappedComponent} | ||
</Consumer> | ||
); | ||
} | ||
} | ||
|
||
const Result = Shadow; | ||
Result.prototype.getWrappedInstance = function getWrappedInstance() { | ||
const hasWrappedInstance = this.wrappedComponentRef && this.wrappedComponentRef.getWrappedInstance; | ||
return hasWrappedInstance ? this.wrappedComponentRef.getWrappedInstance() : this.wrappedComponentRef; | ||
}; | ||
|
||
return forwardProps(Component, Result); | ||
} |
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,24 @@ | ||
import React, { ReactNode } from 'react'; | ||
import { Provider } from '../service'; | ||
import { ThemeType } from './type'; | ||
|
||
interface Props { | ||
children: JSX.Element | ReactNode; | ||
theme: ThemeType; | ||
} | ||
|
||
export class ThemeProvider extends React.PureComponent<Props> { | ||
|
||
static defaultProps = { | ||
theme: {}, | ||
}; | ||
|
||
render() { | ||
return ( | ||
<Provider | ||
value={this.props.theme}> | ||
{this.props.children} | ||
</Provider> | ||
); | ||
} | ||
} |
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,5 @@ | ||
// TODO(@type): declare theme types | ||
|
||
export type ThemeType = any; | ||
export type ThemedStyleType = any; | ||
export type StyleSheetType = any; |
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 +1,2 @@ | ||
export * from './component'; | ||
export * from './service'; |
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,2 +1,5 @@ | ||
export { Consumer, Provider } from './react-context.service'; | ||
export { forwardProps } from './react-props-mapping.service'; | ||
export { | ||
Provider, | ||
Consumer, | ||
} from './reactContext.service'; | ||
export { forwardProps } from './reactPropsForward.service'; |
This file was deleted.
Oops, something went wrong.
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,14 @@ | ||
import React from 'react'; | ||
import { ThemeType } from '../component'; | ||
|
||
const defaultThemeValue: ThemeType = {}; | ||
|
||
const { | ||
Provider, | ||
Consumer, | ||
} = React.createContext(defaultThemeValue); | ||
|
||
export { | ||
Provider, | ||
Consumer, | ||
}; |
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
Oops, something went wrong.