-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redesign Slider and add a11y support (#282)
* Redesign Slider * Added a11y support * Code review - ts fixes, support for keyboard focus * Small styling fix - margins and input focus
- Loading branch information
1 parent
408b27c
commit ef0f0a4
Showing
5 changed files
with
360 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,46 @@ | ||
import React, { useEffect, useRef } from 'react'; | ||
|
||
import { | ||
HandleStyled, | ||
Hidden, | ||
LabelInput, | ||
Label, | ||
} from './Slider.style'; | ||
import React from 'react'; | ||
|
||
import { HandleStyled, Hidden } from './Slider.style'; | ||
import { Focus } from '../../../utils'; | ||
|
||
export function Handle({ | ||
disabled, | ||
dragging, | ||
editableLabel, | ||
focused, | ||
formatter, | ||
handle, | ||
max, | ||
min, | ||
onChange, | ||
onDragEnd, | ||
setDragging, | ||
setFocus, | ||
setValue, | ||
value, | ||
...props | ||
}) { | ||
const ref = useRef(null); | ||
|
||
useEffect(() => { | ||
const event = new Event('change', { bubbles: true }); | ||
ref?.current?.dispatchEvent(event); | ||
onChange && onChange(event); | ||
}, [onChange, value]); | ||
|
||
return ( | ||
<HandleStyled | ||
focused={focused === handle || dragging === handle} | ||
style={{ left: (value / max) * 100 + '%' }} | ||
onMouseDown={() => { | ||
!disabled && setDragging(handle); | ||
}} | ||
{...props} | ||
> | ||
<Hidden | ||
min={min} | ||
max={max} | ||
value={value} | ||
onFocus={setFocus(handle)} | ||
onBlur={setFocus(false)} | ||
ref={ref} | ||
readOnly | ||
/> | ||
<Focus | ||
parent={Hidden} | ||
radius={50} | ||
focused={focused === handle || dragging === handle} | ||
/> | ||
|
||
<Label focused={focused}> | ||
{editableLabel ? ( | ||
<> | ||
<LabelInput | ||
value={value} | ||
onChange={(e) => setValue(e.target.value)} | ||
onFocus={setFocus(handle)} | ||
onBlur={setFocus(false)} | ||
focused={focused === handle} | ||
/> | ||
<Focus parent={LabelInput} distance={1} /> | ||
</> | ||
) : ( | ||
formatter(value) | ||
)} | ||
</Label> | ||
</HandleStyled> | ||
); | ||
interface Props { | ||
disabled: boolean; | ||
handle: string; | ||
min: number; | ||
max: number; | ||
setDragging: (value: string) => void; | ||
setFocus: (value: string) => void; | ||
value: number; | ||
} | ||
|
||
export const Handle = React.forwardRef<HTMLInputElement, Props>( | ||
( | ||
{ disabled, handle, min, max, setDragging, setFocus, value }, | ||
ref | ||
) => { | ||
return ( | ||
<HandleStyled | ||
style={{ left: (value / max) * 100 + '%' }} | ||
aria-label={handle} | ||
onMouseDown={() => { | ||
!disabled && setDragging(handle); | ||
}} | ||
> | ||
<Hidden | ||
min={min} | ||
max={max} | ||
value={value} | ||
onFocus={() => { | ||
setFocus(handle); | ||
}} | ||
onBlur={() => { | ||
setFocus(null); | ||
}} | ||
ref={ref} | ||
readOnly | ||
/> | ||
<Focus parent={Hidden} radius={50} isKeyboardOnly /> | ||
</HandleStyled> | ||
); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import React from 'react'; | ||
import '@testing-library/jest-dom'; | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import { ThemeProvider } from 'styled-components'; | ||
import { themes } from '../../../themes'; | ||
import { Slider } from './Slider'; | ||
|
||
describe('Slider', () => { | ||
it('Renders slider', () => { | ||
render( | ||
<ThemeProvider theme={themes['light']}> | ||
<Slider /> | ||
</ThemeProvider> | ||
); | ||
|
||
const slider = screen.getByLabelText('slider'); | ||
expect(slider).toBeInTheDocument(); | ||
}); | ||
|
||
it('Renders slider with 50% value', async () => { | ||
render( | ||
<ThemeProvider theme={themes['light']}> | ||
<Slider initialValues={[0, 100]} editableLabel /> | ||
</ThemeProvider> | ||
); | ||
|
||
const input = screen.getByRole('start-input'); | ||
await userEvent.click(input); | ||
fireEvent.change(input, { target: { value: 50 } }); | ||
|
||
const handle = screen.getByLabelText('startHandle'); | ||
expect(handle).toHaveStyle({ left: '50%' }); | ||
}); | ||
|
||
it('Renders slider with min and max', async () => { | ||
render( | ||
<ThemeProvider theme={themes['light']}> | ||
<Slider | ||
initialValues={[0, 100]} | ||
editableLabel | ||
min={0} | ||
max={100} | ||
range | ||
/> | ||
</ThemeProvider> | ||
); | ||
|
||
const endInput = screen.getByRole('end-input'); | ||
await userEvent.click(endInput); | ||
fireEvent.change(endInput, { target: { value: 120 } }); | ||
|
||
const endHandle = screen.getByLabelText('endHandle'); | ||
expect(endHandle).toHaveStyle({ left: '100%' }); | ||
|
||
const startInput = screen.getByRole('start-input'); | ||
await userEvent.click(startInput); | ||
fireEvent.change(startInput, { target: { value: -10 } }); | ||
|
||
const startHandle = screen.getByLabelText('startHandle'); | ||
expect(startHandle).toHaveStyle({ left: '0%' }); | ||
}); | ||
|
||
it('Renders slider and fire onChange', async () => { | ||
const onChange = jest.fn(); | ||
|
||
render( | ||
<ThemeProvider theme={themes['light']}> | ||
<Slider | ||
onChange={onChange} | ||
initialValues={[0, 100]} | ||
editableLabel | ||
/> | ||
</ThemeProvider> | ||
); | ||
|
||
const input = screen.getByRole('start-input'); | ||
await userEvent.click(input); | ||
fireEvent.change(input, { target: { value: 50 } }); | ||
|
||
expect(onChange).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('Cannot change value of disabled slider', async () => { | ||
render( | ||
<ThemeProvider theme={themes['light']}> | ||
<Slider initialValues={[0, 100]} disabled editableLabel /> | ||
</ThemeProvider> | ||
); | ||
|
||
const input = screen.getByRole('start-input'); | ||
expect(input).toHaveAttribute('disabled'); | ||
}); | ||
|
||
it('Limit values based on range', async () => { | ||
render( | ||
<ThemeProvider theme={themes['light']}> | ||
<Slider initialValues={[0, 100]} range editableLabel /> | ||
</ThemeProvider> | ||
); | ||
|
||
const endInput = screen.getByRole('end-input'); | ||
await userEvent.click(endInput); | ||
fireEvent.change(endInput, { target: { value: 80 } }); | ||
|
||
const startInput = screen.getByRole('start-input'); | ||
await userEvent.click(startInput); | ||
fireEvent.change(startInput, { target: { value: 90 } }); | ||
|
||
const startHandle = screen.getByLabelText('startHandle'); | ||
expect(startHandle).toHaveStyle({ left: '79%' }); | ||
|
||
const endHandle = screen.getByLabelText('endHandle'); | ||
expect(endHandle).toHaveStyle({ left: '80%' }); | ||
|
||
await userEvent.click(endInput); | ||
fireEvent.change(endInput, { target: { value: 20 } }); | ||
|
||
expect(endHandle).toHaveStyle({ left: '80%' }); | ||
}); | ||
}); |
Oops, something went wrong.