-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1956 from oasisprotocol/ml/prevent-scroll-input-n…
…umber Prevent value change on number input wheel scroll
- Loading branch information
Showing
8 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Prevent value change on number input wheel scroll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { test, expect } from '@playwright/test' | ||
import { privateKey, privateKeyAddress } from '../../src/utils/__fixtures__/test-inputs' | ||
import { fillPrivateKeyWithoutPassword } from '../utils/fillPrivateKey' | ||
import { warnSlowApi } from '../utils/warnSlowApi' | ||
import { mockApi } from '../utils/mockApi' | ||
|
||
test.beforeEach(async ({ page }) => { | ||
await warnSlowApi(page) | ||
await mockApi(page, 123) | ||
|
||
await page.goto('/open-wallet/private-key') | ||
await fillPrivateKeyWithoutPassword(page, { | ||
privateKey: privateKey, | ||
privateKeyAddress: privateKeyAddress, | ||
persistenceCheckboxDisabled: false, | ||
}) | ||
await expect(page.getByTestId('account-selector')).toBeVisible() | ||
}) | ||
|
||
test('Scrolling on amount input field should preserve value', async ({ page }) => { | ||
await page.getByTestId('nav-myaccount').click() | ||
|
||
const input = page.getByPlaceholder('Enter an amount') | ||
await input.click() | ||
await input.fill('1111') | ||
const inputPosition = await input.boundingBox() | ||
await page.mouse.move( | ||
inputPosition!.x + inputPosition!.width / 2, | ||
inputPosition!.y + inputPosition!.height / 2, | ||
) | ||
await page.mouse.wheel(0, 10) | ||
await page.mouse.wheel(0, 10) | ||
await page.mouse.wheel(0, 10) | ||
await expect(input).toHaveValue('1111') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/app/lib/__tests__/usePreventChangeOnNumberInputScroll.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { render, fireEvent, act, waitFor } from '@testing-library/react' | ||
import { TextInput } from 'grommet/es6/components/TextInput' | ||
import { usePreventChangeOnNumberInputScroll } from '../usePreventChangeOnNumberInputScroll' | ||
|
||
const TextInputFormItem = () => ( | ||
<TextInput role="input" type="number" {...usePreventChangeOnNumberInputScroll()} /> | ||
) | ||
|
||
describe('usePreventChangeOnNumberInputScroll', () => { | ||
it('Should focus on input that is already in focus', async () => { | ||
const { getByRole } = render(<TextInputFormItem />) | ||
|
||
const numberInput = getByRole('input') | ||
fireEvent.change(numberInput, { target: { value: '123' } }) | ||
|
||
act(() => { | ||
fireEvent.focus(numberInput) | ||
fireEvent.wheel(numberInput) | ||
}) | ||
|
||
waitFor(() => expect(numberInput).toHaveFocus()) | ||
expect(numberInput).toHaveValue(123) | ||
}) | ||
|
||
it('Should not focus on input that is not focused', async () => { | ||
const { getByRole } = render(<TextInputFormItem />) | ||
|
||
const numberInput = getByRole('input') | ||
|
||
act(() => { | ||
fireEvent.wheel(numberInput) | ||
}) | ||
|
||
expect(numberInput).not.toHaveFocus() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { TextInputProps } from 'grommet/es6/components/TextInput' | ||
|
||
export const usePreventChangeOnNumberInputScroll = ({ onWheel }: Pick<TextInputProps, 'onWheel'> = {}): Pick< | ||
TextInputProps, | ||
'onWheel' | ||
> => { | ||
return { | ||
onWheel: e => { | ||
onWheel?.(e) | ||
|
||
const target = e.target as HTMLElement | ||
if (target !== document.activeElement) return | ||
|
||
// Prevents input value change | ||
target.blur() | ||
|
||
// Focus after blur | ||
setTimeout(() => { | ||
target.focus() | ||
}, 0) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters