Skip to content

Commit 831eb5d

Browse files
authored
refactor: add config for getResetStyles (#29)
* refactor: add config for getResetStyles * test: add test case * refactor: optimizing code logic * chore: add comments to style reset logic
1 parent 8dbe50f commit 831eb5d

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

src/util/genStyleUtils.tsx

+14-6
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ export type CSSVarRegisterProps = {
9191
};
9292
};
9393

94-
export type GetResetStyles<AliasToken extends TokenType> = (token: AliasToken) => CSSInterpolation;
94+
type GetResetStylesConfig = {
95+
prefix: ReturnType<UsePrefix>;
96+
csp: ReturnType<UseCSP>
97+
};
98+
99+
export type GetResetStyles<AliasToken extends TokenType> = (token: AliasToken, config?: GetResetStylesConfig) => CSSInterpolation;
95100

96101
export type GetCompUnitless<CompTokenMap extends TokenMap, AliasToken extends TokenType> = <
97102
C extends TokenMapKey<CompTokenMap>,
@@ -345,11 +350,14 @@ function genStyleUtils<
345350
order: options.order || -999,
346351
};
347352

348-
// Generate style for all need reset tags.
349-
useStyleRegister(
350-
{ ...sharedConfig, clientOnly: false, path: ['Shared', rootPrefixCls] },
351-
() => (typeof getResetStyles === 'function' ? getResetStyles(token) : []),
352-
);
353+
// This if statement is safe, as it will only be used if the generator has the function. It's not dynamic.
354+
if (typeof getResetStyles === 'function') {
355+
// Generate style for all need reset tags.
356+
useStyleRegister(
357+
{ ...sharedConfig, clientOnly: false, path: ['Shared', rootPrefixCls] },
358+
() => getResetStyles(token, { prefix: { rootPrefixCls, iconPrefixCls }, csp }),
359+
);
360+
}
353361

354362
const wrapSSR = useStyleRegister(
355363
{ ...sharedConfig, path: [concatComponent, prefixCls, iconPrefixCls] },

tests/genStyleUtils.test.tsx

+21
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,27 @@ describe('genStyleUtils', () => {
8888
});
8989
});
9090

91+
describe('genComponentStyleHook should run without getResetStyles', () => {
92+
it('should generate component style hook', () => {
93+
const component = 'TestComponent';
94+
const styleFn = jest.fn();
95+
const getDefaultToken = jest.fn();
96+
const { getResetStyles, ...restMockConfig } = mockConfig;
97+
const { genComponentStyleHook: genComponentStyleHookWithoutReset } = genStyleUtils<
98+
TestCompTokenMap,
99+
object,
100+
object
101+
>(restMockConfig);
102+
const hook = genComponentStyleHookWithoutReset(component, styleFn, getDefaultToken);
103+
const TestComponent: React.FC<SubStyleComponentProps> = ({ prefixCls, rootCls }) => {
104+
hook(prefixCls, rootCls);
105+
return <div data-testid="test-component">Test</div>;
106+
};
107+
const { container } = render(<TestComponent prefixCls="test-prefix" rootCls="test-root" />);
108+
expect(() => container).not.toThrow();
109+
});
110+
});
111+
91112
describe('CSSVarRegister', () => {
92113
it('should render CSSVarRegister component', () => {
93114
const CSSVarRegister: React.FC<CSSVarRegisterProps> = ({ rootCls, cssVar = {} }) => {

0 commit comments

Comments
 (0)