Skip to content

Commit

Permalink
refactor: split the genStyleUtils and make the getComponentToken
Browse files Browse the repository at this point in the history
…`getCompVarPrefix` `getDefaultComponentToken` independent
  • Loading branch information
YumoImer committed Jul 9, 2024
1 parent 100d152 commit 4a4beeb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/util/getCompVarPrefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const getCompVarPrefix = (component: string, prefix?: string) =>
`${[
prefix,
component.replace(/([A-Z]+)([A-Z][a-z]+)/g, '$1-$2').replace(/([a-z])([A-Z])/g, '$1-$2'),

Check failure

Code scanning / CodeQL

Polynomial regular expression used on uncontrolled data High

This
regular expression
that depends on
library input
may run slow on strings with many repetitions of 'A'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of 'A'.
This
regular expression
that depends on
library input
may run slow on strings with many repetitions of 'A'.
]
.filter(Boolean)
.join('-')}`;

export default getCompVarPrefix;
42 changes: 42 additions & 0 deletions src/util/getComponentToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { GlobalToken } from '../interface';
import { warning } from 'rc-util';
import type { OverrideComponent, ComponentTokenKey, ComponentToken } from './genStyleUtils';

export default function getComponentToken<CompTokenMap, C extends OverrideComponent<CompTokenMap>>(
component: C,
token: GlobalToken<CompTokenMap>,
defaultToken: CompTokenMap[C],
options?: {
deprecatedTokens?: [ComponentTokenKey<CompTokenMap, C>, ComponentTokenKey<CompTokenMap, C>][];
},
) {
const customToken = { ...(token[component] as ComponentToken<CompTokenMap, C>) };
if (options?.deprecatedTokens) {
const { deprecatedTokens } = options;
deprecatedTokens.forEach(([oldTokenKey, newTokenKey]) => {
if (process.env.NODE_ENV !== 'production') {
warning(
!customToken?.[oldTokenKey],
`Component Token \`${String(
oldTokenKey,
)}\` of ${String(component)} is deprecated. Please use \`${String(newTokenKey)}\` instead.`,
);
}

// Should wrap with `if` clause, or there will be `undefined` in object.
if (customToken?.[oldTokenKey] || customToken?.[newTokenKey]) {
customToken[newTokenKey] ??= customToken?.[oldTokenKey];
}
});
}
const mergedToken: any = { ...defaultToken, ...customToken };

// Remove same value as global token to minimize size
Object.keys(mergedToken).forEach((key) => {
if (mergedToken[key] === token[key as keyof GlobalToken<CompTokenMap>]) {
delete mergedToken[key];
}
});

return mergedToken;
};
14 changes: 14 additions & 0 deletions src/util/getDefaultComponentToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { merge as mergeToken } from './statistic';
import type { GlobalToken } from '../interface';
import type { OverrideComponent, GetDefaultToken } from './genStyleUtils';

export default function getDefaultComponentToken<CompTokenMap, C extends OverrideComponent<CompTokenMap>>(
component: C,
token: GlobalToken<CompTokenMap>,
getDefaultToken: GetDefaultToken<CompTokenMap, C>,
) {
if (typeof getDefaultToken === 'function') {
return (getDefaultToken as Function)(mergeToken(token, token[component] ?? {}));
}
return getDefaultToken ?? {};
};

0 comments on commit 4a4beeb

Please sign in to comment.