[improvement] rename packages dir to src (#3659)

This commit is contained in:
neverland
2019-06-27 11:25:57 +08:00
committed by GitHub
parent 8489918dca
commit 75c79b7044
619 changed files with 21 additions and 21 deletions
+49
View File
@@ -0,0 +1,49 @@
/**
* 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[];
const ELEMENT = '__';
const MODS = '--';
function join(name: string, el?: string, symbol?: string): string {
return el ? name + symbol + el : name;
}
function prefix(name: string, mods: Mods): Mods {
if (typeof mods === 'string') {
return join(name, mods, MODS);
}
if (Array.isArray(mods)) {
return mods.map(item => <Mod>prefix(name, item));
}
const ret: Mods = {};
if (mods) {
Object.keys(mods).forEach(key => {
ret[name + MODS + key] = mods[key];
});
}
return ret;
}
export function createBEM(name: string) {
return function (el?: Mods, mods?: Mods): Mods {
if (el && typeof el !== 'string') {
mods = el;
el = '';
}
el = join(name, el, ELEMENT);
return mods ? [el, prefix(el, mods)] : el;
};
}
+101
View File
@@ -0,0 +1,101 @@
/**
* Create a basic component with common options
*/
import '../../locale';
import { camelize } from '../format/string';
import { SlotsMixin } from '../../mixins/slots';
import Vue, { VNode, VueConstructor, ComponentOptions, RenderContext } from 'vue';
import { DefaultProps, FunctionComponent } from '../types';
export interface VantComponentOptions extends ComponentOptions<Vue> {
functional?: boolean;
install?: (Vue: VueConstructor) => void;
}
export type TsxBaseProps<Slots> = {
key: string | number;
// hack for jsx prop spread
props: any;
class: any;
style: string | object[] | object;
scopedSlots: Slots;
};
export type TsxComponent<Props, Events, Slots> = (
props: Partial<Props & Events & TsxBaseProps<Slots>>
) => VNode;
const arrayProp = {
type: Array,
default: () => []
};
const numberProp = {
type: Number,
default: 0
};
function defaultProps(props: any) {
Object.keys(props).forEach(key => {
if (props[key] === Array) {
props[key] = arrayProp;
} else if (props[key] === Number) {
props[key] = numberProp;
}
});
}
function install(this: ComponentOptions<Vue>, Vue: VueConstructor) {
const { name } = this;
Vue.component(name as string, this);
Vue.component(camelize(`-${name}`), this);
}
// unify slots & scopedSlots
export function unifySlots(context: RenderContext) {
// use data.scopedSlots in lower Vue version
const scopedSlots = context.scopedSlots || context.data.scopedSlots || {};
const slots = context.slots();
Object.keys(slots).forEach(key => {
if (!scopedSlots[key]) {
scopedSlots[key] = () => slots[key];
}
});
return scopedSlots;
}
// should be removed after Vue 3
function transformFunctionComponent(pure: FunctionComponent): VantComponentOptions {
return {
functional: true,
props: pure.props,
model: pure.model,
render: (h, context): any => pure(h, context.props, unifySlots(context), context)
};
}
export function createComponent(name: string) {
return function<Props = DefaultProps, Events = {}, Slots = {}> (
sfc: VantComponentOptions | FunctionComponent
): TsxComponent<Props, Events, Slots> {
if (typeof sfc === 'function') {
sfc = transformFunctionComponent(sfc);
}
if (!sfc.functional) {
sfc.mixins = sfc.mixins || [];
sfc.mixins.push(SlotsMixin);
}
if (sfc.props) {
defaultProps(sfc.props);
}
sfc.name = name;
sfc.install = install;
return sfc as TsxComponent<Props, Events, Slots>;
};
}
+12
View File
@@ -0,0 +1,12 @@
import { get } from '..';
import { camelize } from '../format/string';
import locale from '../../locale';
export function createI18N(name: string) {
const prefix = camelize(name) + '.';
return function (path: string, ...args: any[]): string {
const message = get(locale.messages(), prefix + path) || get(locale.messages(), path);
return typeof message === 'function' ? message(...args) : message;
};
}
+14
View File
@@ -0,0 +1,14 @@
import { createBEM } from './bem';
import { createComponent } from './component';
import { createI18N } from './i18n';
type CreateNamespaceReturn = [
ReturnType<typeof createComponent>,
ReturnType<typeof createBEM>,
ReturnType<typeof createI18N>
];
export function createNamespace(name: string): CreateNamespaceReturn {
name = 'van-' + name;
return [createComponent(name), createBEM(name), createI18N(name)];
}