mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
chore: move createNamespace
This commit is contained in:
parent
eeaeace1c0
commit
79e7b4be65
45
src-next/utils/create/bem.ts
Normal file
45
src-next/utils/create/bem.ts
Normal file
@ -0,0 +1,45 @@
|
||||
/**
|
||||
* bem helper
|
||||
* b() // 'button'
|
||||
* b('text') // 'button__text'
|
||||
* b({ disabled }) // 'button button--disabled'
|
||||
* b('text', { disabled }) // 'button__text button__text--disabled'
|
||||
* b(['disabled', 'primary']) // 'button button--disabled button--primary'
|
||||
*/
|
||||
|
||||
export type Mod = string | { [key: string]: any };
|
||||
export type Mods = Mod | Mod[];
|
||||
|
||||
function gen(name: string, mods?: Mods): string {
|
||||
if (!mods) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (typeof mods === 'string') {
|
||||
return ` ${name}--${mods}`;
|
||||
}
|
||||
|
||||
if (Array.isArray(mods)) {
|
||||
return mods.reduce<string>((ret, item) => ret + gen(name, item), '');
|
||||
}
|
||||
|
||||
return Object.keys(mods).reduce(
|
||||
(ret, key) => ret + (mods[key] ? gen(name, key) : ''),
|
||||
''
|
||||
);
|
||||
}
|
||||
|
||||
export function createBEM(name: string) {
|
||||
return function (el?: Mods, mods?: Mods): Mods {
|
||||
if (el && typeof el !== 'string') {
|
||||
mods = el;
|
||||
el = '';
|
||||
}
|
||||
|
||||
el = el ? `${name}__${el}` : name;
|
||||
|
||||
return `${el}${gen(el, mods)}`;
|
||||
};
|
||||
}
|
||||
|
||||
export type BEM = ReturnType<typeof createBEM>;
|
18
src-next/utils/create/component.ts
Normal file
18
src-next/utils/create/component.ts
Normal file
@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Create a basic component with common options
|
||||
*/
|
||||
|
||||
// function install(this: any, Vue: VueConstructor) {
|
||||
// const { name } = this;
|
||||
// Vue.component(name as string, this);
|
||||
// Vue.component(camelize(`-${name}`), this);
|
||||
// }
|
||||
|
||||
export function createComponent(name: string) {
|
||||
return function (sfc: any) {
|
||||
sfc.name = name;
|
||||
// sfc.install = install;
|
||||
|
||||
return sfc;
|
||||
};
|
||||
}
|
18
src-next/utils/create/index.ts
Normal file
18
src-next/utils/create/index.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { createBEM, BEM } from './bem';
|
||||
import { createComponent } from './component';
|
||||
// import { createI18N, Translate } from './i18n';
|
||||
|
||||
type CreateNamespaceReturn = [
|
||||
ReturnType<typeof createComponent>,
|
||||
BEM,
|
||||
// Translate
|
||||
];
|
||||
|
||||
export function createNamespace(name: string): CreateNamespaceReturn {
|
||||
name = 'van-' + name;
|
||||
return [
|
||||
createComponent(name),
|
||||
createBEM(name),
|
||||
// createI18N(name)
|
||||
];
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user