11import React from 'react' ;
22import hoistNonReactStatics from 'hoist-non-react-statics' ;
3+ import { ThemeMappingType } from '../mapping' ;
34import {
45 ThemeType ,
56 StyleType ,
67} from '../theme' ;
7- import {
8- ThemeMappingType ,
9- ComponentMappingType ,
10- } from '../mapping' ;
118import ThemeContext from '../theme/themeContext' ;
129import MappingContext from '../mapping/mappingContext' ;
1310import {
1411 createStyle ,
15- getComponentMapping ,
1612 StyleConsumerService ,
17- } from '../../service' ;
13+ } from '../../service/style' ;
14+ import { getComponentMapping } from '../../service/mapping' ;
15+ import { Interaction } from './type' ;
1816
1917interface PrivateProps < T > {
2018 forwardedRef : React . RefObject < T > ;
@@ -29,50 +27,58 @@ export interface Props {
2927 appearance ?: string ;
3028 theme ?: ThemeType ;
3129 themedStyle ?: StyleType ;
32- requestStateStyle ?: ( state : string [ ] ) => StyleType ;
30+ dispatch ?: ( interaction : Interaction [ ] ) => void ;
31+ }
32+
33+ interface State {
34+ interaction : Interaction [ ] ;
3335}
3436
3537export const styled = < T extends React . Component , P extends object > ( Component : React . ComponentClass < P > ) => {
3638
3739 type ComponentProps = Props & P ;
3840 type WrapperProps = PrivateProps < T > & ComponentProps ;
3941
40- class Wrapper extends React . Component < WrapperProps > {
42+ class Wrapper extends React . Component < WrapperProps , State > {
4143
4244 service : StyleConsumerService = new StyleConsumerService ( ) ;
4345
44- getComponentName = ( ) : string => Component . displayName || Component . name ;
45-
46- createComponentStyle = ( theme : ThemeType ,
47- mapping : ComponentMappingType ,
48- appearance : string ,
49- variant : string [ ] ,
50- state : string [ ] ) : StyleType => {
46+ state : State = {
47+ interaction : [ ] ,
48+ } ;
5149
52- if ( state . length === 0 ) {
53- console . warn ( 'Redundant `requestStateStyle` call! Use ` this.props.themedStyle` instead!' ) ;
54- }
55- return createStyle ( theme , mapping , appearance , variant , state ) ;
50+ private onDispatch = ( interaction : Interaction [ ] ) => {
51+ this . setState ( {
52+ interaction : interaction ,
53+ } ) ;
5654 } ;
5755
58- createCustomProps = ( props : ConsumerProps , componentProps : P & Props ) : Props => {
56+ private getComponentName = ( ) : string => Component . displayName || Component . name ;
57+
58+ private createCustomProps = ( props : ConsumerProps , componentProps : P & Props ) : Props => {
5959 const mapping = getComponentMapping ( props . mapping , this . getComponentName ( ) ) ;
60- const variants = this . service . getVariantPropKeys < P & Props > ( mapping , componentProps ) ;
60+
61+ const style = createStyle (
62+ props . theme ,
63+ mapping ,
64+ componentProps . appearance ,
65+ this . service . getVariantPropKeys ( mapping , componentProps ) ,
66+ this . service . getStatePropKeys ( mapping , componentProps , this . state . interaction ) ,
67+ ) ;
6168
6269 return {
6370 appearance : componentProps . appearance ,
6471 theme : props . theme ,
65- themedStyle : createStyle ( props . theme , mapping , componentProps . appearance , variants ) ,
66- requestStateStyle : state => {
67- return this . createComponentStyle ( props . theme , mapping , componentProps . appearance , variants , state ) ;
68- } ,
72+ themedStyle : style ,
73+ dispatch : this . onDispatch ,
6974 } ;
7075 } ;
7176
7277 renderWrappedComponent = ( props : ConsumerProps ) => {
7378 // TS issue: with spreading Generics https://github.com/Microsoft/TypeScript/issues/15792
7479 const { forwardedRef, ...restProps } = this . props as PrivateProps < T > ;
7580 const componentProps = restProps as P & Props ;
81+
7682 return (
7783 < Component
7884 ref = { forwardedRef }
0 commit comments