Skip to content

Commit

Permalink
fix: fixed typings for usePropsResolution
Browse files Browse the repository at this point in the history
  • Loading branch information
md-rehman committed Oct 28, 2021
1 parent e172c03 commit 6b361a5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
22 changes: 20 additions & 2 deletions src/hooks/useThemeProps/propsFlattener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const INITIAL_PROP_SPECIFICITY = {
[SPECIFICITY_1]: 0,
};

const pseudoPropsMap: any = {
const pseudoPropsMap = {
_web: { dependentOn: 'platform', priority: SPECIFICITY_10 },
_ios: { dependentOn: 'platform', priority: SPECIFICITY_10 },
_android: { dependentOn: 'platform', priority: SPECIFICITY_10 },
Expand Down Expand Up @@ -93,7 +93,14 @@ const pseudoPropsMap: any = {
respondTo: 'isDisabled',
priority: SPECIFICITY_100,
},
} as const;

type IPseudoPropsMap = typeof pseudoPropsMap;
type ExtractState<T extends IPseudoPropsMap> = {
// @ts-ignore
[P in keyof T as T[P]['respondTo']]?: boolean;
};
export type IStateProps = ExtractState<IPseudoPropsMap>;

export const compareSpecificity = (
exisiting: any,
Expand Down Expand Up @@ -127,12 +134,18 @@ const shouldResolvePseudoProp = ({
state,
platform,
colormode,
}: any) => {
}: {
property: keyof IPseudoPropsMap;
state: IStateProps;
platform: any;
colormode: any;
}) => {
if (pseudoPropsMap[property].dependentOn === 'platform') {
return property === `_${platform}`;
} else if (pseudoPropsMap[property].dependentOn === 'colormode') {
return property === `_${colormode}`;
} else if (pseudoPropsMap[property].dependentOn === 'state') {
// @ts-ignore
return state[pseudoPropsMap[property].respondTo];
} else {
return false;
Expand Down Expand Up @@ -162,10 +175,13 @@ const simplifyProps = (
};

if (
// @ts-ignore
state[pseudoPropsMap[property]?.respondTo] ||
['_dark', '_light', '_web', '_ios', '_android'].includes(property)
) {
// @ts-ignore
if (shouldResolvePseudoProp({ property, state, platform, colormode })) {
// @ts-ignore
propertySpecity[pseudoPropsMap[property].priority]++;

simplifyProps(
Expand All @@ -182,6 +198,7 @@ const simplifyProps = (
priority
);
}
// @ts-ignore
} else if (state[pseudoPropsMap[property]?.respondTo] === undefined) {
if (property.startsWith('_')) {
if (
Expand Down Expand Up @@ -229,6 +246,7 @@ export const propsFlattener = (

for (const property in props) {
if (
// @ts-ignore
state[pseudoPropsMap[property]?.respondTo] === undefined &&
property.startsWith('_')
) {
Expand Down
22 changes: 17 additions & 5 deletions src/hooks/useThemeProps/usePropsResolution.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { useColorMode } from '../../core/color-mode';
import { omitUndefined, extractInObject } from '../../theme/tools';
import { useContrastText } from '../useContrastText';
import { useBreakpointResolvedProps } from '../useBreakpointResolvedProps';
import { propsFlattener, compareSpecificity } from './propsFlattener';
import {
propsFlattener,
compareSpecificity,
IStateProps,
} from './propsFlattener';
import { useResponsiveSSRProps } from '../useResponsiveSSRProps';
import type { ComponentTheme } from '../../theme';

Expand Down Expand Up @@ -120,8 +124,12 @@ function propsSpreader(incomingProps: any, incomingSpecifity: any) {
export function usePropsResolution(
component: string,
incomingProps: any,
state?: any,
config?: any
state?: IStateProps,
config?: {
componentTheme: any;
resolveResponsively: Array<string>;
ignoreProps: Array<string>;
}
) {
const { theme } = useNativeBase();
const componentTheme =
Expand All @@ -138,8 +146,12 @@ export function usePropsResolution(
export const usePropsResolutionWithComponentTheme = (
componentTheme: ComponentTheme,
incomingProps: any,
state?: any,
config?: any
state?: IStateProps,
config?: {
componentTheme: any;
resolveResponsively: Array<string>;
ignoreProps: Array<string>;
}
) => {
const modifiedPropsForSSR = useResponsiveSSRProps(incomingProps);

Expand Down

0 comments on commit 6b361a5

Please sign in to comment.