Skip to content

Commit c106365

Browse files
authored
fix(NumberKeyboard): fix IOS missing the touch event when finger trigger the scale case (#6651)
* test: adjust test case * test: test driven * test: update test case
1 parent 2203ba2 commit c106365

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

src/components/number-keyboard/number-keyboard.tsx

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import React, { useRef, useMemo } from 'react'
2-
import type { FC, TouchEvent, MouseEvent } from 'react'
3-
import classNames from 'classnames'
1+
import { useMemoizedFn } from 'ahooks'
42
import { DownOutline, TextDeletionOutline } from 'antd-mobile-icons'
5-
import { mergeProps } from '../../utils/with-default-props'
3+
import classNames from 'classnames'
4+
import type { FC, MouseEvent, TouchEvent } from 'react'
5+
import React, { useMemo, useRef } from 'react'
6+
import { NativeProps, withNativeProps } from '../../utils/native-props'
67
import { shuffle } from '../../utils/shuffle'
8+
import { mergeProps } from '../../utils/with-default-props'
9+
import { useConfig } from '../config-provider'
710
import Popup, { PopupProps } from '../popup'
8-
import { NativeProps, withNativeProps } from '../../utils/native-props'
911
import SafeArea from '../safe-area'
10-
import { useMemoizedFn } from 'ahooks'
11-
import { useConfig } from '../config-provider'
1212

1313
const classPrefix = 'adm-number-keyboard'
1414

@@ -88,13 +88,13 @@ export const NumberKeyboard: FC<NumberKeyboardProps> = p => {
8888
props.onDelete?.()
8989
})
9090

91-
const onBackspacePressStart = () => {
91+
const startContinueClear = () => {
9292
timeoutRef.current = window.setTimeout(() => {
9393
onDelete()
9494
intervalRef.current = window.setInterval(onDelete, 150)
9595
}, 700)
9696
}
97-
const onBackspacePressEnd = () => {
97+
const stopContinueClear = () => {
9898
clearTimeout(timeoutRef.current)
9999
clearInterval(intervalRef.current)
100100
}
@@ -174,14 +174,16 @@ export const NumberKeyboard: FC<NumberKeyboardProps> = p => {
174174
key={key}
175175
className={className}
176176
onTouchStart={() => {
177+
stopContinueClear()
178+
177179
if (key === 'BACKSPACE') {
178-
onBackspacePressStart()
180+
startContinueClear()
179181
}
180182
}}
181183
onTouchEnd={e => {
182184
onKeyPress(e, key)
183185
if (key === 'BACKSPACE') {
184-
onBackspacePressEnd()
186+
stopContinueClear()
185187
}
186188
}}
187189
{...ariaProps}
@@ -226,11 +228,11 @@ export const NumberKeyboard: FC<NumberKeyboardProps> = p => {
226228
<div
227229
className={`${classPrefix}-key ${classPrefix}-key-extra ${classPrefix}-key-bs`}
228230
onTouchStart={() => {
229-
onBackspacePressStart()
231+
startContinueClear()
230232
}}
231233
onTouchEnd={e => {
232234
onKeyPress(e, 'BACKSPACE')
233-
onBackspacePressEnd()
235+
stopContinueClear()
234236
}}
235237
onContextMenu={e => {
236238
// Long press should not trigger native context menu

src/components/number-keyboard/tests/number-keyboard.test.tsx

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState } from 'react'
2-
import { fireEvent, render, testA11y, screen, waitFor } from 'testing'
2+
import { fireEvent, render, screen, testA11y, waitFor } from 'testing'
33
import NumberKeyboard from '..'
44

55
const classPrefix = 'adm-number-keyboard'
@@ -10,7 +10,17 @@ function mockClick(el: HTMLElement) {
1010
}
1111

1212
describe('NumberKeyboard', () => {
13+
beforeEach(() => {
14+
jest.useFakeTimers()
15+
})
16+
17+
afterEach(() => {
18+
jest.clearAllTimers()
19+
jest.useRealTimers()
20+
})
21+
1322
test('a11y', async () => {
23+
jest.useRealTimers()
1424
await testA11y(<NumberKeyboard visible />)
1525
})
1626

@@ -160,4 +170,34 @@ describe('NumberKeyboard', () => {
160170
expect(right).toBeInTheDocument()
161171
expect(screen.getByTitle('0')).not.toHaveClass(`${classPrefix}-key-mid`)
162172
})
173+
174+
test('long press backspace and release', () => {
175+
const onDelete = jest.fn()
176+
const { container } = render(<NumberKeyboard visible onDelete={onDelete} />)
177+
178+
// Fire touchstart event
179+
fireEvent.touchStart(
180+
document.body.querySelector(
181+
'.adm-number-keyboard-key-sign'
182+
) as HTMLDivElement,
183+
{ touches: [{}] }
184+
)
185+
onDelete.mockReset()
186+
187+
jest.advanceTimersByTime(10000)
188+
expect(onDelete).toHaveBeenCalled()
189+
onDelete.mockReset()
190+
191+
// We do not fire touchend event to mock ISO missing touchend event
192+
// Press other key
193+
fireEvent.touchStart(
194+
document.body.querySelector(
195+
'.adm-number-keyboard-key-number'
196+
) as HTMLDivElement,
197+
{ touches: [{}] }
198+
)
199+
200+
jest.advanceTimersByTime(10000)
201+
expect(onDelete).not.toHaveBeenCalled()
202+
})
163203
})

0 commit comments

Comments
 (0)