Skip to content

Commit 9fdc1d9

Browse files
committed
feat: extend the ClassNames interface
1 parent 86fb1e5 commit 9fdc1d9

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/common/className.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,29 @@ export function prefixClaName(name: string, prefix: string = APP_PREFIX) {
1010
return name ? `${prefix}-${name}` : '';
1111
}
1212

13-
export function classNames(...names) {
14-
return names.filter((name) => !!name).join(' ');
13+
export function classNames(...args) {
14+
let classList: string[] = [];
15+
for (let arg of args) {
16+
if (!arg) continue;
17+
let argType = typeof arg;
18+
if (argType === 'string' || argType === 'number') {
19+
classList.push(arg);
20+
continue
21+
}
22+
if (argType === 'object') {
23+
if (arg.toString !== Object.prototype.toString) {
24+
classList.push(arg.toString());
25+
continue
26+
}
27+
for (let key in arg) {
28+
if (Object.hasOwnProperty.call(arg, key) && arg[key]) {
29+
classList.push(key);
30+
}
31+
}
32+
}
33+
}
34+
return classList.join(' ');
1535
}
16-
1736
/**
1837
* Element names may consist of Latin letters, digits, dashes and underscores.
1938
* CSS class is formed as block name plus two underscores plus element name: .block__elem

src/components/tabs/tabButton.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default function TabButton({
4444
<div
4545
className={classNames(
4646
'tab-button',
47-
active ? 'tab-button--active' : '',
47+
{ 'tab-button--active': true },
4848
className
4949
)}
5050
onClick={handleClick}

0 commit comments

Comments
 (0)