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: prevent value empty string on slider right panel #3635

Merged
merged 2 commits into from
Sep 12, 2024
Merged
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
Next Next commit
fix: prevent value empty string on slider right panel
urmauur committed Sep 11, 2024
commit 552063b77b8b8b8229e7eb653a403142cad017c5
19 changes: 16 additions & 3 deletions web/containers/SliderRightPanel/index.test.tsx
Original file line number Diff line number Diff line change
@@ -67,26 +67,39 @@ describe('SliderRightPanel', () => {
it('calls onValueChanged with max value when input exceeds max', () => {
defaultProps.onValueChanged = jest.fn()
const { getByRole } = render(<SliderRightPanel {...defaultProps} />)
const input = getByRole('textbox')
const input = getByRole('textbox') as HTMLInputElement
fireEvent.change(input, { target: { value: '150' } })
fireEvent.focusOut(input)
expect(defaultProps.onValueChanged).toHaveBeenCalledWith(100)
expect(input.value).toEqual('100')
})

it('calls onValueChanged with min value when input is below min', () => {
defaultProps.onValueChanged = jest.fn()
const { getByRole } = render(<SliderRightPanel {...defaultProps} />)
const input = getByRole('textbox')
const input = getByRole('textbox') as HTMLInputElement
fireEvent.change(input, { target: { value: '0' } })
fireEvent.focusOut(input)
expect(defaultProps.onValueChanged).toHaveBeenCalledWith(0)
expect(input.value).toEqual('0')
urmauur marked this conversation as resolved.
Show resolved Hide resolved
})

it('calls onValueChanged when input value is empty string', () => {
defaultProps.onValueChanged = jest.fn()
const { getByRole } = render(<SliderRightPanel {...defaultProps} />)
const input = getByRole('textbox') as HTMLInputElement
fireEvent.change(input, { target: { value: '' } })
fireEvent.focusOut(input)
expect(defaultProps.onValueChanged).toHaveBeenCalledWith(0)
expect(input.value).toEqual('0')
})

it('does not call onValueChanged when input is invalid', () => {
defaultProps.onValueChanged = jest.fn()
const { getByRole } = render(<SliderRightPanel {...defaultProps} />)
const input = getByRole('textbox')
const input = getByRole('textbox') as HTMLInputElement
fireEvent.change(input, { target: { value: 'invalid' } })
expect(defaultProps.onValueChanged).not.toHaveBeenCalledWith(0)
expect(input.value).toEqual('50')
})
})
10 changes: 8 additions & 2 deletions web/containers/SliderRightPanel/index.tsx
Original file line number Diff line number Diff line change
@@ -80,7 +80,10 @@ const SliderRightPanel = ({
onValueChanged?.(Number(max))
setVal(max.toString())
setShowTooltip({ max: true, min: false })
} else if (Number(e.target.value) < Number(min)) {
} else if (
Number(e.target.value) < Number(min) ||
!e.target.value.length
) {
onValueChanged?.(Number(min))
setVal(min.toString())
setShowTooltip({ max: false, min: true })
@@ -92,7 +95,10 @@ const SliderRightPanel = ({
// Which is incorrect
if (Number(e.target.value) > Number(max)) {
setVal(max.toString())
} else if (Number(e.target.value) < Number(min)) {
} else if (
Number(e.target.value) < Number(min) ||
!e.target.value.length
) {
setVal(min.toString())
} else if (Number.isNaN(Number(e.target.value))) return