[type] classNames (#622)

This commit is contained in:
neverland 2018-09-21 14:52:47 +08:00 committed by GitHub
parent f98d8eb613
commit 99360b0ef6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 62 additions and 36 deletions

View File

@ -1,29 +1,28 @@
const hasOwn = {}.hasOwnProperty; const hasOwn = {}.hasOwnProperty;
export function classNames() { export function classNames() {
const classes = []; const classes = [];
for (let i = 0; i < arguments.length; i++) {
for (let i = 0; i < arguments.length; i++) { const arg = arguments[i];
const arg = arguments[i]; if (!arg)
if (!arg) continue; continue;
const argType = typeof arg;
const argType = typeof arg; if (argType === 'string' || argType === 'number') {
classes.push(arg);
if (argType === 'string' || argType === 'number') { }
classes.push(arg); else if (Array.isArray(arg) && arg.length) {
} else if (Array.isArray(arg) && arg.length) { const inner = classNames.apply(null, arg);
const inner = classNames.apply(null, arg); if (inner) {
if (inner) { classes.push(inner);
classes.push(inner); }
} }
} else if (argType === 'object') { else if (argType === 'object') {
for (const key in arg) { for (const key in arg) {
if (hasOwn.call(arg, key) && arg[key]) { if (hasOwn.call(arg, key) && arg[key]) {
classes.push(key); classes.push(key);
}
}
} }
}
} }
} return classes.join(' ');
}
return classes.join(' '); ;
};

View File

@ -21,7 +21,7 @@ VantComponent({
}, },
computed: { computed: {
classes() { classes(): string {
const { type, size, plain, disabled, loading, square, block } = this.data; const { type, size, plain, disabled, loading, square, block } = this.data;
return this.classNames(`van-button--${type}`, `van-button--${size}`, { return this.classNames(`van-button--${type}`, `van-button--${size}`, {
'van-button--block': block, 'van-button--block': block,

View File

@ -1,6 +1,6 @@
const hasOwn = {}.hasOwnProperty; const hasOwn = {}.hasOwnProperty;
export function classNames() { export function classNames(): string {
const classes = []; const classes = [];
for (let i = 0; i < arguments.length; i++) { for (let i = 0; i < arguments.length; i++) {

16
types/class-names.d.ts vendored Normal file
View File

@ -0,0 +1,16 @@
type ClassValue =
| string
| number
| ClassDictionary
| ClassArray
| undefined
| null
| boolean;
interface ClassDictionary {
[id: string]: any;
}
interface ClassArray extends Array<ClassValue> {}
export type classNames = (...classes: ClassValue[]) => string;

20
types/index.d.ts vendored
View File

@ -1,5 +1,5 @@
/// <reference path="./weapp.d.ts" /> /// <reference path="./weapp.d.ts" />
import { Vue } from './vue'; import { ComponentInstance } from './instance';
type Mixins = any[]; type Mixins = any[];
type ExternalClasses = string[]; type ExternalClasses = string[];
@ -14,12 +14,20 @@ type Relations<Instance> = {
}; };
}; };
type RecordToAny<T> = { [K in keyof T]: any }; type RecordToAny<T> = { [K in keyof T]: any };
type Accessors<T> = {
[K in keyof T]: (() => T[K])
}
export type CombinedComponentInstance<Data, Props, Methods, Computed> = Vue & export type CombinedComponentInstance<
Methods & Data,
Props,
Methods,
Computed
> = Methods &
LooseObject & LooseObject &
Weapp.Component & { Weapp.Component &
data: Data & RecordToAny<Props> & RecordToAny<Computed>; ComponentInstance & {
data: Data & RecordToAny<Props> & Computed;
}; };
export type VantComponentOptions<Data, Props, Methods, Computed, Instance> = { export type VantComponentOptions<Data, Props, Methods, Computed, Instance> = {
@ -27,7 +35,7 @@ export type VantComponentOptions<Data, Props, Methods, Computed, Instance> = {
props?: Props; props?: Props;
field?: boolean; field?: boolean;
mixins?: Mixins; mixins?: Mixins;
computed?: Computed & ThisType<Instance>; computed?: Accessors<Computed> & ThisType<Instance>;
relations?: Relations<Instance>; relations?: Relations<Instance>;
classes?: ExternalClasses; classes?: ExternalClasses;
methods?: Methods & ThisType<Instance>; methods?: Methods & ThisType<Instance>;

6
types/instance.ts Normal file
View File

@ -0,0 +1,6 @@
import { classNames } from './class-names';
export interface ComponentInstance {
$emit(name: string, detail?: any): void;
classNames: classNames;
}

3
types/vue.d.ts vendored
View File

@ -1,3 +0,0 @@
export interface Vue {
$emit(name: string, detail?: any): void;
}