Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(input): 添加输入事件延迟触发 #6444

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions packages/taro-components/src/components/input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'weui'
import Nerv from 'nervjs'
import omit from 'omit.js'
import classNames from 'classnames'
import { Timer } from '@tarojs/utils'

import './index.scss'

Expand Down Expand Up @@ -35,6 +36,11 @@ class Input extends Nerv.Component {
// input hook
this.isOnComposition = false
this.onInputExcuted = false

const onInputDelay = this.props.onInputDelay
if (onInputDelay && typeof onInputDelay === 'number' && this.onInputDelay > 0) {
this.timer = Timer.delay(onInputDelay)
}
}

componentDidMount () {
Expand All @@ -49,6 +55,9 @@ class Input extends Nerv.Component {
if (this.props.type === 'file') {
this.inputRef.removeEventListener('change', this.onInput)
}
if (this.timer) {
this.timer.clear()
}
}

onInput (e) {
Expand Down Expand Up @@ -85,8 +94,8 @@ class Input extends Nerv.Component {
)
}

if (onChange) return onChange(e)
if (onInput) return onInput(e)
if (onChange) return this.timer ? this.timer.run(onChange, e) : onChange(e)
if (onInput) return this.timer ? this.timer.run(onInput, e) : onInput(e)
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/taro-components/src/components/input/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ input
| √ (onFocus) | bindfocus | EventHandle | | 输入框聚焦时触发,height 参数在基础库 1.9.90 起支持 |
| √ (onBlur) | bindblur | EventHandle | | 输入框失去焦点时触发 |
| | bindconfirm | EventHandle | | 点击完成按钮时触发 |
| | on-input-delay | Number | | 当键盘输入时,指延迟指定时间后触发 input 事件 |
7 changes: 5 additions & 2 deletions packages/taro-utils/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import shallowEqual from './shallow-equal'
import SimpleMap from './simple-map'
import Timer from './timer'

function addLeadingSlash (path) {
return path.charAt(0) === '/' ? path : '/' + path
Expand All @@ -14,11 +15,13 @@ function getCurrentPageUrl () {
export default {
shallowEqual,
getCurrentPageUrl,
SimpleMap
SimpleMap,
Timer
}

export {
shallowEqual,
getCurrentPageUrl,
SimpleMap
SimpleMap,
Timer
}
41 changes: 41 additions & 0 deletions packages/taro-utils/src/timer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const TIMER_TIMEOUT = 1
const TIMER_INTERVAL = 2

export default class Timer {
constructor (type, ms) {
this.type = type
this.ms = ms
this.handler = null
}

static delay (ms) {
return new Timer(TIMER_TIMEOUT, ms)
}

static period (ms) {
return new Timer(TIMER_INTERVAL, ms)
}

run (fn, ...args) {
this.clear()

if (this.type === TIMER_TIMEOUT) {
this.handler = setTimeout(fn, this.ms, ...args)
return true
} else if (this.type === TIMER_INTERVAL) {
this.handler = setInterval(fn, this.ms, ...args)
return true
}
return false
}

clear () {
if (this.handler) {
if (this.type === TIMER_TIMEOUT) {
clearTimeout(this.handler)
} else if (this.type === TIMER_INTERVAL) {
clearInterval(this.handler)
}
}
}
}