chore: prettier all ts code
This commit is contained in:
@@ -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<Vue> {
|
||||
@@ -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<Props = DefaultProps, Events = {}, Slots = {}> (
|
||||
return function<Props = DefaultProps, Events = {}, Slots = {}>(
|
||||
sfc: VantComponentOptions | FunctionComponent
|
||||
): TsxComponent<Props, Events, Slots> {
|
||||
if (typeof sfc === 'function') {
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
+42
-21
@@ -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(<string>overflowY)) {
|
||||
if (node.tagName !== 'BODY') {
|
||||
return node;
|
||||
}
|
||||
|
||||
// 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)) {
|
||||
return node;
|
||||
}
|
||||
}
|
||||
node = <HTMLElement>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 : (<HTMLElement>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
|
||||
: (<HTMLElement>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 : (<HTMLElement>element).getBoundingClientRect().top;
|
||||
export function getVisibleTop(el: ScrollElement) {
|
||||
if (el === window) {
|
||||
return 0;
|
||||
}
|
||||
return (el as HTMLElement).getBoundingClientRect().top;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+10
-10
@@ -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 || {};
|
||||
|
||||
+13
-5
@@ -5,7 +5,9 @@ export type EventHandler = (event: Event) => void;
|
||||
|
||||
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 = {
|
||||
default?: ScopedSlot;
|
||||
@@ -22,10 +24,16 @@ export type ModelOptions = {
|
||||
|
||||
export type DefaultProps = ObjectIndex;
|
||||
|
||||
export type FunctionComponent<Props = DefaultProps, PropDefs = PropsDefinition<Props>> = {
|
||||
(h: CreateElement, props: Props, slots: ScopedSlots, context: RenderContext<Props>):
|
||||
| VNode
|
||||
| undefined;
|
||||
export type FunctionComponent<
|
||||
Props = DefaultProps,
|
||||
PropDefs = PropsDefinition<Props>
|
||||
> = {
|
||||
(
|
||||
h: CreateElement,
|
||||
props: Props,
|
||||
slots: ScopedSlots,
|
||||
context: RenderContext<Props>
|
||||
): VNode | undefined;
|
||||
props?: PropDefs;
|
||||
model?: ModelOptions;
|
||||
inject?: InjectOptions;
|
||||
|
||||
@@ -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())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user