[improvement] check event.cancelable before preventDefault

This commit is contained in:
陈嘉涵 2019-05-10 14:18:46 +08:00
parent 2fb4bd3090
commit 4119b5b3c7
15 changed files with 43 additions and 33 deletions

View File

@ -2,6 +2,7 @@ import Icon from '../icon';
import Cell from '../cell'; import Cell from '../cell';
import { cellProps } from '../cell/shared'; import { cellProps } from '../cell/shared';
import { use, isObj, isDef, isIOS } from '../utils'; import { use, isObj, isDef, isIOS } from '../utils';
import { preventDefault } from '../utils/event';
import { getRootScrollTop } from '../utils/scroll'; import { getRootScrollTop } from '../utils/scroll';
import { isNumber } from '../utils/validate/number'; import { isNumber } from '../utils/validate/number';
@ -137,7 +138,7 @@ export default sfc({
}, },
onClear(event) { onClear(event) {
event.preventDefault(); preventDefault(event);
this.$emit('input', ''); this.$emit('input', '');
this.$emit('clear'); this.$emit('clear');
}, },
@ -148,8 +149,9 @@ export default sfc({
const allowPoint = String(this.value).indexOf('.') === -1; const allowPoint = String(this.value).indexOf('.') === -1;
const isValidKey = const isValidKey =
(keyCode >= 48 && keyCode <= 57) || (keyCode === 46 && allowPoint) || keyCode === 45; (keyCode >= 48 && keyCode <= 57) || (keyCode === 46 && allowPoint) || keyCode === 45;
if (!isValidKey) { if (!isValidKey) {
event.preventDefault(); preventDefault(event);
} }
} }

View File

