chore: prettier all ts code

This commit is contained in:
陈嘉涵 2020-01-19 15:27:28 +08:00
parent 04c49b645a
commit 207e80f396
19 changed files with 124 additions and 68 deletions

View File

@ -14,7 +14,7 @@ export type SharedCellProps = {
value?: string | number; value?: string | number;
label?: string | number; label?: string | number;
arrowDirection?: 'up' | 'down' | 'left' | 'right'; arrowDirection?: 'up' | 'down' | 'left' | 'right';
} };
export const cellProps = { export const cellProps = {
icon: String, icon: String,

View File

@ -20,7 +20,10 @@ export const ClickOutsideMixin = (config: ClickOutsideMixinConfig) =>
data() { data() {
const clickOutsideHandler = (event: Event) => { const clickOutsideHandler = (event: Event) => {
if (this.closeOnClickOutside && !this.$el.contains(event.target as Node)) { if (
this.closeOnClickOutside &&
!this.$el.contains(event.target as Node)
) {
(this as any)[config.method](); (this as any)[config.method]();
} }
}; };

View File

@ -17,7 +17,7 @@ function getElement(selector: string | GetContainer): Element | null {
export function PortalMixin({ ref, afterPortal }: PortalMixinOptions) { export function PortalMixin({ ref, afterPortal }: PortalMixinOptions) {
return Vue.extend({ return Vue.extend({
props: { props: {
getContainer: [String, Function] as (PropType<string | GetContainer>), getContainer: [String, Function] as PropType<string | GetContainer>,
}, },
watch: { watch: {
@ -33,7 +33,7 @@ export function PortalMixin({ ref, afterPortal }: PortalMixinOptions) {
methods: { methods: {
portal() { portal() {
const { getContainer } = this; const { getContainer } = this;
const el = ref ? this.$refs[ref] as HTMLElement : this.$el; const el = ref ? (this.$refs[ref] as HTMLElement) : this.$el;
let container; let container;
if (getContainer) { if (getContainer) {

View File

@ -25,7 +25,10 @@ type ChildrenMixinThis = {
disableBindRelation?: boolean; disableBindRelation?: boolean;
}; };
export function ChildrenMixin(parent: string, options: ChildrenMixinOptions = {}) { export function ChildrenMixin(
parent: string,
options: ChildrenMixinOptions = {}
) {
const indexKey = options.indexKey || 'index'; const indexKey = options.indexKey || 'index';
return Vue.extend({ return Vue.extend({
@ -56,7 +59,9 @@ export function ChildrenMixin(parent: string, options: ChildrenMixinOptions = {}
beforeDestroy() { beforeDestroy() {
if (this.parent) { if (this.parent) {
this.parent.children = this.parent.children.filter((item: any) => item !== this); this.parent.children = this.parent.children.filter(
(item: any) => item !== this
);
} }
}, },
@ -68,7 +73,9 @@ export function ChildrenMixin(parent: string, options: ChildrenMixinOptions = {}
const children = [...this.parent.children, this]; const children = [...this.parent.children, this];
const vnodes = flattenVNodes(this.parent.slots()); const vnodes = flattenVNodes(this.parent.slots());
children.sort((a, b) => vnodes.indexOf(a.$vnode) - vnodes.indexOf(b.$vnode)); children.sort(
(a, b) => vnodes.indexOf(a.$vnode) - vnodes.indexOf(b.$vnode)
);
this.parent.children = children; this.parent.children = children;
}, },

View File

@ -58,7 +58,8 @@ export const TouchMixin = Vue.extend({
// avoid Vue 2.6 event bubble issues by manually binding events // avoid Vue 2.6 event bubble issues by manually binding events
// https://github.com/youzan/vant/issues/3015 // https://github.com/youzan/vant/issues/3015
bindTouchEvent(el: HTMLElement) { bindTouchEvent(el: HTMLElement) {
const { onTouchStart, onTouchMove, onTouchEnd } = (this as any); const { onTouchStart, onTouchMove, onTouchEnd } = this as any;
on(el, 'touchstart', onTouchStart); on(el, 'touchstart', onTouchStart);
on(el, 'touchmove', onTouchMove); on(el, 'touchmove', onTouchMove);

View File

@ -6,7 +6,7 @@ export type SharedPickerProps = {
visibleItemCount: number; visibleItemCount: number;
cancelButtonText?: string; cancelButtonText?: string;
confirmButtonText?: string; confirmButtonText?: string;
} };
export const pickerProps = { export const pickerProps = {
title: String, title: String,

View File

@ -16,14 +16,16 @@ export default {
stock: '剩余', stock: '剩余',
stockUnit: '件', stockUnit: '件',
quotaTip: (quota: number) => `每人限购${quota}`, quotaTip: (quota: number) => `每人限购${quota}`,
quotaUsedTip: (quota: number, count: number) => `每人限购${quota}件,你已购买${count}`, quotaUsedTip: (quota: number, count: number) =>
`每人限购${quota}件,你已购买${count}`,
}, },
vanSkuActions: { vanSkuActions: {
buy: '立即购买', buy: '立即购买',
addCart: '加入购物车', addCart: '加入购物车',
}, },
vanSkuImgUploader: { vanSkuImgUploader: {
oversize: (maxSize: number) => `最大可上传图片为${maxSize}MB请尝试压缩图片尺寸`, oversize: (maxSize: number) =>
`最大可上传图片为${maxSize}MB请尝试压缩图片尺寸`,
fail: '上传失败<br />重新上传', fail: '上传失败<br />重新上传',
}, },
vanSkuStepper: { vanSkuStepper: {

View File

@ -48,7 +48,6 @@ const group3 = [
}, },
]; ];
export const enUSData = [ export const enUSData = [
{ {
text: 'Group 1', text: 'Group 1',

View File

@ -4,7 +4,12 @@
import '../../locale'; import '../../locale';
import { camelize } from '../format/string'; import { camelize } from '../format/string';
import { SlotsMixin } from '../../mixins/slots'; import { SlotsMixin } from '../../mixins/slots';
import Vue, { VNode, VueConstructor, ComponentOptions, RenderContext } from 'vue'; import Vue, {
VNode,
VueConstructor,
ComponentOptions,
RenderContext,
} from 'vue';
import { DefaultProps, FunctionComponent } from '../types'; import { DefaultProps, FunctionComponent } from '../types';
export interface VantComponentOptions extends ComponentOptions<Vue> { export interface VantComponentOptions extends ComponentOptions<Vue> {
@ -47,17 +52,20 @@ export function unifySlots(context: RenderContext) {
} }
// should be removed after Vue 3 // should be removed after Vue 3
function transformFunctionComponent(pure: FunctionComponent): VantComponentOptions { function transformFunctionComponent(
pure: FunctionComponent
): VantComponentOptions {
return { return {
functional: true, functional: true,
props: pure.props, props: pure.props,
model: pure.model, model: pure.model,
render: (h, context): any => pure(h, context.props, unifySlots(context), context), render: (h, context): any =>
pure(h, context.props, unifySlots(context), context),
}; };
} }
export function createComponent(name: string) { export function createComponent(name: string) {
return function<Props = DefaultProps, Events = {}, Slots = {}> ( return function<Props = DefaultProps, Events = {}, Slots = {}>(
sfc: VantComponentOptions | FunctionComponent sfc: VantComponentOptions | FunctionComponent
): TsxComponent<Props, Events, Slots> { ): TsxComponent<Props, Events, Slots> {
if (typeof sfc === 'function') { if (typeof sfc === 'function') {

View File

@ -6,7 +6,9 @@ export function createI18N(name: string) {
const prefix = camelize(name) + '.'; const prefix = camelize(name) + '.';
return function(path: string, ...args: any[]): string { return function(path: string, ...args: any[]): string {
const message = get(locale.messages(), prefix + path) || get(locale.messages(), path); const messages = locale.messages();
const message = get(messages, prefix + path) || get(messages, path);
return typeof message === 'function' ? message(...args) : message; return typeof message === 'function' ? message(...args) : message;
}; };
} }

View File

@ -10,7 +10,11 @@ function assignKey(to: ObjectIndex, from: ObjectIndex, key: string) {
return; return;
} }
if (!hasOwnProperty.call(to, key) || !isObj(val) || typeof val === 'function') { if (
!hasOwnProperty.call(to, key) ||
!isObj(val) ||
typeof val === 'function'
) {
to[key] = val; to[key] = val;
} else { } else {
// eslint-disable-next-line no-use-before-define // eslint-disable-next-line no-use-before-define

View File

@ -34,11 +34,7 @@ export function on(
} }
} }
export function off( export function off(target: EventTarget, event: string, handler: EventHandler) {
target: EventTarget,
event: string,
handler: EventHandler
) {
if (!isServer) { if (!isServer) {
target.removeEventListener(event, handler); target.removeEventListener(event, handler);
} }

View File

@ -4,41 +4,56 @@ type ScrollElement = HTMLElement | Window;
// http://w3help.org/zh-cn/causes/SD9013 // http://w3help.org/zh-cn/causes/SD9013
// http://stackoverflow.com/questions/17016740/onscroll-function-is-not-working-for-chrome // http://stackoverflow.com/questions/17016740/onscroll-function-is-not-working-for-chrome
const overflowScrollReg = /scroll|auto/i; const overflowScrollReg = /scroll|auto/i;
export function getScroller(element: HTMLElement, rootParent: ScrollElement = window) { export function getScroller(el: HTMLElement, root: ScrollElement = window) {
let node = element; let node = el;
while ( while (
node && node &&
node.tagName !== 'HTML' && node.tagName !== 'HTML' &&
node.nodeType === 1 && node.nodeType === 1 &&
node !== rootParent node !== root
) { ) {
const { overflowY } = window.getComputedStyle(node); const { overflowY } = window.getComputedStyle(node);
if (overflowScrollReg.test(<string>overflowY)) { if (overflowScrollReg.test(<string>overflowY)) {
if (node.tagName !== 'BODY') { if (node.tagName !== 'BODY') {
return node; return node;
} }
// see: https://github.com/youzan/vant/issues/3823 // see: https://github.com/youzan/vant/issues/3823
const { overflowY: htmlOverflowY } = window.getComputedStyle(<Element>node.parentNode); const { overflowY: htmlOverflowY } = window.getComputedStyle(
<Element>node.parentNode
);
if (overflowScrollReg.test(<string>htmlOverflowY)) { if (overflowScrollReg.test(<string>htmlOverflowY)) {
return node; return node;
} }
} }
node = <HTMLElement>node.parentNode; node = <HTMLElement>node.parentNode;
} }
return rootParent;
return root;
} }
export function getScrollTop(element: ScrollElement): number { export function getScrollTop(el: ScrollElement): number {
return 'scrollTop' in element ? element.scrollTop : element.pageYOffset; return 'scrollTop' in el ? el.scrollTop : el.pageYOffset;
} }
export function setScrollTop(element: ScrollElement, value: number) { export function setScrollTop(el: ScrollElement, value: number) {
'scrollTop' in element ? (element.scrollTop = value) : element.scrollTo(element.scrollX, value); if ('scrollTop' in el) {
el.scrollTop = value;
} else {
el.scrollTo(el.scrollX, value);
}
} }
export function getRootScrollTop(): number { export function getRootScrollTop(): number {
return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0; return (
window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop ||
0
);
} }
export function setRootScrollTop(value: number) { export function setRootScrollTop(value: number) {
@ -47,19 +62,25 @@ export function setRootScrollTop(value: number) {
} }
// get distance from element top to page top // get distance from element top to page top
export function getElementTop(element: ScrollElement) { export function getElementTop(el: ScrollElement) {
return ( if (el === window) {
(element === window ? 0 : (<HTMLElement>element).getBoundingClientRect().top) + return 0;
getRootScrollTop() }
);
return (el as HTMLElement).getBoundingClientRect().top + getRootScrollTop();
} }
export function getVisibleHeight(element: ScrollElement) { export function getVisibleHeight(el: ScrollElement) {
return element === window if (el === window) {
? element.innerHeight return el.innerHeight;
: (<HTMLElement>element).getBoundingClientRect().height; }
return (el as HTMLElement).getBoundingClientRect().height;
} }
export function getVisibleTop(element: ScrollElement) { export function getVisibleTop(el: ScrollElement) {
return element === window ? 0 : (<HTMLElement>element).getBoundingClientRect().top; if (el === window) {
return 0;
}
return (el as HTMLElement).getBoundingClientRect().top;
} }

View File

@ -1,11 +1,11 @@
export function isHidden(element: HTMLElement) { export function isHidden(el: HTMLElement) {
const style = window.getComputedStyle(element); const style = window.getComputedStyle(el);
const isHidden = style.display === 'none'; const hidden = style.display === 'none';
// offsetParent returns null in the following situations: // offsetParent returns null in the following situations:
// 1. The element or its parent element has the display property set to none. // 1. The element or its parent element has the display property set to none.
// 2. The element has the position property set to fixed // 2. The element has the position property set to fixed
const isParentHidden = element.offsetParent === null && style.position !== 'fixed'; const parentHidden = el.offsetParent === null && style.position !== 'fixed';
return isHidden || isParentHidden; return hidden || parentHidden;
} }

View File

@ -19,16 +19,16 @@ const inheritKey = [
const mapInheritKey: ObjectIndex = { nativeOn: 'on' }; const mapInheritKey: ObjectIndex = { nativeOn: 'on' };
// inherit partial context, map nativeOn to on // inherit partial context, map nativeOn to on
export function inherit(context: Context, inheritListeners?: boolean): InheritContext { export function inherit(
const result = inheritKey.reduce( context: Context,
(obj, key) => { inheritListeners?: boolean
): InheritContext {
const result = inheritKey.reduce((obj, key) => {
if (context.data[key]) { if (context.data[key]) {
obj[mapInheritKey[key] || key] = context.data[key]; obj[mapInheritKey[key] || key] = context.data[key];
} }
return obj; return obj;
}, }, {} as InheritContext);
{} as InheritContext
);
if (inheritListeners) { if (inheritListeners) {
result.on = result.on || {}; result.on = result.on || {};

View File

@ -5,7 +5,9 @@ export type EventHandler = (event: Event) => void;
export type ObjectIndex = Record<string, any>; export type ObjectIndex = Record<string, any>;
export type ScopedSlot<Props = any> = (props?: Props) => VNode[] | VNode | undefined; export type ScopedSlot<Props = any> = (
props?: Props
) => VNode[] | VNode | undefined;
export type DefaultSlots = { export type DefaultSlots = {
default?: ScopedSlot; default?: ScopedSlot;
@ -22,10 +24,16 @@ export type ModelOptions = {
export type DefaultProps = ObjectIndex; export type DefaultProps = ObjectIndex;
export type FunctionComponent<Props = DefaultProps, PropDefs = PropsDefinition<Props>> = { export type FunctionComponent<
(h: CreateElement, props: Props, slots: ScopedSlots, context: RenderContext<Props>): Props = DefaultProps,
| VNode PropDefs = PropsDefinition<Props>
| undefined; > = {
(
h: CreateElement,
props: Props,
slots: ScopedSlots,
context: RenderContext<Props>
): VNode | undefined;
props?: PropDefs; props?: PropDefs;
model?: ModelOptions; model?: ModelOptions;
inject?: InjectOptions; inject?: InjectOptions;

View File

@ -2,6 +2,7 @@ import { isNaN } from './number';
export function isDate(date: Date): boolean { export function isDate(date: Date): boolean {
return ( return (
Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime()) Object.prototype.toString.call(date) === '[object Date]' &&
!isNaN(date.getTime())
); );
} }

View File

@ -1,4 +1,6 @@
export function isMobile(value: string): boolean { export function isMobile(value: string): boolean {
value = value.replace(/[^-|\d]/g, ''); value = value.replace(/[^-|\d]/g, '');
return /^((\+86)|(86))?(1)\d{10}$/.test(value) || /^0[0-9-]{10,13}$/.test(value); return (
/^((\+86)|(86))?(1)\d{10}$/.test(value) || /^0[0-9-]{10,13}$/.test(value)
);
} }

View File

@ -7,5 +7,7 @@ export function isAndroid(): boolean {
export function isIOS(): boolean { export function isIOS(): boolean {
/* istanbul ignore next */ /* istanbul ignore next */
return isServer ? false : /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase()); return isServer
? false
: /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase());
} }