Skip to content

Commit

Permalink
feat: 支持tree、treeSelect组件单向联动,支持单独禁用treeNode的checkbox (#30)
Browse files Browse the repository at this point in the history
* feat: 支持tree、treeSelect组件单向联动,支持单独禁用treeNode的checkbox

* doc: update
  • Loading branch information
hangaoke1 authored Nov 30, 2020
1 parent fa3b9c9 commit be5ad43
Show file tree
Hide file tree
Showing 10 changed files with 713 additions and 20 deletions.
108 changes: 108 additions & 0 deletions site/docs/zh-CN/tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,112 @@
```
:::

## 受控操作示例,单向绑定

:::demo 受控操作示例,单向绑定

```js

constructor(props){
super(props);
this.treeData = [{
title: '0-0',
key: '0-0',
children: [{
title: '0-0-0',
key: '0-0-0',
children: [
{ title: '0-0-0-0', key: '0-0-0-0' },
{ title: '0-0-0-1', key: '0-0-0-1' },
{ title: '0-0-0-2', key: '0-0-0-2' },
],
}, {
title: '0-0-1',
key: '0-0-1',
children: [
{ title: '0-0-1-0', key: '0-0-1-0' },
{ title: '0-0-1-1', key: '0-0-1-1' },
{ title: '0-0-1-2', key: '0-0-1-2' },
],
}, {
title: '0-0-2',
key: '0-0-2',
}],
}, {
title: '0-1',
key: '0-1',
children: [
{ title: '0-1-0-0', key: '0-1-0-0' },
{ title: '0-1-0-1', key: '0-1-0-1' },
{ title: '0-1-0-2', key: '0-1-0-2' },
],
}, {
title: '0-2',
key: '0-2',
}];
this.state = {
expandedKeys: ['0-0-0', '0-0-1'],
autoExpandParent: true,
checkedKeys: ['0-0-0'],
selectedKeys: [],
}
}

onExpand = (expandedKeys) => {
console.log('onExpand', expandedKeys);
// if not set autoExpandParent to false, if children expanded, parent can not collapse.
// or, you can remove all expanded children keys.
this.setState({
expandedKeys,
autoExpandParent: false,
});
}

onCheck = (checkedKeys) => {
console.log('onCheck', checkedKeys);
this.setState({ checkedKeys });
}

onSelect = (selectedKeys, info) => {
console.log('onSelect', info);
this.setState({ selectedKeys });
}

renderTreeNodes = (data) => {
const TreeNode = Tree.TreeNode;
return data.map((item) => {
if (item.children) {
return (
<TreeNode title={item.title} key={item.key} dataRef={item}>
{this.renderTreeNodes(item.children)}
</TreeNode>
);
}
return <TreeNode {...item} />;
});
}

render() {
const TreeNode = Tree.TreeNode;
return (
<Tree
checkable
onExpand={this.onExpand}
expandedKeys={this.state.expandedKeys}
autoExpandParent={this.state.autoExpandParent}
onCheck={this.onCheck}
checkedKeys={this.state.checkedKeys}
onSelect={this.onSelect}
selectedKeys={this.state.selectedKeys}
checkType="countDown"
>
{this.renderTreeNodes(this.treeData)}
</Tree>
);
}
```
:::


## 拖动示例

Expand Down Expand Up @@ -740,6 +846,7 @@
| checkable | 节点前添加 Checkbox 复选框 | Boolean | false |
| checkedKeys | (受控)选中复选框的树节点(注意:父子节点有关联,如果传入父节点key,则子节点自动选中;相应当子节点key都传入,父节点也自动选中。当设置`checkable``checkStrictly`,它是一个有`checked``halfChecked`属性的对象,并且父子节点的选中与否不再关联 | Array< String > \| {checked: Array< String >, halfChecked: Array< String >} | [] |
| checkStrictly | checkable状态下节点选择完全受控(父子节点选中状态不再关联) | Boolean | false |
| checkType | 父子节点联动类型,可选择`countDown`,可以让联动变成单向,父节点选中时,默认选中所有子节点,子节点全部选中时,不会选中父节点 | String | - |
| className | 容器类名 | String | - |
| defaultCheckedKeys | 默认选中复选框的树节点 | Array< String > | [] |
| defaultExpandAll | 默认展开所有树节点 | Boolean | false |
Expand Down Expand Up @@ -777,6 +884,7 @@

| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| checkable | 当树为 checkable 时,设置独立节点是否展示 Checkbox,默认继承Tree当中的checkable属性 | Boolean | - |
| disableCheckbox | 禁掉 checkbox | Boolean | false |
| disabled | 禁掉响应 | Boolean | false |
| icon | 自定义当前节点 title 前的图标。可接收组件,props 为当前节点的 props | ReactNode \| (props) => ReactNode | - |
Expand Down
119 changes: 119 additions & 0 deletions site/docs/zh-CN/treeSelect.md
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,123 @@
```
:::

## 多选,单向联动

:::demo 使用勾选框实现多选功能,且单向联动。


```js

state = {
value: ['0-0-0'],
}

onConfirm = (value, infoList, extra) => {
console.log('选中节点:', value);
console.log('详细信息:', infoList);
console.log('额外信息:', extra);
this.setState({ value });
}

onCancel = (value) => {
this.setState({ value });
}

onSelect = (value, valueList, infoList, extra) => {
console.log('选中:', value);
console.log('已选择:', valueList);
}

render() {
const treeData = [{
title: 'Node1',
value: '0-0',
key: '0-0a',
children: [{
title: 'CNode1',
value: '0-0-0',
key: '0-0a-0',
}, {
title: 'CNode2-long-title-CNode2-long-title-CNode2-long-title-CNode2-long-title',
value: '0-0-1',
key: '0-0a-1',
}],
}, {
title: 'Node2',
value: '0-1',
key: '0-1a',
children: [{
title: 'CNode3',
value: '0-1-0',
key: '0-1a-0',
}, {
title: 'CNode4',
value: '0-1-1',
key: '0-1a-1',
disabled: true
}, {
title: 'CNode5',
value: '0-1-2',
key: '0-1a-2',
}, {
title: 'CNode6',
value: '0-1-3',
key: '0-1a-3',
}, {
title: 'CNode7',
value: '0-1-4',
key: '0-1a-4',
}, {
title: 'CNode8',
value: '0-1-5',
key: '0-1a-5',
}, {
title: 'CNode9',
value: '0-1-6',
key: '0-1a-6',
}, {
title: 'CNode10',
value: '0-1-7',
key: '0-1a-7',
}, {
title: 'CNode11',
value: '0-1-8',
key: '0-1a-8',
}, {
title: 'CNode12',
value: '0-1-9',
key: '0-1a-9',
}],
}];
const tProps = {
showSearch: true,
treeData,
treeDefaultExpandedKeys: ['0-0a', '0-1a'],
value: this.state.value,
onConfirm: this.onConfirm,
onCancel: this.onCancel,
onSelect: this.onSelect,
treeCheckable: true,
treeCheckType: 'countDown',
style: { width: 300 },
dropdownStyle: { width: 300 }
};
const tPropsDisabled = {
disabled: true,
treeCheckable: true,
style: { width: 300, marginTop: '10px' }
};
return (
<div>
<TreeSelect {...tProps} />
<br/>
<TreeSelect {...tPropsDisabled} />
</div>
);
}
```
:::


## 后端搜索

Expand Down Expand Up @@ -1107,6 +1224,7 @@
| style | 选择框的样式 | Object | - |
| tagWidth | 标签的固定宽度,不能超过选择框的宽度,多选时有效 | Number | 100 |
| treeCheckable | 显示 checkbox | Boolean | false |
| treeCheckType | 父子节点联动类型,可选择`countDown`,可以让联动变成单向,父节点选中时,默认选中所有子节点,子节点全部选中时,不会选中父节点 | String | - |
| treeData | treeNodes 数据,如果设置则不需要手动构造 TreeNode 节点。treeData 中 value 必须设置,且其值需在整个树范围内唯一;key 可选,未设置时取 value 的值。 | Array< {value, title, [children, key, icon, disabled, disableCheckbox, selectable, isLeaf]} > | [] |
| treeDefaultExpandAll | 默认展开所有树节点 | Boolean | false |
| treeDefaultExpandedKeys | 默认展开的树节点 | Array | [] |
Expand All @@ -1129,6 +1247,7 @@
| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| checkable | 当树为 checkable 时,设置独立节点是否展示 Checkbox,默认继承Tree当中的checkable属性 | Boolean | - |
| disableCheckbox | 禁掉 checkbox | Boolean | false |
| disabled | 是否禁用 | Boolean | false |
| icon | 自定义当前节点 title 前的图标。可接收组件,props 为当前节点的 props。Tree props 中 `showIcon` 为 true 时有效。 | ReactNode \| (props) => ReactNode | - |
Expand Down
Loading

0 comments on commit be5ad43

Please sign in to comment.