ant-design-vue/components/_util/classNames.ts

46 lines
926 B
TypeScript
Raw Normal View History

2020-08-31 16:53:19 +08:00
import { isArray, isString, isObject } from './util';
2020-09-16 12:45:04 +08:00
export type ClassArray = ClassValue[];
export interface ClassDictionary {
[id: string]: any;
}
export type ClassValue =
| string
| number
| ClassDictionary
| ClassArray
| undefined
| null
| boolean;
function classNames(...args: ClassValue[]): string {
2020-10-01 20:57:03 +08:00
const classes: string[] = [];
2020-09-16 12:45:04 +08:00
for (let i = 0; i < args.length; i++) {
const value = args[i];
if (!value) {
continue;
}
2020-08-31 17:16:16 +08:00
if (isString(value)) {
classes.push(value);
} else if (isArray(value)) {
for (let i = 0; i < value.length; i++) {
const inner = classNames(value[i]);
if (inner) {
classes.push(inner);
}
}
} else if (isObject(value)) {
for (const name in value) {
if (value[name]) {
classes.push(name);
}
2020-08-31 16:53:19 +08:00
}
}
}
2020-08-31 17:16:16 +08:00
return classes.join(' ');
2020-08-31 16:53:19 +08:00
}
export default classNames;