Skip to content

Commit

Permalink
refactor(context): refactor context (#175)
Browse files Browse the repository at this point in the history
* refactor(context): refactor context

* fix bug

* fix test
  • Loading branch information
zzt1224 authored Nov 8, 2021
1 parent 38de9ff commit 5a08e58
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 90 deletions.
30 changes: 5 additions & 25 deletions src/design-context/DesignContext.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import React from 'react';
import type { DesignContextProps, DesignProviderProps, SizeType } from './interfaces';
import type { DesignContextProps } from './interfaces';

const defaultRootPrefixCls = 'gio';
const DefaultPrefixCls = 'gio';

export const getDesignPrefixCls = (subPrefixCls: string, customRootPrefixCls: string = defaultRootPrefixCls): string =>
[customRootPrefixCls, subPrefixCls].join('-');

const defaultProps = {
getPrefixCls: getDesignPrefixCls,
rootPrefixCls: defaultRootPrefixCls,
size: 'middle' as SizeType,
export const DefaultContextProps = {
prefixCls: DefaultPrefixCls,
locale: { code: 'zh-CN' },
theme: {},
};
const DesignContext = React.createContext<DesignContextProps>(defaultProps);

export const DesignConsumer = DesignContext.Consumer;

export const DesignProvider = ({
getPrefixCls = getDesignPrefixCls,
rootPrefixCls = defaultRootPrefixCls,
size = 'middle' as SizeType,
locale = { code: 'zh-CN' },
theme = {},
children,
}: DesignProviderProps) => (
<DesignContext.Provider value={{ getPrefixCls, rootPrefixCls, size, locale, theme }}>
{children}
</DesignContext.Provider>
);
const DesignContext = React.createContext<DesignContextProps>(DefaultContextProps);

export default DesignContext;
23 changes: 7 additions & 16 deletions src/design-context/__test__/DesignContext.test.tsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,36 @@
import React from 'react';
import { renderHook } from '@testing-library/react-hooks';
import DesignContext, { DesignProvider, getDesignPrefixCls } from '../DesignContext';
import { DesignContextProps, SizeType } from '../interfaces';
import DesignContext, { DefaultContextProps } from '../DesignContext';
import { DesignContextProps } from '../interfaces';

function useDesignContext() {
return React.useContext(DesignContext);
}

const wrapper = ({ children, props }: { children?: React.ReactNode; props: Partial<DesignContextProps> }) => (
<DesignProvider {...props}>{children}</DesignProvider>
<DesignContext.Provider value={{ ...DefaultContextProps, ...props }}>{children}</DesignContext.Provider>
);

describe('DesignContext', () => {
const defaultLocale = { code: 'zh-CN' };
const defaultProps = { getPrefixCls: getDesignPrefixCls };

it('has default getPrefixCls function', () => {
expect(getDesignPrefixCls('design')).toBe('gio-design');
expect(getDesignPrefixCls('design', 'growing')).toBe('growing-design');
});

it('has default props', () => {
const { result } = renderHook(() => useDesignContext());
expect(result.current?.rootPrefixCls).toEqual('gio');
expect(result.current?.getPrefixCls).toBe(getDesignPrefixCls);
expect(result.current?.prefixCls).toEqual('gio');
expect(result.current?.locale).toEqual(defaultLocale);
});

it('can change props', () => {
const currentProps = { ...defaultProps, size: 'middle' as SizeType };
const { result, rerender } = renderHook(() => useDesignContext(), {
wrapper,
initialProps: { props: {} },
});
expect(result.current.size).toEqual(currentProps.size);

expect(result.current.locale).toEqual(defaultLocale);

const newProps = { ...defaultProps, rootPrefixCls: 'custom', size: 'small' as SizeType, locale: { code: 'zh' } };
const newProps = { prefixCls: 'custom', locale: { code: 'zh' } };
rerender({ props: newProps });
expect(result.current.rootPrefixCls).toEqual(newProps.rootPrefixCls);
expect(result.current.size).toEqual(newProps.size);
expect(result.current.prefixCls).toEqual(newProps.prefixCls);
expect(result.current.locale).toEqual(newProps.locale);
});
});
8 changes: 2 additions & 6 deletions src/design-context/demos/DesignContext.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import React from 'react';
import { DesignContextProps, IntlShape } from '../interfaces';
import { DesignContextProps } from '../interfaces';

export function DesignContextPropsComponent(_: DesignContextProps) {
return <></>;
}

export function IntlShapeComponent(_: IntlShape) {
export default function DesignContextPropsComponent(_: DesignContextProps) {
return <></>;
}
4 changes: 2 additions & 2 deletions src/design-context/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as DesignContext, DesignConsumer, DesignProvider, getDesignPrefixCls } from './DesignContext';
export type { DesignContextProps, DesignProviderProps, Locale, SizeType } from './interfaces';
export { default as DesignContext, DefaultContextProps } from './DesignContext';
export type { DesignContextProps, Locale } from './interfaces';
19 changes: 6 additions & 13 deletions src/design-context/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import React from 'react';

export type SizeType = 'small' | 'middle' | 'large' | undefined;
export interface Locale {
code: string;
[key: string]: any;
}
export interface DesignContextProps {
getPrefixCls?: (subPrefixCls: string, customRootPrefixCls?: string) => string;
locale?: Locale;
rootPrefixCls?: string;
size?: SizeType;
theme?: Record<string, any>;
[key: string]: any;
}

export interface DesignProviderProps extends Partial<DesignContextProps> {
children: React.ReactNode;
interface DesignContextInteral {
locale: Locale;
prefixCls: string;
theme: Record<string, any>;
}

export type DesignContextProps<T = Record<string, any>> = DesignContextInteral & T;
9 changes: 0 additions & 9 deletions src/hooks/__test__/useSize.test.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ export { default as useControlledState } from './useControlledState';
export { default as useLocale } from './useLocale';
export { default as useLocalStorage } from './useLocalStorage';
export { default as usePrefixCls } from './usePrefixCls';
export { default as useSize } from './useSize';
6 changes: 3 additions & 3 deletions src/hooks/useLocale.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useContext } from 'react';
import { get } from 'lodash';
import { DesignContext } from '../design-context';
import { DesignContext, Locale } from '../design-context';

const useLocale = (componentName: string) => {
const useLocale = <T = Record<string, any>>(componentName: string) => {
const { locale } = useContext(DesignContext);
return get(locale, componentName);
return get(locale, componentName) as Locale & T;
};

export default useLocale;
8 changes: 5 additions & 3 deletions src/hooks/usePrefixCls.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { useContext } from 'react';
import { DesignContext } from '../design-context';

const usePrefixCls = (subPrefixCls: string, customRootPrefixCls?: string) => {
const { rootPrefixCls, getPrefixCls } = useContext(DesignContext);
return getPrefixCls?.(subPrefixCls, customRootPrefixCls ?? rootPrefixCls);
const getPrefixCls = (prefix: string, rootPrefix: string) => [prefix, rootPrefix].join('-');

const usePrefixCls = (prefixCls: string, customRootPrefixCls?: string) => {
const { prefixCls: rootPrefixCls } = useContext(DesignContext);
return getPrefixCls(customRootPrefixCls ?? rootPrefixCls, prefixCls);
};

export default usePrefixCls;
9 changes: 0 additions & 9 deletions src/hooks/useSize.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable import/prefer-default-export */
export { DesignContext, DesignProvider, DesignConsumer } from './design-context';
export type { DesignContextProps, DesignProviderProps, Locale, SizeType } from './design-context';
export { DesignContext, DefaultContextProps } from './design-context';
export type { DesignContextProps, Locale } from './design-context';

export { injectPinyinWith } from './pinyin';

export { formatTimeRange, humanizeTimeRange } from './format/formatTimeRange';

export { useControlledState, useLocale, useLocalStorage, usePrefixCls, useSize } from './hooks';
export { useControlledState, useLocale, useLocalStorage, usePrefixCls } from './hooks';

export type { BaseProps, CommonProps, OverridableComponent } from './interfaces';

1 comment on commit 5a08e58

@vercel
Copy link

@vercel vercel bot commented on 5a08e58 Nov 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.