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

Prevent NumberField change on wheel [MC-2926] #561

Merged
merged 3 commits into from
Jul 12, 2024
Merged
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions packages/ui/__stories__/NumberField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ WithMinAndMax.args = {
max: 44,
}

export const WithChangeOnScrollDisabled = Template.bind({})
WithChangeOnScrollDisabled.args = {
disableChangeOnScroll: true,
}

export const WithHelperText = Template.bind({})
WithHelperText.args = {
helperText: 'Lorem Ipsum is simply dummy text of the printing and typesetting industry.',
Expand Down
24 changes: 23 additions & 1 deletion packages/ui/src/NumberField.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DataTestProp } from '@hazelcast/helpers'
import React, { FC, FocusEvent, ChangeEvent, InputHTMLAttributes, useCallback, useMemo } from 'react'
import React, { FC, FocusEvent, ChangeEvent, InputHTMLAttributes, useCallback, useMemo, useRef, useEffect } from 'react'
import { PlusCircle, MinusCircle } from 'react-feather'
import useIsomorphicLayoutEffect from 'react-use/lib/useIsomorphicLayoutEffect'
import cn from 'classnames'
Expand All @@ -23,6 +23,7 @@ export type NumberFieldExtraProps = Omit<TextFieldProps<'number'>, 'onChange' |
step?: number
min?: number
max?: number
disableChangeOnScroll?: boolean
defaultValue?: number
numberType?: 'int' | 'float'
iconPosition?: 'separate' | 'together'
Expand All @@ -46,6 +47,7 @@ export const NumberField: FC<NumberFieldProps> = ({
step = 1,
min,
max,
disableChangeOnScroll,
value,
numberType = 'int',
iconPosition = 'together',
Expand All @@ -57,6 +59,25 @@ export const NumberField: FC<NumberFieldProps> = ({
size,
...props
}) => {
const field = useRef<HTMLInputElement>(null)

const handleWheel = useCallback((e: WheelEvent) => {
const target = e.target as HTMLInputElement
target.blur()
}, [])

useEffect(() => {
if (disableChangeOnScroll) {
const current = field.current
if (current) {
current.addEventListener('wheel', handleWheel)
return () => {
current.removeEventListener('wheel', handleWheel)
}
}
}
}, [disableChangeOnScroll, handleWheel])

useIsomorphicLayoutEffect(() => {
if (min !== undefined && value !== undefined && value < min) {
onChange(min)
Expand Down Expand Up @@ -205,6 +226,7 @@ export const NumberField: FC<NumberFieldProps> = ({
return (
<TextField
{...props}
{...(disableChangeOnScroll ? { mRef: field } : {})}
value={value}
onChange={onChangeWrapped}
type="number"
Expand Down
Loading