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

[Slider] Improve RTL support #20851

Merged
merged 4 commits into from
May 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 7 additions & 3 deletions packages/material-ui/src/Slider/Slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,17 @@ const Slider = React.forwardRef(function Slider(props, ref) {
setOpen(-1);
});

const isRtl = theme.direction === 'rtl';

const handleKeyDown = useEventCallback((event) => {
const index = Number(event.currentTarget.getAttribute('data-index'));
const value = values[index];
const tenPercents = (max - min) / 10;
const marksValues = marks.map((mark) => mark.value);
const marksIndex = marksValues.indexOf(value);
let newValue;
const ArrowRight = isRtl ? 'ArrowLeft' : 'ArrowRight';
joshwooding marked this conversation as resolved.
Show resolved Hide resolved
const ArrowLeft = isRtl ? 'ArrowRight' : 'ArrowLeft';
joshwooding marked this conversation as resolved.
Show resolved Hide resolved

switch (event.key) {
case 'Home':
Expand All @@ -450,15 +454,15 @@ const Slider = React.forwardRef(function Slider(props, ref) {
newValue = value - tenPercents;
}
break;
case 'ArrowRight':
case ArrowRight:
joshwooding marked this conversation as resolved.
Show resolved Hide resolved
case 'ArrowUp':
if (step) {
newValue = value + step;
} else {
newValue = marksValues[marksIndex + 1] || marksValues[marksValues.length - 1];
}
break;
case 'ArrowLeft':
case ArrowLeft:
joshwooding marked this conversation as resolved.
Show resolved Hide resolved
case 'ArrowDown':
if (step) {
newValue = value - step;
Expand Down Expand Up @@ -503,7 +507,7 @@ const Slider = React.forwardRef(function Slider(props, ref) {

const previousIndex = React.useRef();
let axis = orientation;
if (theme.direction === 'rtl' && orientation === 'horizontal') {
if (isRtl && orientation === 'horizontal') {
axis += '-reverse';
}

Expand Down
19 changes: 19 additions & 0 deletions packages/material-ui/src/Slider/Slider.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,25 @@ describe('<Slider />', () => {
fireEvent.keyDown(document.activeElement, moveLeftEvent);
expect(thumb).to.have.attribute('aria-valuenow', '-3e-8');
});

it('should handle RTL', () => {
const { getByRole } = render(
<ThemeProvider
theme={createMuiTheme({
direction: 'rtl',
})}
>
<Slider defaultValue={30} />
</ThemeProvider>,
);
const thumb = getByRole('slider');
thumb.focus();

fireEvent.keyDown(document.activeElement, moveLeftEvent);
expect(thumb).to.have.attribute('aria-valuenow', '31');
fireEvent.keyDown(document.activeElement, moveRightEvent);
expect(thumb).to.have.attribute('aria-valuenow', '30');
});
});

describe('prop: valueLabelDisplay', () => {
Expand Down