Skip to content

Commit

Permalink
[TreeView] Improve RTL support (#18855)
Browse files Browse the repository at this point in the history
  • Loading branch information
eladex authored and oliviertassinari committed Dec 17, 2019
1 parent 162515b commit 6cbf297
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions packages/material-ui-lab/src/TreeItem/TreeItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import clsx from 'clsx';
import PropTypes from 'prop-types';
import Typography from '@material-ui/core/Typography';
import Collapse from '@material-ui/core/Collapse';
import { withStyles } from '@material-ui/core/styles';
import { withStyles, useTheme } from '@material-ui/core/styles';
import { useForkRef } from '@material-ui/core/utils';
import TreeViewContext from '../TreeView/TreeViewContext';

Expand Down Expand Up @@ -102,6 +102,7 @@ const TreeItem = React.forwardRef(function TreeItem(props, ref) {
const focused = isFocused ? isFocused(nodeId) : false;
const tabable = isTabable ? isTabable(nodeId) : false;
const icons = contextIcons || {};
const theme = useTheme();

if (!icon) {
if (expandable) {
Expand Down Expand Up @@ -146,6 +147,20 @@ const TreeItem = React.forwardRef(function TreeItem(props, ref) {
return false;
};

const handleNextArrow = event => {
if (expandable) {
if (expanded) {
focusNextNode(nodeId);
} else {
toggle(event);
}
}
};

const handlePreviousArrow = event => {
handleLeftArrow(nodeId, event);
};

const handleKeyDown = event => {
let flag = false;
const key = event.key;
Expand Down Expand Up @@ -178,17 +193,20 @@ const TreeItem = React.forwardRef(function TreeItem(props, ref) {
flag = true;
break;
case 'ArrowRight':
if (expandable) {
if (expanded) {
focusNextNode(nodeId);
} else {
toggle(event);
}
if (theme.direction === 'rtl') {
handlePreviousArrow(event);
} else {
handleNextArrow(event);
flag = true;
}
flag = true;
break;
case 'ArrowLeft':
handleLeftArrow(nodeId, event);
if (theme.direction === 'rtl') {
handleNextArrow(event);
flag = true;
} else {
handlePreviousArrow(event);
}
break;
case 'Home':
focusFirstNode();
Expand Down

0 comments on commit 6cbf297

Please sign in to comment.