Skip to content

Commit 5dc2f94

Browse files
committed
remove halfCheckedKeys api
1 parent 81ebf39 commit 5dc2f94

File tree

6 files changed

+45
-15
lines changed

6 files changed

+45
-15
lines changed

HISTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# History
22
---
33

4+
## 1.2.1 / 2016-04-08
5+
- remove `halfCheckedKeys` api, and change `checkedKeys` to an object on setting `checkStrictly`.
6+
47
## 1.2.0 / 2016-04-06
58
- improve performance.
69
- add `checkStrictly`/`halfCheckedKeys` api.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ see examples
6969
|expandedKeys | Controlled expand specific treeNodes | String[] | - |
7070
|autoExpandParent | whether auto expand parent treeNodes | bool | true |
7171
|defaultCheckedKeys | default checked treeNodes | String[] | [] |
72-
|checkedKeys | Controlled checked treeNodes(After setting, defaultCheckedKeys will not work). Note: parent and children nodes are associated, if the parent node's key exists, it all children node will be checked, and vice versa. | String[] | [] |
72+
|checkedKeys | Controlled checked treeNodes(After setting, defaultCheckedKeys will not work). Note: parent and children nodes are associated, if the parent node's key exists, it all children node will be checked, and vice versa. When set checkable and checkStrictly, it should be an object, which contains checked array and halfChecked array. | String[]/{checked:Array<String>,halfChecked:Array<String>} | [] |
7373
|halfCheckedKeys | when set checkStrictly, it works | String[] | [] |
7474
|checkStrictly| check node precisely, parent and children nodes are not associated| bool | false |
7575
|defaultSelectedKeys | default selected treeNodes | String[] | [] |
@@ -99,7 +99,7 @@ see examples
9999
|className | additional class to treeNode | String | '' |
100100
|disabled | whether disabled the treeNode | bool | false |
101101
|disableCheckbox | whether disable the treeNode' checkbox | bool | false |
102-
|title | tree/subTree's title | String/node | '---' |
102+
|title | tree/subTree's title | String/element | '---' |
103103
|key | it's used with tree props's (default)ExpandedKeys / (default)CheckedKeys / (default)SelectedKeys. you'd better to set it, and it must be unique in the tree's all treeNodes | String | treeNode's position |
104104
|isLeaf | whether it's leaf node | bool | false |
105105

