diff --git a/src/cell/shared.ts b/src/cell/shared.ts index 5721801f4..9553abf88 100644 --- a/src/cell/shared.ts +++ b/src/cell/shared.ts @@ -14,7 +14,7 @@ export type SharedCellProps = { value?: string | number; label?: string | number; arrowDirection?: 'up' | 'down' | 'left' | 'right'; -} +}; export const cellProps = { icon: String, diff --git a/src/mixins/click-outside.ts b/src/mixins/click-outside.ts index 057e4867d..d3be9be04 100644 --- a/src/mixins/click-outside.ts +++ b/src/mixins/click-outside.ts @@ -20,7 +20,10 @@ export const ClickOutsideMixin = (config: ClickOutsideMixinConfig) => data() { 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](); } }; diff --git a/src/mixins/portal.ts b/src/mixins/portal.ts index fab338113..6c42faed7 100644 --- a/src/mixins/portal.ts +++ b/src/mixins/portal.ts @@ -17,7 +17,7 @@ function getElement(selector: string | GetContainer): Element | null { export function PortalMixin({ ref, afterPortal }: PortalMixinOptions) { return Vue.extend({ props: { - getContainer: [String, Function] as (PropType), + getContainer: [String, Function] as PropType, }, watch: { @@ -33,7 +33,7 @@ export function PortalMixin({ ref, afterPortal }: PortalMixinOptions) { methods: { portal() { 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; if (getContainer) { diff --git a/src/mixins/relation.ts b/src/mixins/relation.ts index 208f9facc..64830b927 100644 --- a/src/mixins/relation.ts +++ b/src/mixins/relation.ts @@ -25,7 +25,10 @@ type ChildrenMixinThis = { disableBindRelation?: boolean; }; -export function ChildrenMixin(parent: string, options: ChildrenMixinOptions = {}) { +export function ChildrenMixin( + parent: string, + options: ChildrenMixinOptions = {} +) { const indexKey = options.indexKey || 'index'; return Vue.extend({ @@ -56,7 +59,9 @@ export function ChildrenMixin(parent: string, options: ChildrenMixinOptions = {} beforeDestroy() { 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 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; }, diff --git a/src/mixins/touch.ts b/src/mixins/touch.ts index 0a1c3621e..aa4fa5ee9 100644 --- a/src/mixins/touch.ts +++ b/src/mixins/touch.ts @@ -58,7 +58,8 @@ export const TouchMixin = Vue.extend({ // avoid Vue 2.6 event bubble issues by manually binding events // https://github.com/youzan/vant/issues/3015 bindTouchEvent(el: HTMLElement) { - const { onTouchStart, onTouchMove, onTouchEnd } = (this as any); + const { onTouchStart, onTouchMove, onTouchEnd } = this as any; + on(el, 'touchstart', onTouchStart); on(el, 'touchmove', onTouchMove); diff --git a/src/picker/shared.ts b/src/picker/shared.ts index 85bc0c25b..085eee17a 100644 --- a/src/picker/shared.ts +++ b/src/picker/shared.ts @@ -6,7 +6,7 @@ export type SharedPickerProps = { visibleItemCount: number; cancelButtonText?: string; confirmButtonText?: string; -} +}; export const pickerProps = { title: String, diff --git a/src/sku/lang.ts b/src/sku/lang.ts index a5de84375..d3132172d 100644 --- a/src/sku/lang.ts +++ b/src/sku/lang.ts @@ -16,14 +16,16 @@ export default { stock: '剩余', stockUnit: '件', quotaTip: (quota: number) => `每人限购${quota}件`, - quotaUsedTip: (quota: number, count: number) => `每人限购${quota}件,你已购买${count}件`, + quotaUsedTip: (quota: number, count: number) => + `每人限购${quota}件,你已购买${count}件`, }, vanSkuActions: { buy: '立即购买', addCart: '加入购物车', }, vanSkuImgUploader: { - oversize: (maxSize: number) => `最大可上传图片为${maxSize}MB,请尝试压缩图片尺寸`, + oversize: (maxSize: number) => + `最大可上传图片为${maxSize}MB,请尝试压缩图片尺寸`, fail: '上传失败
重新上传', }, vanSkuStepper: { diff --git a/src/tree-select/demo/data.en-US.ts b/src/tree-select/demo/data.en-US.ts index cd948c788..57d93f4e0 100644 --- a/src/tree-select/demo/data.en-US.ts +++ b/src/tree-select/demo/data.en-US.ts @@ -48,7 +48,6 @@ const group3 = [ }, ]; - export const enUSData = [ { text: 'Group 1', diff --git a/src/utils/create/component.ts b/src/utils/create/component.ts index 7c266d702..46f338a29 100644 --- a/src/utils/create/component.ts +++ b/src/utils/create/component.ts @@ -4,7 +4,12 @@ import '../../locale'; import { camelize } from '../format/string'; 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'; export interface VantComponentOptions extends ComponentOptions { @@ -47,17 +52,20 @@ export function unifySlots(context: RenderContext) { } // should be removed after Vue 3 -function transformFunctionComponent(pure: FunctionComponent): VantComponentOptions { +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), + render: (h, context): any => + pure(h, context.props, unifySlots(context), context), }; } export function createComponent(name: string) { - return function ( + return function( sfc: VantComponentOptions | FunctionComponent ): TsxComponent { if (typeof sfc === 'function') { diff --git a/src/utils/create/i18n.ts b/src/utils/create/i18n.ts index 9d43c2e63..a1bf18ddb 100644 --- a/src/utils/create/i18n.ts +++ b/src/utils/create/i18n.ts @@ -6,7 +6,9 @@ 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); + const messages = locale.messages(); + const message = get(messages, prefix + path) || get(messages, path); + return typeof message === 'function' ? message(...args) : message; }; } diff --git a/src/utils/deep-assign.ts b/src/utils/deep-assign.ts index 5686ef425..6ce75ac8d 100644 --- a/src/utils/deep-assign.ts +++ b/src/utils/deep-assign.ts @@ -10,7 +10,11 @@ function assignKey(to: ObjectIndex, from: ObjectIndex, key: string) { return; } - if (!hasOwnProperty.call(to, key) || !isObj(val) || typeof val === 'function') { + if ( + !hasOwnProperty.call(to, key) || + !isObj(val) || + typeof val === 'function' + ) { to[key] = val; } else { // eslint-disable-next-line no-use-before-define diff --git a/src/utils/dom/event.ts b/src/utils/dom/event.ts index 4308297e4..459e581f1 100644 --- a/src/utils/dom/event.ts +++ b/src/utils/dom/event.ts @@ -15,7 +15,7 @@ if (!isServer) { }, }); window.addEventListener('test-passive', null as any, opts); - // eslint-disable-next-line no-empty + // eslint-disable-next-line no-empty } catch (e) {} } @@ -34,11 +34,7 @@ export function on( } } -export function off( - target: EventTarget, - event: string, - handler: EventHandler -) { +export function off(target: EventTarget, event: string, handler: EventHandler) { if (!isServer) { target.removeEventListener(event, handler); } diff --git a/src/utils/dom/scroll.ts b/src/utils/dom/scroll.ts index 44a651bc1..adb36fbd5 100644 --- a/src/utils/dom/scroll.ts +++ b/src/utils/dom/scroll.ts @@ -4,41 +4,56 @@ type ScrollElement = HTMLElement | Window; // http://w3help.org/zh-cn/causes/SD9013 // http://stackoverflow.com/questions/17016740/onscroll-function-is-not-working-for-chrome const overflowScrollReg = /scroll|auto/i; -export function getScroller(element: HTMLElement, rootParent: ScrollElement = window) { - let node = element; +export function getScroller(el: HTMLElement, root: ScrollElement = window) { + let node = el; + while ( node && node.tagName !== 'HTML' && node.nodeType === 1 && - node !== rootParent + node !== root ) { const { overflowY } = window.getComputedStyle(node); + if (overflowScrollReg.test(overflowY)) { if (node.tagName !== 'BODY') { return node; } // see: https://github.com/youzan/vant/issues/3823 - const { overflowY: htmlOverflowY } = window.getComputedStyle(node.parentNode); + const { overflowY: htmlOverflowY } = window.getComputedStyle( + node.parentNode + ); + if (overflowScrollReg.test(htmlOverflowY)) { return node; } } node = node.parentNode; } - return rootParent; + + return root; } -export function getScrollTop(element: ScrollElement): number { - return 'scrollTop' in element ? element.scrollTop : element.pageYOffset; +export function getScrollTop(el: ScrollElement): number { + return 'scrollTop' in el ? el.scrollTop : el.pageYOffset; } -export function setScrollTop(element: ScrollElement, value: number) { - 'scrollTop' in element ? (element.scrollTop = value) : element.scrollTo(element.scrollX, value); +export function setScrollTop(el: ScrollElement, value: number) { + if ('scrollTop' in el) { + el.scrollTop = value; + } else { + el.scrollTo(el.scrollX, value); + } } 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) { @@ -47,19 +62,25 @@ export function setRootScrollTop(value: number) { } // get distance from element top to page top -export function getElementTop(element: ScrollElement) { - return ( - (element === window ? 0 : (element).getBoundingClientRect().top) + - getRootScrollTop() - ); +export function getElementTop(el: ScrollElement) { + if (el === window) { + return 0; + } + + return (el as HTMLElement).getBoundingClientRect().top + getRootScrollTop(); } -export function getVisibleHeight(element: ScrollElement) { - return element === window - ? element.innerHeight - : (element).getBoundingClientRect().height; +export function getVisibleHeight(el: ScrollElement) { + if (el === window) { + return el.innerHeight; + } + + return (el as HTMLElement).getBoundingClientRect().height; } -export function getVisibleTop(element: ScrollElement) { - return element === window ? 0 : (element).getBoundingClientRect().top; +export function getVisibleTop(el: ScrollElement) { + if (el === window) { + return 0; + } + return (el as HTMLElement).getBoundingClientRect().top; } diff --git a/src/utils/dom/style.ts b/src/utils/dom/style.ts index 8c725a564..e3fe897b1 100644 --- a/src/utils/dom/style.ts +++ b/src/utils/dom/style.ts @@ -1,11 +1,11 @@ -export function isHidden(element: HTMLElement) { - const style = window.getComputedStyle(element); - const isHidden = style.display === 'none'; +export function isHidden(el: HTMLElement) { + const style = window.getComputedStyle(el); + const hidden = style.display === 'none'; // offsetParent returns null in the following situations: // 1. The element or its parent element has the display property set to none. // 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; } diff --git a/src/utils/functional.ts b/src/utils/functional.ts index 336616d4a..ee7e6df16 100644 --- a/src/utils/functional.ts +++ b/src/utils/functional.ts @@ -19,16 +19,16 @@ const inheritKey = [ const mapInheritKey: ObjectIndex = { nativeOn: 'on' }; // inherit partial context, map nativeOn to on -export function inherit(context: Context, inheritListeners?: boolean): InheritContext { - const result = inheritKey.reduce( - (obj, key) => { - if (context.data[key]) { - obj[mapInheritKey[key] || key] = context.data[key]; - } - return obj; - }, - {} as InheritContext - ); +export function inherit( + context: Context, + inheritListeners?: boolean +): InheritContext { + const result = inheritKey.reduce((obj, key) => { + if (context.data[key]) { + obj[mapInheritKey[key] || key] = context.data[key]; + } + return obj; + }, {} as InheritContext); if (inheritListeners) { result.on = result.on || {}; diff --git a/src/utils/types.ts b/src/utils/types.ts index 8e1a50b65..14821d4b6 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -5,7 +5,9 @@ export type EventHandler = (event: Event) => void; export type ObjectIndex = Record; -export type ScopedSlot = (props?: Props) => VNode[] | VNode | undefined; +export type ScopedSlot = ( + props?: Props +) => VNode[] | VNode | undefined; export type DefaultSlots = { default?: ScopedSlot; @@ -22,10 +24,16 @@ export type ModelOptions = { export type DefaultProps = ObjectIndex; -export type FunctionComponent> = { - (h: CreateElement, props: Props, slots: ScopedSlots, context: RenderContext): - | VNode - | undefined; +export type FunctionComponent< + Props = DefaultProps, + PropDefs = PropsDefinition +> = { + ( + h: CreateElement, + props: Props, + slots: ScopedSlots, + context: RenderContext + ): VNode | undefined; props?: PropDefs; model?: ModelOptions; inject?: InjectOptions; diff --git a/src/utils/validate/date.ts b/src/utils/validate/date.ts index f7a2e2ed1..4e43e6889 100644 --- a/src/utils/validate/date.ts +++ b/src/utils/validate/date.ts @@ -2,6 +2,7 @@ import { isNaN } from './number'; export function isDate(date: Date): boolean { return ( - Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime()) + Object.prototype.toString.call(date) === '[object Date]' && + !isNaN(date.getTime()) ); } diff --git a/src/utils/validate/mobile.ts b/src/utils/validate/mobile.ts index 8ee0aa87a..b32401981 100644 --- a/src/utils/validate/mobile.ts +++ b/src/utils/validate/mobile.ts @@ -1,4 +1,6 @@ export function isMobile(value: string): boolean { 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) + ); } diff --git a/src/utils/validate/system.ts b/src/utils/validate/system.ts index 3c13bb612..d91f2f131 100644 --- a/src/utils/validate/system.ts +++ b/src/utils/validate/system.ts @@ -7,5 +7,7 @@ export function isAndroid(): boolean { export function isIOS(): boolean { /* 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()); }