Skip to content

Commit

Permalink
FIX: TreeDropdownField wouldn't render children when using TreeBaseID (
Browse files Browse the repository at this point in the history
…fixes #954)
  • Loading branch information
kinglozzer committed Oct 22, 2020
1 parent 0e4cda1 commit 397fa06
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions client/src/components/TreeDropdownField/TreeDropdownField.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class TreeDropdownField extends Component {
}
// No more path means this is the complete tree
let node = this.props.tree;

// eslint-disable-next-line no-restricted-syntax
for (const next of path) {
if (!node.children) {
Expand Down Expand Up @@ -182,7 +183,7 @@ class TreeDropdownField extends Component {
}

getPath(id) {
const treePath = this.props.findTreePath(this.props.tree, id);
const treePath = this.props.findTreePath(this.props.tree, id, this.props.data.treeBaseId);
const breadcrumbs = this.getBreadcrumbs(treePath);

return breadcrumbs
Expand All @@ -205,7 +206,7 @@ class TreeDropdownField extends Component {
let newPath = [];
if (!this.props.data.multiple && this.props.value) {
// Get path of current node
newPath = this.props.findTreePath(treeData, this.props.value);
newPath = this.props.findTreePath(treeData, this.props.value, this.props.data.treeBaseId);
if (newPath) {
// Revert one level to show parent
newPath.pop();
Expand Down Expand Up @@ -439,8 +440,9 @@ class TreeDropdownField extends Component {
if (this.hasSearch()) {
return;
}

// Find parent path
let path = this.props.findTreePath(this.props.tree, id);
let path = this.props.findTreePath(this.props.tree, id, this.props.data.treeBaseId);
if (!path) {
// Edge case: Path hasn't been loaded yet,
// so append to current path
Expand Down Expand Up @@ -753,6 +755,7 @@ TreeDropdownField.propTypes = {
showSearch: PropTypes.bool,
multiple: PropTypes.bool,
showSelectedPath: PropTypes.bool,
treeBaseId: PropTypes.number
}),
onLoadingError: PropTypes.func,
search: PropTypes.string,
Expand Down
6 changes: 4 additions & 2 deletions client/src/components/TreeDropdownField/treeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const findTreeByPath = (tree, path) => {
if (path.length === 0) {
return tree;
}

const subPath = path.slice(0);
const nextID = subPath.shift();
const subTree = tree.children.find((nextSubTree) => (nextSubTree.id === nextID));
Expand Down Expand Up @@ -60,9 +61,10 @@ const findTreeByID = (tree, id) => {
*
* @param {Object} tree - Tree to search
* @param {*} id - id property of node to find path for
* @param {*} treeBaseId - tree root id, may be non-zero if a custom root node has been specified
* @return {Array} - The path to this node, or null if not found
*/
export const findTreePath = (tree, id) => {
export const findTreePath = (tree, id, treeBaseId = 0) => {
// root node
if (!id) {
return [];
Expand All @@ -85,7 +87,7 @@ export const findTreePath = (tree, id) => {
// Node found in subtree, shift this id and return
if (childPath !== null) {
// Don't add root ID
if (tree.id) {
if (tree.id && (tree.id !== treeBaseId)) {
childPath.unshift(tree.id);
}
return childPath;
Expand Down

0 comments on commit 397fa06

Please sign in to comment.