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(TextField): add wheel event handler for input[type="number"] #3820

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions src/components/TextField/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const TextFieldRender = <TYPE extends string>(
onKeyUp,
onKeyUpCapture,
onClear,
onWheel: onWheelProp,
...otherProps
} = usePropsHandler(COMPONENT_NAME, props, textFieldRef);

Expand Down Expand Up @@ -239,6 +240,24 @@ export const TextFieldRender = <TYPE extends string>(
onEvent: onKeyDownProp,
});

const onWheel = (e: React.WheelEvent<HTMLInputElement>) => {
if (!step) {
inputRef.current?.blur();
} else if (type === 'number' && onChange) {
const { value } = e.target as HTMLInputElement;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут необходимо учитывать брать значение шага из getValueByStep, так как шаг можно указать массивом произвольных чисел.

Я в канари компоненте начну сейчас реализацию если что сюда могу перенести, задачка у меня в спринте уже была.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Так здесь же по идее эту задачу браузер возьмет на себя, и в поле value будет значение с учетом шага

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Да, но можно в step закинуть массив к примеру [0,15,25,70] и тогда нужно чтобы инпут переходил от шага к шагу.

onChange(value, {
e,
id,
name,
});
}

if (onWheelProp) {
onWheelProp(e);
}
};

const textareaProps = {
rows,
cols,
Expand All @@ -255,6 +274,7 @@ export const TextFieldRender = <TYPE extends string>(
min,
step: !Array.isArray(sortedSteps) ? sortedSteps : 0,
onKeyDown,
onWheel,
ref: useForkRef([
inputRef,
inputRefProp,
Expand Down
24 changes: 24 additions & 0 deletions src/components/TextField/__tests__/TextField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,28 @@ describe('Компонент Button', () => {
});
});
});
describe('проверка onWheel при input[type="number"]', () => {
it('проверяет поведение при step = 0', () => {
const handleChange = jest.fn();
renderComponent({
type: 'number',
step: 0,
onChange: handleChange,
});

const input = getInput() as Element;
fireEvent.wheel(input, { deltaMode: 0, deltaY: 10 });

expect(handleChange).toHaveBeenCalledTimes(0);
});
it('проверяет поведение при наличии step', () => {
const handleChange = jest.fn();

renderComponent({ type: 'number', step: 1, onChange: handleChange });
const input = getInput() as Element;
fireEvent.wheel(input, { deltaMode: 0, deltaY: 10 });

expect(handleChange).toHaveBeenCalledTimes(1);
});
});
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,11 @@
webpack-manifest-plugin "^5.0.0"
workbox-webpack-plugin "^6.4.1"

"@consta/uikit@^5.0.1":
version "5.14.0"
resolved "https://registry.yarnpkg.com/@consta/uikit/-/uikit-5.14.0.tgz#1457c54a28623db720e9e6efb2fb5451aa533f73"
integrity sha512-IeBhi7HgyRw2aZWVb9va5rAxt/EIqZ2vJx+30OKMWNwBOfViV2QXLpTQ74z+sJXg+L5KinO0rtAbyIfjkrjPMQ==

"@consta/uikit@^5.4.1":
version "5.9.2"
resolved "https://registry.yarnpkg.com/@consta/uikit/-/uikit-5.9.2.tgz#5e3ee82b51445e8648305b82f096b23daf2f22c4"
Expand Down
Loading