Skip to content

Commit a75afd3

Browse files
authored
Fix/custom modifiers typings (#750)
* Fix issue with custom boolean modifiers throwing errors * update generated types
1 parent 362a6f2 commit a75afd3

File tree

12 files changed

+58
-45
lines changed

12 files changed

+58
-45
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import React from 'react';
22
import * as Modifiers from './modifiers';
33
export interface BaseComponentInjectedProps {
4+
/**
5+
* All generated styles from the modifiers props
6+
*/
47
modifiers: ReturnType<typeof Modifiers.generateModifiersStyle>;
58
}
6-
declare function asBaseComponent<PROPS>(WrappedComponent: React.ComponentType<PROPS>): React.ComponentType<Omit<PROPS, keyof BaseComponentInjectedProps>>;
9+
declare function asBaseComponent<PROPS>(WrappedComponent: React.ComponentType<any>): React.ComponentType<PROPS>;
710
export default asBaseComponent;
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import React from 'react';
22
export interface ForwardRefInjectedProps {
3+
/**
4+
* The forwarded ref of the containing element
5+
*/
36
forwardedRef: any;
47
}
5-
export default function forwardRef<P>(WrappedComponent: React.ComponentType<P>): React.ComponentType<Omit<P, keyof ForwardRefInjectedProps>>;
8+
export default function forwardRef(WrappedComponent: React.ComponentType<any>): React.ComponentType<any>;

generatedTypes/commons/modifiers.d.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ export declare type TypographyLiterals = keyof typeof TypographyPresets;
6060
export declare type BorderRadiusLiterals = keyof typeof BorderRadiusesLiterals;
6161
export declare type AlignmentLiterals = 'row' | 'spread' | 'center' | 'centerH' | 'centerV' | 'left' | 'right' | 'top' | 'bottom';
6262
export declare type Modifier<T extends string> = Partial<Record<T, boolean>>;
63-
export declare type TypographyModifiers = Modifier<TypographyLiterals>;
64-
export declare type ColorsModifiers = Modifier<ColorLiterals>;
63+
export declare type CustomModifier = {
64+
[key: string]: any;
65+
};
66+
export declare type TypographyModifiers = Modifier<TypographyLiterals> & CustomModifier;
67+
export declare type ColorsModifiers = Modifier<ColorLiterals> & CustomModifier;
6568
export declare type BackgroundColorModifier = Modifier<'bg'>;
6669
export declare type AlignmentModifiers = Modifier<AlignmentLiterals>;
6770
export declare type PaddingModifiers = Modifier<PaddingLiterals>;

generatedTypes/commons/new.d.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export { default as UIComponent } from './UIComponent';
2-
export { default as asBaseComponent } from './asBaseComponent';
3-
export { BaseComponentInjectedProps } from './asBaseComponent';
4-
export { default as forwardRef } from './forwardRef';
5-
export { ForwardRefInjectedProps } from './forwardRef';
2+
export { default as asBaseComponent, BaseComponentInjectedProps } from './asBaseComponent';
3+
export { default as forwardRef, ForwardRefInjectedProps } from './forwardRef';
4+
export { ContainerModifiers, MarginModifiers, TypographyModifiers, ColorsModifiers } from './modifiers';

generatedTypes/components/text/index.d.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { PureComponent } from 'react';
22
import { TextProps, TextStyle } from 'react-native';
3-
import { BaseComponentInjectedProps, ForwardRefInjectedProps } from '../../commons/new';
4-
import { MarginModifiers, TypographyModifiers, ColorsModifiers } from '../../commons/modifiers';
5-
interface TextPropTypes extends TextProps {
3+
import { BaseComponentInjectedProps, ForwardRefInjectedProps, MarginModifiers, TypographyModifiers, ColorsModifiers } from '../../commons/new';
4+
interface TextPropTypes extends TextProps, TypographyModifiers, ColorsModifiers, MarginModifiers {
65
/**
76
* color of the text
87
*/
@@ -30,7 +29,7 @@ interface TextPropTypes extends TextProps {
3029
ref?: any;
3130
textAlign?: string;
3231
}
33-
declare type PropsTypes = BaseComponentInjectedProps & TextPropTypes & ForwardRefInjectedProps & MarginModifiers & TypographyModifiers & ColorsModifiers;
32+
declare type PropsTypes = BaseComponentInjectedProps & ForwardRefInjectedProps & TextPropTypes;
3433
/**
3534
* @description: A wrapper for Text component with extra functionality like modifiers support
3635
* @extends: Text
@@ -45,5 +44,5 @@ declare class Text extends PureComponent<PropsTypes> {
4544
render(): JSX.Element;
4645
}
4746
export { Text };
48-
declare const _default: React.ComponentType<Pick<Pick<PropsTypes, "black" | "white" | "dark10" | "dark20" | "dark30" | "dark40" | "dark50" | "dark60" | "dark70" | "dark80" | "grey10" | "grey20" | "grey30" | "grey40" | "grey50" | "grey60" | "grey70" | "grey80" | "blue10" | "blue20" | "blue30" | "blue40" | "blue50" | "blue60" | "blue70" | "blue80" | "cyan10" | "cyan20" | "cyan30" | "cyan40" | "cyan50" | "cyan60" | "cyan70" | "cyan80" | "green10" | "green20" | "green30" | "green40" | "green50" | "green60" | "green70" | "green80" | "yellow10" | "yellow20" | "yellow30" | "yellow40" | "yellow50" | "yellow60" | "yellow70" | "yellow80" | "orange10" | "orange20" | "orange30" | "orange40" | "orange50" | "orange60" | "orange70" | "orange80" | "red10" | "red20" | "red30" | "red40" | "red50" | "red60" | "red70" | "red80" | "purple10" | "purple20" | "purple30" | "purple40" | "purple50" | "purple60" | "purple70" | "purple80" | "violet10" | "violet20" | "violet30" | "violet40" | "violet50" | "violet60" | "violet70" | "violet80" | "margin" | "marginL" | "marginT" | "marginR" | "marginB" | "marginH" | "marginV" | "text10" | "text10T" | "text10L" | "text10R" | "text10M" | "text10BO" | "text10H" | "text10BL" | "text20" | "text20T" | "text20L" | "text20R" | "text20M" | "text20BO" | "text20H" | "text20BL" | "text20B" | "text30" | "text30T" | "text30L" | "text30R" | "text30M" | "text30BO" | "text30H" | "text30BL" | "text40" | "text40T" | "text40L" | "text40R" | "text40M" | "text40BO" | "text40H" | "text40BL" | "text50" | "text50T" | "text50L" | "text50R" | "text50M" | "text50BO" | "text50H" | "text50BL" | "text60" | "text60T" | "text60L" | "text60R" | "text60M" | "text60BO" | "text60H" | "text60BL" | "text60B" | "text65" | "text65T" | "text65L" | "text65R" | "text65M" | "text65BO" | "text65H" | "text65BL" | "text70" | "text70T" | "text70L" | "text70R" | "text70M" | "text70BO" | "text70H" | "text70BL" | "text80" | "text80T" | "text80L" | "text80R" | "text80M" | "text80BO" | "text80H" | "text80BL" | "text90" | "text90T" | "text90L" | "text90R" | "text90M" | "text90BO" | "text90H" | "text90BL" | "text100" | "text100T" | "text100L" | "text100R" | "text100M" | "text100BO" | "text100H" | "text100BL" | "center" | "style" | "testID" | "ref" | "modifiers" | "color" | "uppercase" | "highlightString" | "highlightStyle" | "animated" | "textAlign" | "allowFontScaling" | "ellipsizeMode" | "lineBreakMode" | "numberOfLines" | "onLayout" | "onPress" | "onLongPress" | "nativeID" | "maxFontSizeMultiplier" | "adjustsFontSizeToFit" | "minimumFontScale" | "suppressHighlighting" | "selectable" | "selectionColor" | "textBreakStrategy" | "accessible" | "accessibilityActions" | "accessibilityLabel" | "accessibilityRole" | "accessibilityStates" | "accessibilityState" | "accessibilityHint" | "accessibilityValue" | "onAccessibilityAction" | "accessibilityComponentType" | "accessibilityLiveRegion" | "importantForAccessibility" | "accessibilityElementsHidden" | "accessibilityTraits" | "accessibilityViewIsModal" | "onAccessibilityEscape" | "onAccessibilityTap" | "onMagicTap" | "accessibilityIgnoresInvertColors">, "black" | "white" | "dark10" | "dark20" | "dark30" | "dark40" | "dark50" | "dark60" | "dark70" | "dark80" | "grey10" | "grey20" | "grey30" | "grey40" | "grey50" | "grey60" | "grey70" | "grey80" | "blue10" | "blue20" | "blue30" | "blue40" | "blue50" | "blue60" | "blue70" | "blue80" | "cyan10" | "cyan20" | "cyan30" | "cyan40" | "cyan50" | "cyan60" | "cyan70" | "cyan80" | "green10" | "green20" | "green30" | "green40" | "green50" | "green60" | "green70" | "green80" | "yellow10" | "yellow20" | "yellow30" | "yellow40" | "yellow50" | "yellow60" | "yellow70" | "yellow80" | "orange10" | "orange20" | "orange30" | "orange40" | "orange50" | "orange60" | "orange70" | "orange80" | "red10" | "red20" | "red30" | "red40" | "red50" | "red60" | "red70" | "red80" | "purple10" | "purple20" | "purple30" | "purple40" | "purple50" | "purple60" | "purple70" | "purple80" | "violet10" | "violet20" | "violet30" | "violet40" | "violet50" | "violet60" | "violet70" | "violet80" | "margin" | "marginL" | "marginT" | "marginR" | "marginB" | "marginH" | "marginV" | "text10" | "text10T" | "text10L" | "text10R" | "text10M" | "text10BO" | "text10H" | "text10BL" | "text20" | "text20T" | "text20L" | "text20R" | "text20M" | "text20BO" | "text20H" | "text20BL" | "text20B" | "text30" | "text30T" | "text30L" | "text30R" | "text30M" | "text30BO" | "text30H" | "text30BL" | "text40" | "text40T" | "text40L" | "text40R" | "text40M" | "text40BO" | "text40H" | "text40BL" | "text50" | "text50T" | "text50L" | "text50R" | "text50M" | "text50BO" | "text50H" | "text50BL" | "text60" | "text60T" | "text60L" | "text60R" | "text60M" | "text60BO" | "text60H" | "text60BL" | "text60B" | "text65" | "text65T" | "text65L" | "text65R" | "text65M" | "text65BO" | "text65H" | "text65BL" | "text70" | "text70T" | "text70L" | "text70R" | "text70M" | "text70BO" | "text70H" | "text70BL" | "text80" | "text80T" | "text80L" | "text80R" | "text80M" | "text80BO" | "text80H" | "text80BL" | "text90" | "text90T" | "text90L" | "text90R" | "text90M" | "text90BO" | "text90H" | "text90BL" | "text100" | "text100T" | "text100L" | "text100R" | "text100M" | "text100BO" | "text100H" | "text100BL" | "center" | "style" | "testID" | "ref" | "color" | "uppercase" | "highlightString" | "highlightStyle" | "animated" | "textAlign" | "allowFontScaling" | "ellipsizeMode" | "lineBreakMode" | "numberOfLines" | "onLayout" | "onPress" | "onLongPress" | "nativeID" | "maxFontSizeMultiplier" | "adjustsFontSizeToFit" | "minimumFontScale" | "suppressHighlighting" | "selectable" | "selectionColor" | "textBreakStrategy" | "accessible" | "accessibilityActions" | "accessibilityLabel" | "accessibilityRole" | "accessibilityStates" | "accessibilityState" | "accessibilityHint" | "accessibilityValue" | "onAccessibilityAction" | "accessibilityComponentType" | "accessibilityLiveRegion" | "importantForAccessibility" | "accessibilityElementsHidden" | "accessibilityTraits" | "accessibilityViewIsModal" | "onAccessibilityEscape" | "onAccessibilityTap" | "onMagicTap" | "accessibilityIgnoresInvertColors">>;
47+
declare const _default: React.ComponentType<TextPropTypes>;
4948
export default _default;

generatedTypes/components/view/index.d.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React from 'react';
22
import { ViewProps } from 'react-native';
3-
import { BaseComponentInjectedProps, ForwardRefInjectedProps } from '../../commons/new';
4-
import { ContainerModifiers } from '../../commons/modifiers';
5-
interface ViewPropTypes extends ViewProps {
3+
import { ContainerModifiers } from '../../commons/new';
4+
interface ViewPropTypes extends ViewProps, ContainerModifiers {
65
/**
76
* If true, will render as SafeAreaView
87
*/
@@ -24,6 +23,5 @@ interface ViewPropTypes extends ViewProps {
2423
*/
2524
height?: string | number;
2625
}
27-
declare type PropsTypes = BaseComponentInjectedProps & ViewPropTypes & ForwardRefInjectedProps & ContainerModifiers;
28-
declare const _default: React.ComponentType<Pick<Pick<PropsTypes, "br0" | "br10" | "br20" | "br30" | "br40" | "br50" | "br60" | "br100" | "padding" | "paddingL" | "paddingT" | "paddingR" | "paddingB" | "paddingH" | "paddingV" | "margin" | "marginL" | "marginT" | "marginR" | "marginB" | "marginH" | "marginV" | "flex" | "flexG" | "flexS" | "bg" | "row" | "spread" | "center" | "centerH" | "centerV" | "left" | "right" | "top" | "bottom" | "style" | "testID" | "modifiers" | "animated" | "onLayout" | "nativeID" | "accessible" | "accessibilityActions" | "accessibilityLabel" | "accessibilityRole" | "accessibilityStates" | "accessibilityState" | "accessibilityHint" | "accessibilityValue" | "onAccessibilityAction" | "accessibilityComponentType" | "accessibilityLiveRegion" | "importantForAccessibility" | "accessibilityElementsHidden" | "accessibilityTraits" | "accessibilityViewIsModal" | "onAccessibilityEscape" | "onAccessibilityTap" | "onMagicTap" | "accessibilityIgnoresInvertColors" | "useSafeArea" | "inaccessible" | "width" | "height" | "hitSlop" | "pointerEvents" | "removeClippedSubviews" | "collapsable" | "needsOffscreenAlphaCompositing" | "renderToHardwareTextureAndroid" | "shouldRasterizeIOS" | "isTVSelectable" | "hasTVPreferredFocus" | "tvParallaxProperties" | "tvParallaxShiftDistanceX" | "tvParallaxShiftDistanceY" | "tvParallaxTiltAngle" | "tvParallaxMagnification" | "onStartShouldSetResponder" | "onMoveShouldSetResponder" | "onResponderEnd" | "onResponderGrant" | "onResponderReject" | "onResponderMove" | "onResponderRelease" | "onResponderStart" | "onResponderTerminationRequest" | "onResponderTerminate" | "onStartShouldSetResponderCapture" | "onMoveShouldSetResponderCapture" | "onTouchStart" | "onTouchMove" | "onTouchEnd" | "onTouchCancel" | "onTouchEndCapture">, "br0" | "br10" | "br20" | "br30" | "br40" | "br50" | "br60" | "br100" | "padding" | "paddingL" | "paddingT" | "paddingR" | "paddingB" | "paddingH" | "paddingV" | "margin" | "marginL" | "marginT" | "marginR" | "marginB" | "marginH" | "marginV" | "flex" | "flexG" | "flexS" | "bg" | "row" | "spread" | "center" | "centerH" | "centerV" | "left" | "right" | "top" | "bottom" | "style" | "testID" | "animated" | "onLayout" | "nativeID" | "accessible" | "accessibilityActions" | "accessibilityLabel" | "accessibilityRole" | "accessibilityStates" | "accessibilityState" | "accessibilityHint" | "accessibilityValue" | "onAccessibilityAction" | "accessibilityComponentType" | "accessibilityLiveRegion" | "importantForAccessibility" | "accessibilityElementsHidden" | "accessibilityTraits" | "accessibilityViewIsModal" | "onAccessibilityEscape" | "onAccessibilityTap" | "onMagicTap" | "accessibilityIgnoresInvertColors" | "useSafeArea" | "inaccessible" | "width" | "height" | "hitSlop" | "pointerEvents" | "removeClippedSubviews" | "collapsable" | "needsOffscreenAlphaCompositing" | "renderToHardwareTextureAndroid" | "shouldRasterizeIOS" | "isTVSelectable" | "hasTVPreferredFocus" | "tvParallaxProperties" | "tvParallaxShiftDistanceX" | "tvParallaxShiftDistanceY" | "tvParallaxTiltAngle" | "tvParallaxMagnification" | "onStartShouldSetResponder" | "onMoveShouldSetResponder" | "onResponderEnd" | "onResponderGrant" | "onResponderReject" | "onResponderMove" | "onResponderRelease" | "onResponderStart" | "onResponderTerminationRequest" | "onResponderTerminate" | "onStartShouldSetResponderCapture" | "onMoveShouldSetResponderCapture" | "onTouchStart" | "onTouchMove" | "onTouchEnd" | "onTouchCancel" | "onTouchEndCapture">>;
26+
declare const _default: React.ComponentType<ViewPropTypes>;
2927
export default _default;

src/commons/asBaseComponent.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import forwardRef from './forwardRef';
88
import UIComponent from './UIComponent';
99

1010
export interface BaseComponentInjectedProps {
11+
/**
12+
* All generated styles from the modifiers props
13+
*/
1114
modifiers: ReturnType<typeof Modifiers.generateModifiersStyle>;
1215
}
1316

14-
function asBaseComponent<PROPS>(WrappedComponent: React.ComponentType<PROPS>): React.ComponentType<Omit<PROPS, keyof BaseComponentInjectedProps>> {
17+
function asBaseComponent<PROPS>(WrappedComponent: React.ComponentType<any>): React.ComponentType<PROPS> {
1518
class BaseComponent extends UIComponent {
1619
state = Modifiers.generateModifiersStyle(undefined, BaseComponent.getThemeProps(this.props, this.context));
1720
static displayName: string | undefined;
@@ -34,6 +37,8 @@ function asBaseComponent<PROPS>(WrappedComponent: React.ComponentType<PROPS>): R
3437

3538
render() {
3639
const themeProps = BaseComponent.getThemeProps(this.props, this.context);
40+
// TODO: omit original modifiers props (left, right, flex, etc..)
41+
// Because they throws an error when being passed to RNView on Android
3742
const {forwardedRef, ...others} = themeProps;
3843
return <WrappedComponent /* {...this.props} */ {...others} modifiers={this.state} ref={forwardedRef}/>;
3944
}

src/commons/forwardRef.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ import React from 'react';
33
import hoistStatics from 'hoist-non-react-statics';
44

55
export interface ForwardRefInjectedProps {
6+
/**
7+
* The forwarded ref of the containing element
8+
*/
69
forwardedRef: any;
710
}
811

9-
export default function forwardRef<P>(WrappedComponent: React.ComponentType<P>): React.ComponentType<Omit<P, keyof ForwardRefInjectedProps>> {
10-
function forwardRef(props: P, ref: any) {
12+
export default function forwardRef(WrappedComponent: React.ComponentType<any>): React.ComponentType<any> {
13+
function forwardRef(props: any, ref: any) {
1114
return <WrappedComponent {...props} forwardedRef={ref}/>;
1215
}
1316

src/commons/modifiers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ export type AlignmentLiterals =
7474
| 'left' | 'right' | 'top' | 'bottom';
7575

7676
export type Modifier<T extends string> = Partial<Record<T, boolean>>
77+
export type CustomModifier = {[key: string]: any};
7778

78-
export type TypographyModifiers = Modifier<TypographyLiterals>;
79-
export type ColorsModifiers = Modifier<ColorLiterals>;
79+
export type TypographyModifiers = Modifier<TypographyLiterals> & CustomModifier;
80+
export type ColorsModifiers = Modifier<ColorLiterals> & CustomModifier;
8081
export type BackgroundColorModifier = Modifier<'bg'>;
8182
export type AlignmentModifiers = Modifier<AlignmentLiterals>;
8283
export type PaddingModifiers = Modifier<PaddingLiterals>;

src/commons/new.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// TODO: this file should replace commons/index.js
2-
32
export {default as UIComponent} from './UIComponent';
4-
export {default as asBaseComponent} from './asBaseComponent';
5-
export {BaseComponentInjectedProps} from './asBaseComponent';
6-
export {default as forwardRef} from './forwardRef';
7-
export {ForwardRefInjectedProps} from './forwardRef';
3+
export {default as asBaseComponent, BaseComponentInjectedProps} from './asBaseComponent';
4+
export {default as forwardRef, ForwardRefInjectedProps} from './forwardRef';
5+
export {ContainerModifiers, MarginModifiers, TypographyModifiers, ColorsModifiers} from './modifiers';

src/components/text/index.tsx

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import React, {PureComponent} from 'react';
22
import {Text as RNText, StyleSheet, TextProps, TextStyle, Animated} from 'react-native';
3-
import {asBaseComponent, forwardRef, BaseComponentInjectedProps, ForwardRefInjectedProps} from '../../commons/new';
3+
import {
4+
asBaseComponent,
5+
forwardRef,
6+
BaseComponentInjectedProps,
7+
ForwardRefInjectedProps,
8+
MarginModifiers,
9+
TypographyModifiers,
10+
ColorsModifiers
11+
} from '../../commons/new';
412
import {Colors} from '../../style';
513
import _ from 'lodash';
6-
import {MarginModifiers, TypographyModifiers, ColorsModifiers} from '../../commons/modifiers';
714

8-
interface TextPropTypes extends TextProps {
15+
16+
interface TextPropTypes extends TextProps, TypographyModifiers, ColorsModifiers, MarginModifiers {
917
/**
1018
* color of the text
1119
*/
@@ -35,13 +43,7 @@ interface TextPropTypes extends TextProps {
3543
textAlign?: string;
3644
}
3745

38-
type PropsTypes =
39-
& BaseComponentInjectedProps
40-
& TextPropTypes
41-
& ForwardRefInjectedProps
42-
& MarginModifiers
43-
& TypographyModifiers
44-
& ColorsModifiers;
46+
type PropsTypes = BaseComponentInjectedProps & ForwardRefInjectedProps & TextPropTypes;
4547

4648
/**
4749
* @description: A wrapper for Text component with extra functionality like modifiers support
@@ -88,7 +90,7 @@ class Text extends PureComponent<PropsTypes> {
8890

8991
if (!_.isEmpty(highlightString)) {
9092
if (_.isArray(children)) {
91-
return _.map(children, child => {
93+
return _.map(children, (child) => {
9294
return this.renderText(child);
9395
});
9496
}
@@ -155,4 +157,4 @@ const styles = StyleSheet.create({
155157

156158
export {Text}; // For tests
157159

158-
export default asBaseComponent(forwardRef(Text));
160+
export default asBaseComponent<TextPropTypes>(forwardRef(Text));

0 commit comments

Comments
 (0)