Skip to content

Commit

Permalink
Allow variant prop to be set for pseudo-props component
Browse files Browse the repository at this point in the history
  • Loading branch information
maurobender committed Sep 20, 2022
1 parent fba1f9c commit 97bcf82
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/components/main/IconButton/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { IPressableProps } from '../Pressable/types';
import type { IIconProps } from '../Icon/types';

export interface IIconButtonProps extends Omit<IPressableProps, 'children' | 'size'>,
Omit<IIconProps, 'hitSlop' | 'style'>,
Omit<IIconProps, 'hitSlop' | 'style' | 'variant'>,
IHoverableComponent
{
__icon?: ComponentBaseStyledProps<'Box'>
Expand Down
11 changes: 7 additions & 4 deletions src/core/components/Components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ export default class Components {
{ variant, ...props }: PropsWithVariant,
state: ComponentState = {}
): ComponentStyledProps<C> {
const defaultProps: ComponentStyledProps<C> = this
const defaultProps = this
.defaultPropsFor( componentName ) || {};

const variantProps: ComponentStyledProps<C> = (
const variantProps = (
variant ? this.variantPropsFor( componentName, variant ) : undefined
) || {};

const resolvedProps: ComponentStyledProps<C> = merge(
{}, defaultProps, variantProps, props
{} as ComponentStyledProps<C>,
defaultProps,
variantProps,
props
);

return this.applyStateProps( resolvedProps, state );
Expand All @@ -44,7 +47,7 @@ export default class Components {
variantPropsFor<C extends ComponentName>(
componentName: C,
variantName: VariantName
): ComponentStyledProps<C> | undefined {
): Omit<ComponentStyledProps<C>, 'variant'> | undefined {
return this.configFor( componentName )?.variants?.[ variantName ];
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/components/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type { ComponentName, VariantName } from './common';

export type ComponentConfig<C extends ComponentName> = Partial<{
defaultProps: ComponentStyledProps<C>,
variants: Record<VariantName, ComponentStyledProps<C>>
variants: Record<VariantName, Omit<ComponentStyledProps<C>, 'variant'>>
sizes?: Record<string, string | number>
}>;

Expand Down
5 changes: 5 additions & 0 deletions src/core/components/types/styledProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ export type ComponentBaseStyledProps<C extends ComponentName> = {
[K in (
keyof StyledProps
| keyof ComponentCustomProps<C>
| 'variant'
| 'size'
)]?:
K extends keyof StyledProps
? StyledProps[K]
: K extends keyof ComponentCustomProps<C>
? ComponentCustomProps<C>[K]
: K extends 'variant'
? C extends keyof IThemeConfig['components']
? VariantType<C>
: any
: K extends 'size'
? C extends keyof IThemeConfig['components']
? ComponentSizeType<C>
Expand Down
4 changes: 2 additions & 2 deletions src/core/theme/Theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {
} from '../typography/types';
import type Components from '../components/Components';
import type {
ComponentName, ComponentState, PropsWithVariant, VariantName
ComponentName, ComponentState, ComponentStyledProps, VariantName
} from '../components/types';
import type Layout from '../layout/Layout';
import type {
Expand Down Expand Up @@ -128,7 +128,7 @@ export default class Theme {

resolvePropsFor(
componentName: ComponentName,
props: PropsWithVariant,
props: ComponentStyledProps<ComponentName>,
state?: ComponentState
) {
return this._components.resolvePropsFor( componentName, props, state );
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useComponentPropsResolver.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useMemo } from 'react';
import type {
ComponentName, ComponentState, PropsWithVariant, ComponentStyledProps
ComponentName, ComponentState, ComponentBaseStyledProps, ComponentStyledProps
} from '../core/components/types';
import { useTheme } from '../core/theme/hooks';

const useComponentPropsResolver = <C extends ComponentName>(
componentName: C,
props: PropsWithVariant,
props: ComponentBaseStyledProps<C>,
state: ComponentState = {}
): ComponentStyledProps<C> => {
const theme = useTheme();
Expand Down

0 comments on commit 97bcf82

Please sign in to comment.