examples/basic-controlled.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const Demo = React.createClass({
1818
expandedKeys: getFilterExpandedKeys(gData, ['0-0-0-key']),
1919
// checkedKeys: ['0-0-0-0-key', '0-0-1-0-key', '0-1-0-0-key'],
2020
checkedKeys: ['0-0-0-key'],
21+
checkStrictlyKeys: {},
2122
selectedKeys: [],
2223
};
2324
},
@@ -47,9 +48,14 @@ const Demo = React.createClass({
4748
// const { checkedNodesPositions } = extra;
4849
// const pps = filterParentPosition(checkedNodesPositions.map(i => i.pos));
4950
// console.log(checkedNodesPositions.filter(i => pps.indexOf(i.pos) > -1).map(i => i.node.key));
51+
const cks = {
52+
checked: checkedKeys.checked || checkedKeys,
53+
halfChecked: [`0-0-${parseInt(Math.random() * 3, 10)}-key`],
54+
};
5055
this.setState({
51-
checkedKeys,
52-
halfCheckedKeys: ['0-0-key'],
56+
// checkedKeys,
57+
checkStrictlyKeys: cks,
58+
// checkStrictlyKeys: checkedKeys,
5359
});
5460
},
5561
onSelect(selectedKeys, info) {
@@ -92,7 +98,7 @@ const Demo = React.createClass({
9298
<Tree checkable multiple={this.props.multiple} defaultExpandAll
9399
onExpand={this.onExpand} expandedKeys={this.state.expandedKeys}
94100
onCheck={this.onCheckStrictly}
95-
checkedKeys={this.state.checkedKeys} halfCheckedKeys={this.state.halfCheckedKeys}
101+
checkedKeys={this.state.checkStrictlyKeys}
96102
checkStrictly>
97103
{loop(gData)}
98104
</Tree>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rc-tree",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "tree ui component for react",
55
"keywords": [
66
"react",

src/Tree.jsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import classNames from 'classnames';
44
import {
55
loopAllChildren, isInclude, getOffset,
66
filterParentPosition, handleCheckState, getCheck,
7+
getStrictlyValue,
78
} from './util';
89

910
function noop() {
@@ -37,7 +38,7 @@ class Tree extends React.Component {
3738
st.expandedKeys = expandedKeys;
3839
}
3940
if (checkedKeys) {
40-
if (checkedKeys === this.props.checkedKeys) {
41+
if (nextProps.checkedKeys === this.props.checkedKeys) {
4142
this.checkedKeysChange = false;
4243
} else {
4344
this.checkedKeysChange = true;
@@ -195,7 +196,6 @@ class Tree extends React.Component {
195196
checked,
196197
};
197198

198-
// checkStrictly
199199
if (this.props.checkStrictly && ('checkedKeys' in this.props)) {
200200
if (checked && index === -1) {
201201
checkedKeys.push(key);
@@ -209,6 +209,7 @@ class Tree extends React.Component {
209209
newSt.checkedNodes.push(item);
210210
}
211211
});
212+
this.props.onCheck(getStrictlyValue(checkedKeys, this.props.checkedKeys.halfChecked), newSt);
212213
} else {
213214
if (checked && index === -1) {
214215
this.treeNodesStates[treeNode.props.pos].checked = true;
@@ -236,8 +237,8 @@ class Tree extends React.Component {
236237
checkedKeys,
237238
});
238239
}
240+
this.props.onCheck(checkedKeys, newSt);
239241
}
240-
this.props.onCheck(checkedKeys, newSt);
241242
}
242243

243244
onSelect(treeNode) {
@@ -351,6 +352,13 @@ class Tree extends React.Component {
351352
let checkedKeys = willReceiveProps ? undefined : props.defaultCheckedKeys;
352353
if ('checkedKeys' in props) {
353354
checkedKeys = props.checkedKeys || [];
355+
if (props.checkStrictly) {
356+
if (props.checkedKeys.checked) {
357+
checkedKeys = props.checkedKeys.checked;
358+
} else if (!Array.isArray(props.checkedKeys)) {
359+
checkedKeys = [];
360+
}
361+
}
354362
}
355363
return checkedKeys;
356364
}
@@ -453,15 +461,19 @@ class Tree extends React.Component {
453461
};
454462
if (props.checkable) {
455463
cloneProps.checkable = props.checkable;
456-
cloneProps.checked = (props.checkStrictly ? state.checkedKeys : this.checkedKeys).
457-
indexOf(key) !== -1;
458464
if (props.checkStrictly) {
459-
if (props.halfCheckedKeys) {
460-
cloneProps.checkPart = props.halfCheckedKeys.indexOf(key) !== -1 || false;
465+
if (state.checkedKeys) {
466+
cloneProps.checked = state.checkedKeys.indexOf(key) !== -1 || false;
467+
}
468+
if (props.checkedKeys.halfChecked) {
469+
cloneProps.checkPart = props.checkedKeys.halfChecked.indexOf(key) !== -1 || false;
461470
} else {
462471
cloneProps.checkPart = false;
463472
}
464473
} else {
474+
if (this.checkedKeys) {
475+
cloneProps.checked = this.checkedKeys.indexOf(key) !== -1 || false;
476+
}
465477
cloneProps.checkPart = this.checkPartKeys.indexOf(key) !== -1;
466478
}
467479

@@ -554,8 +566,10 @@ Tree.propTypes = {
554566
defaultExpandedKeys: PropTypes.arrayOf(PropTypes.string),
555567
expandedKeys: PropTypes.arrayOf(PropTypes.string),
556568
defaultCheckedKeys: PropTypes.arrayOf(PropTypes.string),
557-
checkedKeys: PropTypes.arrayOf(PropTypes.string),
558-
halfCheckedKeys: PropTypes.arrayOf(PropTypes.string),
569+
checkedKeys: PropTypes.oneOfType([
570+
PropTypes.arrayOf(PropTypes.string),
571+
PropTypes.object,
572+
]),
559573
defaultSelectedKeys: PropTypes.arrayOf(PropTypes.string),
560574
selectedKeys: PropTypes.arrayOf(PropTypes.string),
561575
onExpand: PropTypes.func,

src/util.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,3 +246,10 @@ export function getCheck(treeNodesStates) {
246246
checkPartKeys, checkedKeys, checkedNodes, checkedNodesPositions, treeNodesStates,
247247
};
248248
}
249+
250+
export function getStrictlyValue(checkedKeys, halfChecked) {
251+
if (halfChecked) {
252+
return { checked: checkedKeys, halfChecked };
253+
}
254+
return checkedKeys;
255+
}

0 commit comments

Comments
 (0)