mirror of
https://github.com/youzan/vant.git
synced 2025-10-22 11:54:02 +00:00
[build] add typescript compiler (#2697)
This commit is contained in:
42
packages/utils/use/bem.ts
Normal file
42
packages/utils/use/bem.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* 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'
|
||||
*/
|
||||
|
||||
const ELEMENT = '__';
|
||||
const MODS = '--';
|
||||
|
||||
const join = (name: string, el: string, symbol: string) => (el ? name + symbol + el : name);
|
||||
|
||||
const prefix = (name: string, mods: any): any => {
|
||||
if (typeof mods === 'string') {
|
||||
return join(name, mods, MODS);
|
||||
}
|
||||
|
||||
if (Array.isArray(mods)) {
|
||||
return mods.map(item => prefix(name, item));
|
||||
}
|
||||
|
||||
const ret: { [key: string]: any } = {};
|
||||
if (mods) {
|
||||
Object.keys(mods).forEach(key => {
|
||||
ret[name + MODS + key] = mods[key];
|
||||
});
|
||||
}
|
||||
|
||||
return ret;
|
||||
};
|
||||
|
||||
export default (name: string) => (el: any, mods?: any) => {
|
||||
if (el && typeof el !== 'string') {
|
||||
mods = el;
|
||||
el = '';
|
||||
}
|
||||
el = join(name, el, ELEMENT);
|
||||
|
||||
return mods ? [el, prefix(el, mods)] : el;
|
||||
};
|
Reference in New Issue
Block a user