mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
refactor: better types for cx
This commit is contained in:
parent
45828116fe
commit
e7ad0917e9
1 changed files with 23 additions and 13 deletions
|
|
@ -1,18 +1,28 @@
|
|||
type CXValue = string | boolean | null | undefined
|
||||
|
||||
/**
|
||||
* cx('class1', { class2: true, class3: false }) --> 'class1 class2'
|
||||
*/
|
||||
export function cx(...classNames: any[]): string {
|
||||
return classNames
|
||||
.filter(Boolean)
|
||||
.map(className => {
|
||||
switch (typeof className) {
|
||||
case 'string':
|
||||
return className
|
||||
case 'object':
|
||||
return cx(...Object.entries(className).map(([key, value]) => (value ? key : null)))
|
||||
default:
|
||||
return ''
|
||||
export function cx(
|
||||
...classNames: (
|
||||
| CXValue
|
||||
| {
|
||||
[className: string]: CXValue
|
||||
}
|
||||
})
|
||||
.join(' ')
|
||||
)[]
|
||||
): string {
|
||||
return classNames.map(handleClassName).filter(Boolean).join(' ')
|
||||
}
|
||||
|
||||
function handleClassName(className: Parameters<typeof cx>[number]): string | false {
|
||||
switch (typeof className) {
|
||||
case 'string':
|
||||
return className
|
||||
case 'object':
|
||||
return className === null
|
||||
? false
|
||||
: cx(...Object.entries(className).map(([key, value]) => (value ? key : false)))
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue