Skip to content

feat: add hide node property #506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ see examples
| isLeaf | whether it's leaf node | bool | false |
| icon | customize icon. When you pass component, whose render will receive full TreeNode props as component props | element/Function(props) | - |
| switcherIcon | specific the switcher icon. | ReactNode / (props: TreeNodeAttribute) => ReactNode | - |
| hidden | whether hide the treeNode(about this property, parent and children nodes are not associated). | bool | false |

## Note

Expand Down
3 changes: 3 additions & 0 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
border-left: none;
}
}
&.hide-node {
display: none;
}
&.filter-node {
> .@{treePrefixCls}-node-content-wrapper {
color: #a60000 !important;
Expand Down
9 changes: 5 additions & 4 deletions src/NodeList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ const NodeList = React.forwardRef<NodeListRef, NodeListProps<any>>((props, ref)
} = props;

// =============================== Ref ================================
const showingData = data.filter(item => !item.hidden);
const listRef = React.useRef<ListRef>(null);
const indentMeasurerRef = React.useRef<HTMLDivElement>(null);
React.useImperativeHandle(ref, () => ({
Expand All @@ -179,7 +180,7 @@ const NodeList = React.forwardRef<NodeListRef, NodeListProps<any>>((props, ref)
// ============================== Motion ==============================
const [prevExpandedKeys, setPrevExpandedKeys] = React.useState(expandedKeys);
const [prevData, setPrevData] = React.useState(data);
const [transitionData, setTransitionData] = React.useState(data);
const [transitionData, setTransitionData] = React.useState(showingData);
const [transitionRange, setTransitionRange] = React.useState([]);
const [motionType, setMotionType] = React.useState<'show' | 'hide' | null>(null);

Expand All @@ -191,7 +192,7 @@ const NodeList = React.forwardRef<NodeListRef, NodeListProps<any>>((props, ref)
const latestData = dataRef.current;

setPrevData(latestData);
setTransitionData(latestData);
setTransitionData(showingData);
setTransitionRange([]);
setMotionType(null);

Expand All @@ -215,7 +216,7 @@ const NodeList = React.forwardRef<NodeListRef, NodeListProps<any>>((props, ref)
itemHeight,
);

const newTransitionData: FlattenNode[] = prevData.slice();
const newTransitionData: FlattenNode[] = prevData.slice().filter(item => !item.hidden);
newTransitionData.splice(keyIndex + 1, 0, MotionFlattenData);

setTransitionData(newTransitionData);
Expand All @@ -231,7 +232,7 @@ const NodeList = React.forwardRef<NodeListRef, NodeListProps<any>>((props, ref)
itemHeight,
);

const newTransitionData: FlattenNode[] = data.slice();
const newTransitionData: FlattenNode[] = data.slice().filter(item => !item.hidden);
newTransitionData.splice(keyIndex + 1, 0, MotionFlattenData);

setTransitionData(newTransitionData);
Expand Down
3 changes: 3 additions & 0 deletions src/TreeNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface TreeNodeProps<TreeDataType extends BasicDataNode = DataNode> {
icon?: IconType;
switcherIcon?: IconType;
children?: React.ReactNode;
hidden?:boolean;
}

export interface InternalTreeNodeProps extends TreeNodeProps {
Expand Down Expand Up @@ -536,6 +537,7 @@ class InternalTreeNode extends React.Component<InternalTreeNodeProps, TreeNodeSt
selected,
checked,
halfChecked,
hidden,
loading,
domRef,
active,
Expand Down Expand Up @@ -586,6 +588,7 @@ class InternalTreeNode extends React.Component<InternalTreeNodeProps, TreeNodeSt
'drag-over-gap-top': !disabled && dragOverGapTop,
'drag-over-gap-bottom': !disabled && dragOverGapBottom,
'filter-node': filterTreeNode && filterTreeNode(convertNodePropsToEventData(this.props)),
'hide-node' : hidden
})}
style={style}
// Draggable config
Expand Down
2 changes: 2 additions & 0 deletions src/interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ export interface BasicDataNode {
isLeaf?: boolean;
selectable?: boolean;
switcherIcon?: IconType;
hidden: boolean;

/** Set style of TreeNode. This is not recommend if you don't have any force requirement */
className?: string;
style?: React.CSSProperties;

}

/** Provide a wrap type define for developer to wrap with customize fieldNames data type */
Expand Down
21 changes: 17 additions & 4 deletions src/utils/treeUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,26 @@ export function flattenTreeData<TreeDataType extends BasicDataNode = DataNode>(

const expandedKeySet = new Set(expandedKeys === true ? [] : expandedKeys);
const flattenList: FlattenNode<TreeDataType>[] = [];

function dig(
list: TreeDataType[],
parent: FlattenNode<TreeDataType> = null,
): FlattenNode<TreeDataType>[] {
// Removed the hidden node
const shouldShowList = list.filter(item => !item.hidden).map((treeNode, index) => {
const shouldShowNode = {
...treeNode,
shouldShowIndex: index
}
return shouldShowNode;
});
return list.map((treeNode, index) => {
const pos: string = getPosition(parent ? parent.pos : '0', index);
const mergedKey = getKey(treeNode[fieldKey], pos);

const mapNode = shouldShowList.filter(item => item.key === treeNode.key);
// Get index in the list that remove the hidden nodes
const shouldShowIndex = mapNode.length > 0 ? mapNode[0].shouldShowIndex: null;

// Pick matched title in field title list
let mergedTitle: React.ReactNode;
for (let i = 0; i < fieldTitles.length; i += 1) {
Expand All @@ -136,7 +147,6 @@ export function flattenTreeData<TreeDataType extends BasicDataNode = DataNode>(
break;
}
}

// Add FlattenDataNode into list
const flattenNode: FlattenNode<TreeDataType> = {
...omit(treeNode, [...fieldTitles, fieldKey, fieldChildren] as any),
Expand All @@ -146,8 +156,11 @@ export function flattenTreeData<TreeDataType extends BasicDataNode = DataNode>(
pos,
children: null,
data: treeNode,
isStart: [...(parent ? parent.isStart : []), index === 0],
isEnd: [...(parent ? parent.isEnd : []), index === list.length - 1],
// isStart: [...(parent ? parent.isStart : []), index === 0],
// isEnd: [...(parent ? parent.isEnd : []), index === list.length - 1],
// Modify judging condition of two variables(isStart and isEnd)
isStart: [...(parent ? parent.isStart : []), shouldShowIndex === 0],
isEnd: [...(parent ? parent.isEnd : []), shouldShowIndex === shouldShowList.length - 1],
};

flattenList.push(flattenNode);
Expand Down