Skip to content

Commit

Permalink
Prevent NumberField change on wheel [MC-2926] (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
emrberk authored Jul 12, 2024
1 parent 9351c2e commit 4daa657
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
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

0 comments on commit 4daa657

Please sign in to comment.