@ -1,4 +1,5 @@
import { use, range } from '../utils'; import { use, range } from '../utils';
import { preventDefault } from '../utils/event';
import { PopupMixin } from '../mixins/popup'; import { PopupMixin } from '../mixins/popup';
import { TouchMixin } from '../mixins/touch'; import { TouchMixin } from '../mixins/touch';
import Swipe from '../swipe'; import Swipe from '../swipe';
@ -98,7 +99,7 @@ export default sfc({
}, },
onWrapperTouchEnd(event) { onWrapperTouchEnd(event) {
event.preventDefault(); preventDefault(event);
const deltaTime = new Date() - this.touchStartTime; const deltaTime = new Date() - this.touchStartTime;
const { offsetX = 0, offsetY = 0 } = this.$refs.swipe || {}; const { offsetX = 0, offsetY = 0 } = this.$refs.swipe || {};
@ -154,8 +155,7 @@ export default sfc({
onTouchMove(event) { onTouchMove(event) {
const { touches } = event; const { touches } = event;
if (this.moving || this.zooming) { if (this.moving || this.zooming) {
event.preventDefault(); preventDefault(event, true);
event.stopPropagation();
} }
if (this.moving) { if (this.moving) {
@ -199,8 +199,7 @@ export default sfc({
} }
if (stopPropagation) { if (stopPropagation) {
event.preventDefault(); preventDefault(event, true);
event.stopPropagation();
} }
} }
}, },

View File

@ -1,6 +1,6 @@
import { context } from './context'; import { context } from './context';
import { TouchMixin } from '../touch'; import { TouchMixin } from '../touch';
import { on, off } from '../../utils/event'; import { on, off, preventDefault } from '../../utils/event';
import { openOverlay, closeOverlay, updateOverlay } from './overlay'; import { openOverlay, closeOverlay, updateOverlay } from './overlay';
import { getScrollEventTarget } from '../../utils/scroll'; import { getScrollEventTarget } from '../../utils/scroll';
@ -176,13 +176,11 @@ export const PopupMixin = {
/* istanbul ignore next */ /* istanbul ignore next */
if ( if (
event.cancelable &&
status !== '11' && status !== '11' &&
this.direction === 'vertical' && this.direction === 'vertical' &&
!(parseInt(status, 2) & parseInt(direction, 2)) !(parseInt(status, 2) & parseInt(direction, 2))
) { ) {
event.preventDefault(); preventDefault(event, true);
event.stopPropagation();
} }
}, },

View File

@ -1,4 +1,5 @@
import { use } from '../utils'; import { use } from '../utils';
import { preventDefault } from '../utils/event';
const [sfc, bem] = use('key'); const [sfc, bem] = use('key');
@ -29,8 +30,7 @@ export default sfc({
}, },
onBlur(event) { onBlur(event) {
event.preventDefault(); preventDefault(event, true);
event.stopPropagation();
this.active = false; this.active = false;
} }
}, },

View File

@ -1,5 +1,5 @@
import { use } from '../utils'; import { use } from '../utils';
import { stop } from '../utils/event'; import { stopPropagation } from '../utils/event';
import Key from './Key'; import Key from './Key';
const [sfc, bem, t] = use('number-keyboard'); const [sfc, bem, t] = use('number-keyboard');
@ -145,7 +145,7 @@ export default sfc({
vShow={this.show} vShow={this.show}
style={{ zIndex: this.zIndex }} style={{ zIndex: this.zIndex }}
class={bem([theme, { 'safe-area-inset-bottom': this.safeAreaInsetBottom }])} class={bem([theme, { 'safe-area-inset-bottom': this.safeAreaInsetBottom }])}
onTouchstart={stop} onTouchstart={stopPropagation}
onAnimationend={this.onAnimationEnd} onAnimationend={this.onAnimationEnd}
onWebkitAnimationEnd={this.onAnimationEnd} onWebkitAnimationEnd={this.onAnimationEnd}
> >

View File

@ -1,5 +1,6 @@
import { use } from '../utils'; import { use } from '../utils';
import { inherit } from '../utils/functional'; import { inherit } from '../utils/functional';
import { preventDefault } from '../utils/event';
// Types // Types
import { CreateElement, RenderContext } from 'vue/types'; import { CreateElement, RenderContext } from 'vue/types';
@ -36,8 +37,7 @@ function Overlay(
style={style} style={style}
class={[bem(), props.className]} class={[bem(), props.className]}
onTouchmove={(event: TouchEvent) => { onTouchmove={(event: TouchEvent) => {
event.preventDefault(); preventDefault(event, true);
event.stopPropagation();
}} }}
{...inherit(ctx, true)} {...inherit(ctx, true)}
/> />

View File

@ -1,5 +1,6 @@
import { deepClone } from '../utils/deep-clone'; import { deepClone } from '../utils/deep-clone';
import { use, isObj, range } from '../utils'; import { use, isObj, range } from '../utils';
import { preventDefault } from '../utils/event';
const DEFAULT_DURATION = 200; const DEFAULT_DURATION = 200;
const [sfc, bem] = use('picker-column'); const [sfc, bem] = use('picker-column');
@ -55,7 +56,7 @@ export default sfc({
}, },
onTouchMove(event) { onTouchMove(event) {
event.preventDefault(); preventDefault(event);
const deltaY = event.touches[0].clientY - this.startY; const deltaY = event.touches[0].clientY - this.startY;
this.offset = range( this.offset = range(
this.startOffset + deltaY, this.startOffset + deltaY,

View File

@ -1,5 +1,5 @@
import { use } from '../utils'; import { use } from '../utils';
import { prevent } from '../utils/event'; import { preventDefault } from '../utils/event';
import { deepClone } from '../utils/deep-clone'; import { deepClone } from '../utils/deep-clone';
import { pickerProps } from './shared'; import { pickerProps } from './shared';
import Loading from '../loading'; import Loading from '../loading';
@ -177,7 +177,7 @@ export default sfc({
) : ( ) : (
h() h()
)} )}
<div class={bem('columns')} style={columnsStyle} onTouchmove={prevent}> <div class={bem('columns')} style={columnsStyle} onTouchmove={preventDefault}>
{columns.map((item, index) => ( {columns.map((item, index) => (
<PickerColumn <PickerColumn
valueKey={this.valueKey} valueKey={this.valueKey}

View File

@ -1,7 +1,8 @@
import { use } from '../utils'; import { use } from '../utils';
import Loading from '../loading'; import { preventDefault } from '../utils/event';
import { TouchMixin } from '../mixins/touch'; import { TouchMixin } from '../mixins/touch';
import { getScrollTop, getScrollEventTarget } from '../utils/scroll'; import { getScrollTop, getScrollEventTarget } from '../utils/scroll';
import Loading from '../loading';
const [sfc, bem, t] = use('pull-refresh'); const [sfc, bem, t] = use('pull-refresh');
const TEXT_STATUS = ['pulling', 'loosing', 'success']; const TEXT_STATUS = ['pulling', 'loosing', 'success'];
@ -90,7 +91,7 @@ export default sfc({
if (this.ceiling && this.deltaY >= 0) { if (this.ceiling && this.deltaY >= 0) {
if (this.direction === 'vertical') { if (this.direction === 'vertical') {
this.setStatus(this.ease(this.deltaY)); this.setStatus(this.ease(this.deltaY));
event.cancelable && event.preventDefault(); preventDefault(event);
} }
} }
}, },

View File

@ -1,6 +1,7 @@
/* eslint-disable prefer-spread */ /* eslint-disable prefer-spread */
import { use } from '../utils'; import { use } from '../utils';
import { emit, inherit } from '../utils/functional'; import { emit, inherit } from '../utils/functional';
import { preventDefault } from '../utils/event';
import Icon from '../icon'; import Icon from '../icon';
// Types // Types
@ -74,7 +75,7 @@ function Rate(
return; return;
} }
event.preventDefault(); preventDefault(event);
const { clientX, clientY } = event.touches[0]; const { clientX, clientY } = event.touches[0];
const target = document.elementFromPoint(clientX, clientY) as HTMLElement; const target = document.elementFromPoint(clientX, clientY) as HTMLElement;

View File

@ -1,5 +1,6 @@
import { use } from '../utils'; import { use } from '../utils';
import { inherit, emit } from '../utils/functional'; import { inherit, emit } from '../utils/functional';
import { preventDefault } from '../utils/event';
import Field from '../field'; import Field from '../field';
// Types // Types
@ -70,7 +71,7 @@ function Search(
keypress(event: KeyboardEvent) { keypress(event: KeyboardEvent) {
// press enter // press enter
if (event.keyCode === 13) { if (event.keyCode === 13) {
event.preventDefault(); preventDefault(event);
emit(ctx, 'search', props.value); emit(ctx, 'search', props.value);
} }
emit(ctx, 'keypress', event); emit(ctx, 'keypress', event);

View File

@ -1,5 +1,6 @@
import { use } from '../utils'; import { use } from '../utils';
import { TouchMixin } from '../mixins/touch'; import { TouchMixin } from '../mixins/touch';
import { preventDefault } from '../utils/event';
const [sfc, bem] = use('slider'); const [sfc, bem] = use('slider');
@ -36,8 +37,7 @@ export default sfc({
}, },
onTouchMove(event) { onTouchMove(event) {
event.preventDefault(); preventDefault(event, true);
event.stopPropagation();
if (this.disabled) return; if (this.disabled) return;

View File

@ -1,4 +1,5 @@
import { use, range } from '../utils'; import { use, range } from '../utils';
import { preventDefault } from '../utils/event';
import { TouchMixin } from '../mixins/touch'; import { TouchMixin } from '../mixins/touch';
import { ClickOutsideMixin } from '../mixins/click-outside'; import { ClickOutsideMixin } from '../mixins/click-outside';
@ -84,7 +85,7 @@ export default sfc({
this.touchMove(event); this.touchMove(event);
if (this.direction === 'horizontal') { if (this.direction === 'horizontal') {
event.preventDefault(); preventDefault(event);
this.swipeMove(this.deltaX + this.startOffset); this.swipeMove(this.deltaX + this.startOffset);
} }
}, },

View File

@ -1,5 +1,5 @@
import { use } from '../utils'; import { use } from '../utils';
import { on, off } from '../utils/event'; import { on, off, preventDefault } from '../utils/event';
import { TouchMixin } from '../mixins/touch'; import { TouchMixin } from '../mixins/touch';
const [sfc, bem] = use('swipe'); const [sfc, bem] = use('swipe');
@ -168,8 +168,7 @@ export default sfc({
this.touchMove(event); this.touchMove(event);
if (this.isCorrectDirection) { if (this.isCorrectDirection) {
event.preventDefault(); preventDefault(event, true);
event.stopPropagation();
this.move({ offset: Math.min(Math.max(this.delta, -this.size), this.size) }); this.move({ offset: Math.min(Math.max(this.delta, -this.size), this.size) });
} }
}, },

View File

@ -41,10 +41,17 @@ export function off(target: HTMLElement, event: string, handler: EventHanlder) {
} }
} }
export function stop(event: Event) { export function stopPropagation(event: Event) {
event.stopPropagation(); event.stopPropagation();
} }
export function prevent(event: Event) { export function preventDefault(event: Event, isStopPropagation?: boolean) {
/* istanbul ignore else */
if (typeof event.cancelable !== 'boolean' || event.cancelable) {
event.preventDefault(); event.preventDefault();
} }
if (isStopPropagation) {
stopPropagation(event);
}
}