Skip to content

Commit

Permalink
refactor: defaultXComponentStyleConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
YumoImer committed Sep 20, 2024
1 parent ee5dac7 commit 1d5153b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
15 changes: 11 additions & 4 deletions components/_util/hooks/use-x-component-config.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import React from 'react';
import XProviderContext, { defaultXComponentConfig } from '../../x-provider/context';
import XProviderContext from '../../x-provider/context';

import type { DefaultXComponentConfig, XComponentsConfig } from '../../x-provider/context';
import type { XComponentStyleConfig, XComponentsConfig } from '../../x-provider/context';

const defaultXComponentStyleConfig: XComponentStyleConfig = {
classNames: {},
styles: {},
className: '',
style: {},
};

const useXComponentConfig = <C extends keyof XComponentsConfig>(
component: C,
): Required<XComponentsConfig>[C] & DefaultXComponentConfig => {
): Required<XComponentsConfig>[C] & XComponentStyleConfig => {
const xProviderContext = React.useContext(XProviderContext);

return React.useMemo(
() => ({
...defaultXComponentConfig,
...defaultXComponentStyleConfig,
...xProviderContext[component],
}),
[xProviderContext[component]],
Expand Down
15 changes: 3 additions & 12 deletions components/x-provider/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React from 'react';

import type { ComponentStyleConfig as AntdComponentStyleConfig } from 'antd/es/config-provider/context';

import type { AnyObject } from '../_util/type';
import type { BubbleProps } from '../bubble';
import type { ConversationsProps } from '../conversations';
Expand All @@ -10,26 +8,19 @@ import type { SenderProps } from '../sender';
import type { SuggestionProps } from '../suggestion';
import type { ThoughtChainProps } from '../thought-chain';

export interface DefaultXComponentConfig {
export interface XComponentStyleConfig {
classNames: Record<string, string>;
styles: Record<string, React.CSSProperties>;
className: string;
style: React.CSSProperties;
}

export const defaultXComponentConfig: DefaultXComponentConfig = {
classNames: {},
styles: {},
className: '',
style: {},
};

type DefaultPickType = keyof DefaultXComponentConfig;
type DefaultPickType = keyof XComponentStyleConfig;

type ComponentStyleConfig<
CompProps extends AnyObject,
PickType extends keyof CompProps = DefaultPickType,
> = AntdComponentStyleConfig & Pick<CompProps, PickType | DefaultPickType>;
> = Pick<CompProps, PickType | DefaultPickType>;

export interface XComponentsConfig {
bubble?: ComponentStyleConfig<BubbleProps>;
Expand Down

0 comments on commit 1d5153b

Please sign in to comment.