Skip to content

Commit

Permalink
feat(Tree): support rtl
Browse files Browse the repository at this point in the history
  • Loading branch information
myronliu347 committed Feb 19, 2019
1 parent 1ed6e25 commit 37fa931
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/tree/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,5 @@
}
}
}

@import './rtl.scss';
34 changes: 34 additions & 0 deletions src/tree/rtl.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#{$tree-prefix}[dir='rtl'] {
#{$tree-prefix} {
&-switcher {
margin-left: $tree-switch-margint-right;
margin-right: 0;
}
&-switcher.#{$css-prefix}noop-line-noroot {
border-left: none;
border-right: $tree-switch-border-width $line-solid $tree-line-color;
}
&-right-angle {
left: auto;
right: - ($tree-child-indent-right + $tree-line-width + $tree-switch-border-width);
border-left: none;
border-right: $tree-line;
}
}

&.#{$css-prefix}show-line #{$tree-prefix}-node #{$tree-prefix}-node:not(:last-child) {
margin-left: 0;
margin-right: $tree-child-indent-left;
border-left: none;
border-right: $tree-line;
padding-left: 0;
padding-right: $tree-child-indent-right;
}

&.#{$css-prefix}node-indent {
#{$tree-prefix}-node #{$tree-prefix}-node {
margin-left: 0;
margin-right: $tree-child-indent;
}
}
}
11 changes: 10 additions & 1 deletion src/tree/view/tree-node.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class TreeNode extends Component {
static propTypes = {
_key: PropTypes.string,
prefix: PropTypes.string,
rtl: PropTypes.bool,
className: PropTypes.string,
/**
* 树节点
Expand Down Expand Up @@ -73,6 +74,7 @@ export default class TreeNode extends Component {

static defaultProps = {
label: '---',
rtl: false,
disabled: false,
checkboxDisabled: false,
isLeaf: false
Expand Down Expand Up @@ -418,6 +420,7 @@ export default class TreeNode extends Component {
render() {
const {
prefix,
rtl,
className,
children,
isLeaf,
Expand Down Expand Up @@ -459,7 +462,9 @@ export default class TreeNode extends Component {
const defaultPaddingLeft = typeof isNodeBlock === 'object' ? parseInt(isNodeBlock.defaultPaddingLeft || 0) : 0;
const indent = typeof isNodeBlock === 'object' ? parseInt(isNodeBlock.indent || 24) : 24;
const level = pos.split('-').length - 2;
const innerStyle = isNodeBlock ? { paddingLeft: `${indent * level + defaultPaddingLeft}px` } : null;
const paddingLeftProp = rtl ? 'paddingRight' : 'paddingLeft';

const innerStyle = isNodeBlock ? { [paddingLeftProp]: `${indent * level + defaultPaddingLeft}px` } : null;
const innerProps = {
className: innerClassName,
style: innerStyle,
Expand All @@ -476,6 +481,10 @@ export default class TreeNode extends Component {

innerProps.tabIndex = root.tabbableKey === _key ? '0' : '-1';

if (rtl) {
others.dir = 'rtl';
}

return (
<li className={newClassName} {...others}>
<div
Expand Down
13 changes: 11 additions & 2 deletions src/tree/view/tree.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const { pickOthers, isPlainObject } = obj;
export default class Tree extends Component {
static propTypes = {
prefix: PropTypes.string,
rtl: PropTypes.bool,
pure: PropTypes.bool,
className: PropTypes.string,
/**
Expand Down Expand Up @@ -234,6 +235,7 @@ export default class Tree extends Component {

static defaultProps = {
prefix: 'next-',
rtl: false,
pure: false,
showLine: false,
selectable: true,
Expand Down Expand Up @@ -869,6 +871,7 @@ export default class Tree extends Component {
}

renderByDataSource() {
const { rtl } = this.props;
const loop = (data, prefix = '0') => {
return data.map((item, index) => {
const pos = `${prefix}-${index}`;
Expand All @@ -881,7 +884,7 @@ export default class Tree extends Component {
if (children && children.length) {
props.children = loop(children, pos);
}
const node = <TreeNode key={key} {...props} />;
const node = <TreeNode rtl={rtl} key={key} {...props} />;
this._k2n[key].node = node;
return node;
});
Expand All @@ -891,6 +894,7 @@ export default class Tree extends Component {
}

renderByChildren() {
const { rtl } = this.props;
const loop = (children, prefix = '0') => {
return Children.map(children, (child, index) => {
const pos = `${prefix}-${index}`;
Expand All @@ -901,6 +905,7 @@ export default class Tree extends Component {
}

props._key = key;
props.rtl = rtl;

const node = cloneElement(child, props);
this._k2n[key].node = node;
Expand All @@ -912,9 +917,13 @@ export default class Tree extends Component {
}

render() {
const { prefix, className, dataSource, showLine, isNodeBlock, isLabelBlock } = this.props;
const { prefix, rtl, className, dataSource, showLine, isNodeBlock, isLabelBlock } = this.props;
const others = pickOthers(Object.keys(Tree.propTypes), this.props);

if (rtl) {
others.dir = 'rtl';
}

const newClassName = cx({
[`${prefix}tree`]: true,
[`${prefix}label-block`]: isLabelBlock,
Expand Down

0 comments on commit 37fa931

Please sign in to comment.