Skip to content

Commit

Permalink
Merge 74019c7 into 111f533
Browse files Browse the repository at this point in the history
  • Loading branch information
heyjul3s committed Jan 2, 2021
2 parents 111f533 + 74019c7 commit d07ed9c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
15 changes: 10 additions & 5 deletions packages/component-generator/src/createComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function createComponents<
if (hasKey(settings, prop)) {
dict[prop] = isConfigBase
? generateComponent<ThemeType, Props, Element>(base, setting)
: createFC(base, setting);
: createFC<Props>(base, setting);
dict[prop].displayName = prop;
}
return dict;
Expand All @@ -45,12 +45,17 @@ export function createComponents<
export function createBaseComponent<
Config = any,
ThemeType = any,
Props = Record<string, unknown>
Props = Record<string, unknown>,
Element = HTMLDivElement
>(base): ComponentsRecord<Config, Props, ThemeType> {
const dict = {} as ComponentsRecord<Config, Props, ThemeType>;

if (isPlainObject(base)) {
dict.Base = createStyledComponent(base);
dict.Base = createStyledComponent<
Props,
ThemeType,
AllHTMLAttributes<Element>
>(base);
}

return dict;
Expand All @@ -61,12 +66,12 @@ export function generateComponent<
Props = Record<string, unknown>,
Element = HTMLDivElement
>(base, setting) {
return createStyledComponent({
return createStyledComponent<Props, ThemeType, AllHTMLAttributes<Element>>({
...base,
styles: { ...base.styles, ...setting },
attrs: { ...base.attrs, ...setting.attrs } || {},
element: !!setting.as ? setting.as : base.element
} as StyledComponentConfig<Props & Variant<ThemeType>, AllHTMLAttributes<Element>, ThemeType>);
} as StyledComponentConfig<Props & Variant<ThemeType>, ThemeType, AllHTMLAttributes<Element>>);
}

/* eslint-disable @typescript-eslint/no-explicit-any */
Expand Down
4 changes: 1 addition & 3 deletions packages/component-generator/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ export type CSSPseudos = { [K in CSS.Pseudos]?: CSSObjectWithScale };
export type StyledSystemCSSObject = CSSObjectWithScale & CSSPseudos;

export type ComponentsRecord<Dict, Props, ThemeType> = {
[key in keyof Dict | 'Base']: React.FC<
Props & BaseProps<ThemeType> & Variant<ThemeType>
>;
[key in keyof Dict | 'Base']: React.FC<Props & BaseProps<ThemeType>>;
};

export type Settings<Element = HTMLDivElement> = {
Expand Down
15 changes: 13 additions & 2 deletions packages/text-input/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,25 @@ const inputs = {
}
};

type BasicUsageProps = {
disabled?: boolean;
placeholder?: string;
onBlur?: () => void;
};

const { Base: InputText, InputSearch, InputUrl } = createTextInputs<
typeof inputs.components
typeof inputs.components,
BasicUsageProps
>(inputs.base, inputs.components);

export function BasicUsage() {
const onBlur = (text: string) => () => {
console.log('Blur', text);
};

return (
<>
<InputText />
<InputText onBlur={onBlur('Hello')} />
<InputSearch />
<InputUrl />
</>
Expand Down
8 changes: 4 additions & 4 deletions packages/text-input/src/createTextInputs.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { AllHTMLAttributes } from 'react';
import { AllHTMLAttributes } from 'react';
import {
createComponents,
ComponentsRecord,
Settings,
StyledComponentConfig,
Variant
StyledComponentConfig
} from '@artifak/component-generator';

/* eslint-disable @typescript-eslint/no-explicit-any */
Expand All @@ -15,7 +15,7 @@ export function createTextInputs<
>(
base: StyledComponentConfig<Props, ThemeType, AllHTMLAttributes<Element>>,
settings: Settings<Element>
): { [key in keyof Config | 'Base']: React.FC<Props & Variant<ThemeType>> } {
): ComponentsRecord<Config, Props, ThemeType> {
return createComponents<Config, ThemeType, Props, Element>(
{
element: 'input',
Expand Down

0 comments on commit d07ed9c

Please sign in to comment.