generated from react-component/trigger
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split the
genStyleUtils
and make the getComponentToken
…
…`getCompVarPrefix` `getDefaultComponentToken` independent
- Loading branch information
Showing
3 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 Error loading related location Loading library input Error loading related location Loading This regular expression Error loading related location Loading library input Error loading related location Loading This regular expression Error loading related location Loading library input Error loading related location Loading |
||
] | ||
.filter(Boolean) | ||
.join('-')}`; | ||
|
||
export default getCompVarPrefix; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?? {}; | ||
}; |