Skip to content

Commit

Permalink
[Pagination] Add RTL support (#20247)
Browse files Browse the repository at this point in the history
  • Loading branch information
HenryLie authored Mar 23, 2020
1 parent d12a999 commit 2f47739
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
25 changes: 25 additions & 0 deletions packages/material-ui-lab/src/Pagination/Pagination.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createMount, getClasses } from '@material-ui/core/test-utils';
import describeConformance from '@material-ui/core/test-utils/describeConformance';
import { createClientRender } from 'test/utils/createClientRender';
import Pagination from './Pagination';
import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles';

describe('<Pagination />', () => {
let classes;
Expand Down Expand Up @@ -51,4 +52,28 @@ describe('<Pagination />', () => {

expect(handleChange.callCount).to.equal(1);
});

it('renders controls with correct order in rtl theme', () => {
const { getAllByRole } = render(
<ThemeProvider
theme={createMuiTheme({
direction: 'rtl',
})}
>
<Pagination count={5} page={3} showFirstButton showLastButton />
</ThemeProvider>,
);

const buttons = getAllByRole('button');

expect(buttons[0].querySelector('svg')).to.have.attribute('data-mui-test', 'LastPageIcon');
expect(buttons[1].querySelector('svg')).to.have.attribute('data-mui-test', 'NavigateNextIcon');
expect(buttons[2].textContent).to.equal('1');
expect(buttons[6].textContent).to.equal('5');
expect(buttons[7].querySelector('svg')).to.have.attribute(
'data-mui-test',
'NavigateBeforeIcon',
);
expect(buttons[8].querySelector('svg')).to.have.attribute('data-mui-test', 'FirstPageIcon');
});
});
25 changes: 20 additions & 5 deletions packages/material-ui-lab/src/PaginationItem/PaginationItem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { fade, withStyles } from '@material-ui/core/styles';
import { fade, useTheme, withStyles } from '@material-ui/core/styles';
import ButtonBase from '@material-ui/core/ButtonBase';
import FirstPageIcon from '../internal/svg-icons/FirstPage';
import LastPageIcon from '../internal/svg-icons/LastPage';
Expand Down Expand Up @@ -208,6 +208,24 @@ const PaginationItem = React.forwardRef(function PaginationItem(props, ref) {
...other
} = props;

const theme = useTheme();

const normalizedIcons =
theme.direction === 'rtl'
? {
previous: NavigateNextIcon,
next: NavigateBeforeIcon,
last: FirstPageIcon,
first: LastPageIcon,
}
: {
previous: NavigateBeforeIcon,
next: NavigateNextIcon,
first: FirstPageIcon,
last: LastPageIcon,
};
const Icon = normalizedIcons[type];

return type === 'start-ellipsis' || type === 'end-ellipsis' ? (
<div
ref={ref}
Expand Down Expand Up @@ -240,10 +258,7 @@ const PaginationItem = React.forwardRef(function PaginationItem(props, ref) {
{...other}
>
{type === 'page' && page}
{type === 'previous' && <NavigateBeforeIcon className={classes.icon} />}
{type === 'next' && <NavigateNextIcon className={classes.icon} />}
{type === 'first' && <FirstPageIcon className={classes.icon} />}
{type === 'last' && <LastPageIcon className={classes.icon} />}
{Icon ? <Icon className={classes.icon} /> : null}
</ButtonBase>
);
});
Expand Down

0 comments on commit 2f47739

Please sign in to comment.