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

fix(Slider): prevent onChange being called with same value #1528

Merged
merged 1 commit into from
Aug 25, 2022
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
6 changes: 6 additions & 0 deletions packages/dnb-eufemia/src/components/slider/SliderProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ export function SliderProvider(localProps: SliderProps) {
multiValues[currentIndex + 1] = numberValue
}
}

if (numberValue === realtimeValue.current[currentIndex]) {
return // stop here
}
} else if (numberValue === realtimeValue.current) {
return // stop here
}

if (typeof onChange === 'function') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const props: SliderProps = {
labelDirection: 'horizontal',
}

const resetMouseSimulation = () => {
fireEvent.mouseUp(document.querySelector('.dnb-slider__track'))
}

describe('Slider component', () => {
it('supports snake_case props', () => {
const props: SliderProps = {
Expand Down Expand Up @@ -215,6 +219,20 @@ describe('Slider component', () => {
).toBe('80,0 norske kroner')
})

it('will not emit onChange with same value twice', () => {
const onChange = jest.fn()

render(<Slider {...props} onChange={onChange} />)

simulateMouseMove({ pageX: 80, width: 100, height: 10 })
simulateMouseMove({ pageX: 80, width: 100, height: 10 })

expect(onChange).toBeCalledTimes(1)
expect(onChange.mock.calls[0][0].value).toBe(80)

resetMouseSimulation()
})

describe('multi thumb', () => {
const SliderWithStateUpdate = (props: SliderProps) => {
const [value, setValue] = React.useState(props.value)
Expand All @@ -227,9 +245,26 @@ describe('Slider component', () => {
return <Slider {...props} value={value} onChange={onChangehandler} />
}

const resetMouseSimulation = () => {
fireEvent.mouseUp(document.querySelector('.dnb-slider__track'))
}
const getRangeElement = (index: number) =>
document.querySelectorAll('[type="range"]')[
index
] as HTMLInputElement

it('will not emit onChange with same value twice', () => {
const onChange = jest.fn()

props.value = [20, 30, 90]
render(<SliderWithStateUpdate {...props} onChange={onChange} />)

fireEvent.mouseDown(getRangeElement(1))
simulateMouseMove({ pageX: 80, width: 100, height: 10 })
simulateMouseMove({ pageX: 80, width: 100, height: 10 })

expect(onChange).toBeCalledTimes(1)
expect(onChange.mock.calls[0][0].value).toEqual([20, 30, 80])

resetMouseSimulation()
})

it('tracks mousemove on track', () => {
const onChange = jest.fn()
Expand All @@ -243,14 +278,9 @@ describe('Slider component', () => {
/>
)

const getRangeElements = (index: number) =>
document.querySelectorAll('[type="range"]')[
index
] as HTMLInputElement

simulateMouseMove({ pageX: 80, width: 100, height: 10 })

expect(parseFloat(getRangeElements(2).value)).toBe(80)
expect(parseFloat(getRangeElement(2).value)).toBe(80)

expect(onChange).toBeCalledWith({
event: {
Expand Down Expand Up @@ -284,7 +314,7 @@ describe('Slider component', () => {
width: 100,
})

fireEvent.mouseDown(getRangeElements(1))
fireEvent.mouseDown(getRangeElement(1))

simulateMouseMove({ pageX: 40, width: 100, height: 10 })

Expand Down