Skip to content

Commit

Permalink
refactor: add config for getResetStyles (#29)
Browse files Browse the repository at this point in the history
* refactor: add config for getResetStyles

* test: add test case

* refactor: optimizing code logic

* chore: add comments to style reset logic
  • Loading branch information
YumoImer authored Dec 5, 2024
1 parent 8dbe50f commit 831eb5d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/util/genStyleUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ export type CSSVarRegisterProps = {
};
};

export type GetResetStyles<AliasToken extends TokenType> = (token: AliasToken) => CSSInterpolation;
type GetResetStylesConfig = {
prefix: ReturnType<UsePrefix>;
csp: ReturnType<UseCSP>
};

export type GetResetStyles<AliasToken extends TokenType> = (token: AliasToken, config?: GetResetStylesConfig) => CSSInterpolation;

export type GetCompUnitless<CompTokenMap extends TokenMap, AliasToken extends TokenType> = <
C extends TokenMapKey<CompTokenMap>,
Expand Down Expand Up @@ -345,11 +350,14 @@ function genStyleUtils<
order: options.order || -999,
};

// Generate style for all need reset tags.
useStyleRegister(
{ ...sharedConfig, clientOnly: false, path: ['Shared', rootPrefixCls] },
() => (typeof getResetStyles === 'function' ? getResetStyles(token) : []),
);
// This if statement is safe, as it will only be used if the generator has the function. It's not dynamic.
if (typeof getResetStyles === 'function') {
// Generate style for all need reset tags.
useStyleRegister(
{ ...sharedConfig, clientOnly: false, path: ['Shared', rootPrefixCls] },
() => getResetStyles(token, { prefix: { rootPrefixCls, iconPrefixCls }, csp }),
);
}

const wrapSSR = useStyleRegister(
{ ...sharedConfig, path: [concatComponent, prefixCls, iconPrefixCls] },
Expand Down
21 changes: 21 additions & 0 deletions tests/genStyleUtils.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,27 @@ describe('genStyleUtils', () => {
});
});

describe('genComponentStyleHook should run without getResetStyles', () => {
it('should generate component style hook', () => {
const component = 'TestComponent';
const styleFn = jest.fn();
const getDefaultToken = jest.fn();
const { getResetStyles, ...restMockConfig } = mockConfig;
const { genComponentStyleHook: genComponentStyleHookWithoutReset } = genStyleUtils<
TestCompTokenMap,
object,
object
>(restMockConfig);
const hook = genComponentStyleHookWithoutReset(component, styleFn, getDefaultToken);
const TestComponent: React.FC<SubStyleComponentProps> = ({ prefixCls, rootCls }) => {
hook(prefixCls, rootCls);
return <div data-testid="test-component">Test</div>;
};
const { container } = render(<TestComponent prefixCls="test-prefix" rootCls="test-root" />);
expect(() => container).not.toThrow();
});
});

describe('CSSVarRegister', () => {
it('should render CSSVarRegister component', () => {
const CSSVarRegister: React.FC<CSSVarRegisterProps> = ({ rootCls, cssVar = {} }) => {
Expand Down

0 comments on commit 831eb5d

Please sign in to comment.