import React, { Component } from 'react'; import { isLeaf } from 'wdk-client/Utils/TreeUtils'; import IndeterminateCheckbox, { IndeterminateCheckboxProps } from 'wdk-client/Components/InputControls/IndeterminateCheckbox'; const visibleElement = {display: ""}; const hiddenElement = {display: "none"}; type TreeRadioProps = { name: string; checked: boolean; value: string; node: T; onChange: (node: T, checked: boolean) => void; className: string; } class TreeRadio extends Component> { handleClick() { let { checked, onChange, node } = this.props; if (!checked) { onChange(node, false); } } render() { let { name, checked, value, className } = this.props; return ( ); } } type NodeState = { isSelected: boolean; isVisible: boolean; isIndeterminate?: boolean; isExpanded?: boolean; } // export type CheckboxFactory = (state?: Partial>) => React.ReactNode; export type CustomCheckboxes = {[index: string]: React.ComponentType>>}; type Props = { node: T; name: string; path: number[]; listClassName: string; getNodeState: (node: T) => NodeState; isSelectable: boolean; isMultiPick: boolean; isActiveSearch: boolean; toggleExpansion: (node: T) => void; toggleSelection: (node: T, checked: boolean) => void; getNodeId: (node: T) => string; getNodeChildren: (node: T) => T[]; renderNode: (node: T, path?: number[]) => React.ReactNode; customCheckboxes?: CustomCheckboxes; shouldExpandOnClick: boolean; } class CheckboxTreeNode extends Component> { toggleExpansion = () => { this.props.toggleExpansion(this.props.node); } shouldComponentUpdate(nextProps: Props) { return (nextProps.node !== this.props.node); } render() { let { name, node, path, listClassName, getNodeState, isSelectable, isMultiPick, isActiveSearch, toggleSelection, toggleExpansion, getNodeId, getNodeChildren, renderNode, customCheckboxes, shouldExpandOnClick } = this.props; // We have to apply the generic type `T` to these child components. This is // a known TypeScript issue and will likely be solved in the future. const IndeterminateCheckboxT = IndeterminateCheckbox as new () => IndeterminateCheckbox; const TreeRadioT = TreeRadio as new () => TreeRadio; let { isSelected, isIndeterminate, isVisible, isExpanded } = getNodeState(node); let isLeafNode = isLeaf(node, getNodeChildren); let nodeVisibilityCss = isVisible ? visibleElement : hiddenElement; let childrenVisibilityCss = isExpanded ? visibleElement : hiddenElement; let nodeType = isLeafNode ? "leaf" : isExpanded ? "expanded" : "collapsed"; let classNames = 'wdk-CheckboxTreeItem wdk-CheckboxTreeItem__' + nodeType + (isSelectable ? ' wdk-CheckboxTreeItem__selectable' : ' wdk-CheckboxTreeItem__not-selectable'); let inputName = isLeafNode ? name : ''; let nodeId = getNodeId(node); const nodeElement = renderNode(node, path); const commonInputProps = { className: "wdk-CheckboxTreeCheckbox", name: inputName, checked: isSelected, node, value: nodeId, }; const checkboxProps: IndeterminateCheckboxProps = {...commonInputProps, indeterminate: !!isIndeterminate, toggleCheckbox: toggleSelection}; const CustomCheckbox = (customCheckboxes && (nodeId in customCheckboxes)) ? customCheckboxes[nodeId] : undefined; return (
  • {isLeafNode || isActiveSearch ? ( ) : ( )} {!isSelectable || (!isMultiPick && !isLeafNode) ? (
    {nodeElement}
    ) : ( )}
    { !isLeafNode && isVisible && isExpanded &&
      {getNodeChildren(node).map((child, index) => )}
    }
  • ); } } export default CheckboxTreeNode;