Skip to content

Commit

Permalink
feat: extend the ClassNames interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mumiao committed Dec 3, 2020
1 parent 86fb1e5 commit 9fdc1d9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions src/common/className.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,29 @@ export function prefixClaName(name: string, prefix: string = APP_PREFIX) {
return name ? `${prefix}-${name}` : '';
}

export function classNames(...names) {
return names.filter((name) => !!name).join(' ');
export function classNames(...args) {
let classList: string[] = [];
for (let arg of args) {
if (!arg) continue;
let argType = typeof arg;
if (argType === 'string' || argType === 'number') {
classList.push(arg);
continue
}
if (argType === 'object') {
if (arg.toString !== Object.prototype.toString) {
classList.push(arg.toString());
continue
}
for (let key in arg) {
if (Object.hasOwnProperty.call(arg, key) && arg[key]) {
classList.push(key);
}
}
}
}
return classList.join(' ');
}

/**
* Element names may consist of Latin letters, digits, dashes and underscores.
* CSS class is formed as block name plus two underscores plus element name: .block__elem
Expand Down
2 changes: 1 addition & 1 deletion src/components/tabs/tabButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function TabButton({
<div
className={classNames(
'tab-button',
active ? 'tab-button--active' : '',
{ 'tab-button--active': true },
className
)}
onClick={handleClick}
Expand Down

0 comments on commit 9fdc1d9

Please sign in to comment.