Skip to content

Commit cd60eb2

Browse files
authored
feat(theme): style consumer dispatch actions (#221)
* refactor(ui): apply jest snapshot resolver * refactor(theme): apply jest snapshot resolver
1 parent ca37382 commit cd60eb2

36 files changed

+591
-418
lines changed

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
},
1818
cacheDirectory: './dist/jest/cache',
1919
coverageDirectory: './dist/jest/coverage',
20+
snapshotResolver: '<rootDir>/jest.config.snapshot',
2021
moduleNameMapper: pathsToModuleNameMapper(jestConfig.compilerOptions.paths, {
2122
prefix: '<rootDir>/',
2223
}),

jest.config.snapshot.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
resolveSnapshotPath: (path, extension) => {
3+
return `${path}${extension}`;
4+
},
5+
resolveTestPath: (path, extension) => {
6+
return path.slice(0, -extension.length);
7+
},
8+
};

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
"babel-jest": "^23.6.0",
4040
"coveralls": "^3.0.2",
4141
"husky": "^1.1.2",
42-
"jest": "^23.6.0",
42+
"jest": "^24.0.0-alpha.9",
4343
"metro-react-native-babel-preset": "^0.49.0",
4444
"react": "^16.6.0",
4545
"react-addons-test-utils": "^15.6.2",
@@ -49,7 +49,7 @@
4949
"react-native-typescript-transformer": "^1.2.10",
5050
"react-test-renderer": "^16.6.0",
5151
"rimraf": "^2.6.2",
52-
"ts-jest": "^23.10.4",
52+
"ts-jest": "^23.10.5",
5353
"tslint": "^5.11.0",
5454
"typescript": "^3.1.6"
5555
},

src/framework/theme/component/style/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ export {
77
styled,
88
Props as StyledComponentProps,
99
} from './styleConsumer.component';
10+
11+
export * from './type';

src/framework/theme/component/style/styleConsumer.component.tsx

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import React from 'react';
22
import hoistNonReactStatics from 'hoist-non-react-statics';
3+
import { ThemeMappingType } from '../mapping';
34
import {
45
ThemeType,
56
StyleType,
67
} from '../theme';
7-
import {
8-
ThemeMappingType,
9-
ComponentMappingType,
10-
} from '../mapping';
118
import ThemeContext from '../theme/themeContext';
129
import MappingContext from '../mapping/mappingContext';
1310
import {
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

1917
interface 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

3537
export 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}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export enum Interaction {
2+
ACTIVE = 'active',
3+
}
4+
5+
export enum State {
6+
CHECKED = 'checked',
7+
DISABLED = 'disabled',
8+
FOCUSED = 'focused',
9+
}
10+
11+
export namespace Interaction {
12+
export function parse(interaction: string): Interaction | undefined {
13+
return Interaction[interaction.toUpperCase()];
14+
}
15+
}
16+
17+
export namespace State {
18+
export function parse(state: string): State | undefined {
19+
return State[state.toUpperCase()];
20+
}
21+
}

src/framework/theme/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export {
1616
ThemedStyleType,
1717
StyleSheetType,
1818
StyleType,
19+
Interaction,
1920
} from './component';
2021

2122
export {

src/framework/theme/service/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './mappingUtil.service';
2-
export * from './themeUtil.service';
3-
export * from './styleUtil.service';
4-
export * from './styleConsumer.service';
1+
export * from './mapping';
2+
export * from './theme';
3+
export * from './style';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './mapping.service';

src/framework/theme/service/mappingUtil.service.ts renamed to src/framework/theme/service/mapping/mapping.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
VariantGroupType,
66
MappingType,
77
StateType,
8-
} from '../component';
8+
} from '../../component';
99

1010
export const APPEARANCE_DEFAULT = 'default';
1111

0 commit comments

Comments
 (0)