Skip to content

Commit

Permalink
fix(text-field): add onWheel listener to prevent value from changing …
Browse files Browse the repository at this point in the history
…on wheel
  • Loading branch information
maximedasilva committed Mar 15, 2023
1 parent 7a937ae commit ca1410f
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/react/lib/TextField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ const TextField = forwardRef(({
)}
>
<Tag
{ ...rest }
autoFocus={autoFocus}
className="field"
ref={inputRef}
Expand All @@ -138,6 +137,8 @@ const TextField = forwardRef(({
onChange={onChange_}
onFocus={onFocus_}
onBlur={onBlur_}
onWheel={() => inputRef.current?.blur()}
{ ...rest }
/>
{ children }
</div>
Expand Down
19 changes: 19 additions & 0 deletions packages/react/lib/TextField/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,22 @@ export const withValidation = () => (
onValidate={val => /^[0-9]+$/g.test(val)}
/>
);

export const typeNumber = () => (
<TextField
className="field noscroll"
label="Age"
placeholder="Age"
type="number"
/>
);

export const withOnWheelOverrided = () => (
<TextField
className="field noscroll"
label="Age"
placeholder="Age"
type="number"
onWheel={action('wheel')}
/>
);
34 changes: 33 additions & 1 deletion packages/react/lib/TextField/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createRef } from 'react';
import { render } from '@testing-library/react';
import { fireEvent, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { blur, reset } from '~test-utils';
Expand Down Expand Up @@ -94,4 +94,36 @@ describe('<TextField />', () => {
expect(container).toMatchSnapshot();
unmount();
});

it('should blur on scroll', async () => {
const user = userEvent.setup();
const { unmount, container } = render(<TextField type="number" />);

const input = container.querySelector('input');
await user.click(input);
expect(container).toMatchSnapshot('focused');
fireEvent.wheel(container.querySelector('.junipero.text-field input'), {
deltaY: 30,
});
expect(container).toMatchSnapshot('unfocused');
unmount();
});

it('should do the given action on scroll if onWheel is set', async () => {
const user = userEvent.setup();
const customWheel = jest.fn();
const { unmount, container } = render(
<TextField type="number" onWheel={customWheel} />
);

const input = container.querySelector('input');
await user.click(input);
expect(container).toMatchSnapshot('focused');
fireEvent.wheel(container.querySelector('.junipero.text-field input'), {
deltaY: 30,
});
expect(customWheel).toHaveBeenCalled();
expect(container).toMatchSnapshot('always focused');
unmount();
});
});
56 changes: 56 additions & 0 deletions packages/react/lib/TextField/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,62 @@ exports[`<TextField /> should be invalid if validation fails 1`] = `
</div>
`;

exports[`<TextField /> should blur on scroll: focused 1`] = `
<div>
<div
class="junipero text-field pristine valid empty focused"
>
<input
class="field"
type="number"
value=""
/>
</div>
</div>
`;

exports[`<TextField /> should blur on scroll: unfocused 1`] = `
<div>
<div
class="junipero text-field pristine valid empty"
>
<input
class="field"
type="number"
value=""
/>
</div>
</div>
`;

exports[`<TextField /> should do the given action on scroll if onWheel is set: always focused 1`] = `
<div>
<div
class="junipero text-field pristine valid empty focused"
>
<input
class="field"
type="number"
value=""
/>
</div>
</div>
`;

exports[`<TextField /> should do the given action on scroll if onWheel is set: focused 1`] = `
<div>
<div
class="junipero text-field pristine valid empty focused"
>
<input
class="field"
type="number"
value=""
/>
</div>
</div>
`;

exports[`<TextField /> should not allow to change value when disabled 1`] = `
<div>
<div
Expand Down

0 comments on commit ca1410f

Please sign in to comment.