2021-09-17 09:08:35 +00:00
|
|
|
import React, {
|
2022-03-11 05:35:05 +00:00
|
|
|
ChangeEvent,
|
2021-09-17 09:08:35 +00:00
|
|
|
ReactNode,
|
|
|
|
|
useCallback,
|
|
|
|
|
useEffect,
|
2022-03-11 05:35:05 +00:00
|
|
|
useMemo,
|
2021-09-17 09:08:35 +00:00
|
|
|
useRef,
|
|
|
|
|
useState,
|
|
|
|
|
} from "react";
|
|
|
|
|
import TreeSelect, { TreeSelectProps as SelectProps } from "rc-tree-select";
|
|
|
|
|
import {
|
|
|
|
|
TreeSelectContainer,
|
|
|
|
|
DropdownStyles,
|
2021-11-16 09:27:38 +00:00
|
|
|
StyledIcon,
|
2021-09-17 09:08:35 +00:00
|
|
|
StyledLabel,
|
|
|
|
|
TextLabelWrapper,
|
|
|
|
|
} from "./index.styled";
|
|
|
|
|
import "rc-tree-select/assets/index.less";
|
|
|
|
|
import { DefaultValueType } from "rc-tree-select/lib/interface";
|
|
|
|
|
import { TreeNodeProps } from "rc-tree-select/lib/TreeNode";
|
|
|
|
|
import {
|
|
|
|
|
CANVAS_CLASSNAME,
|
|
|
|
|
MODAL_PORTAL_CLASSNAME,
|
|
|
|
|
TextSize,
|
|
|
|
|
} from "constants/WidgetConstants";
|
2022-03-11 05:35:05 +00:00
|
|
|
import { Button, Classes, InputGroup } from "@blueprintjs/core";
|
2022-04-04 05:12:33 +00:00
|
|
|
import { labelMargin, WidgetContainerDiff } from "widgets/WidgetUtils";
|
2021-11-16 09:27:38 +00:00
|
|
|
import Icon from "components/ads/Icon";
|
|
|
|
|
import { Colors } from "constants/Colors";
|
2021-09-17 09:08:35 +00:00
|
|
|
export interface TreeSelectProps
|
|
|
|
|
extends Required<
|
|
|
|
|
Pick<
|
|
|
|
|
SelectProps,
|
|
|
|
|
| "disabled"
|
|
|
|
|
| "placeholder"
|
|
|
|
|
| "loading"
|
|
|
|
|
| "dropdownStyle"
|
|
|
|
|
| "allowClear"
|
2022-03-11 05:35:05 +00:00
|
|
|
| "options"
|
2021-09-17 09:08:35 +00:00
|
|
|
>
|
|
|
|
|
> {
|
|
|
|
|
value?: DefaultValueType;
|
|
|
|
|
onChange: (value?: DefaultValueType, labelList?: ReactNode[]) => void;
|
|
|
|
|
expandAll: boolean;
|
|
|
|
|
labelText?: string;
|
|
|
|
|
labelTextColor?: string;
|
|
|
|
|
labelTextSize?: TextSize;
|
|
|
|
|
labelStyle?: string;
|
|
|
|
|
compactMode: boolean;
|
2021-11-30 10:38:46 +00:00
|
|
|
dropDownWidth: number;
|
|
|
|
|
width: number;
|
2021-11-16 09:27:38 +00:00
|
|
|
isValid: boolean;
|
2022-03-11 05:35:05 +00:00
|
|
|
filterText?: string;
|
|
|
|
|
widgetId: string;
|
|
|
|
|
isFilterable: boolean;
|
2021-09-17 09:08:35 +00:00
|
|
|
}
|
|
|
|
|
|
2021-11-16 09:27:38 +00:00
|
|
|
const getSvg = (expanded: boolean) => (
|
2021-09-17 09:08:35 +00:00
|
|
|
<i
|
|
|
|
|
style={{
|
|
|
|
|
cursor: "pointer",
|
|
|
|
|
backgroundColor: "transparent",
|
|
|
|
|
display: "inline-flex",
|
2021-11-16 09:27:38 +00:00
|
|
|
width: "14px",
|
|
|
|
|
height: "100%",
|
2021-09-17 09:08:35 +00:00
|
|
|
}}
|
|
|
|
|
>
|
2021-11-16 09:27:38 +00:00
|
|
|
<StyledIcon
|
|
|
|
|
className="switcher-icon"
|
|
|
|
|
expanded={expanded}
|
|
|
|
|
fillColor={Colors.GREY_10}
|
|
|
|
|
name="dropdown"
|
|
|
|
|
/>
|
2021-09-17 09:08:35 +00:00
|
|
|
</i>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const switcherIcon = (treeNode: TreeNodeProps) => {
|
|
|
|
|
if (treeNode.isLeaf) {
|
|
|
|
|
return (
|
|
|
|
|
<i
|
|
|
|
|
style={{
|
|
|
|
|
cursor: "pointer",
|
|
|
|
|
backgroundColor: "white",
|
|
|
|
|
display: "inline-flex",
|
2021-11-16 09:27:38 +00:00
|
|
|
width: "14px",
|
2021-09-17 09:08:35 +00:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-11-16 09:27:38 +00:00
|
|
|
return getSvg(treeNode.expanded);
|
2021-09-17 09:08:35 +00:00
|
|
|
};
|
2022-03-11 05:35:05 +00:00
|
|
|
const FOCUS_TIMEOUT = 500;
|
2021-09-17 09:08:35 +00:00
|
|
|
|
|
|
|
|
function SingleSelectTreeComponent({
|
|
|
|
|
allowClear,
|
|
|
|
|
compactMode,
|
|
|
|
|
disabled,
|
|
|
|
|
dropdownStyle,
|
2021-11-30 10:38:46 +00:00
|
|
|
dropDownWidth,
|
2021-09-17 09:08:35 +00:00
|
|
|
expandAll,
|
2022-03-11 05:35:05 +00:00
|
|
|
filterText,
|
|
|
|
|
isFilterable,
|
2021-11-16 09:27:38 +00:00
|
|
|
isValid,
|
2021-09-17 09:08:35 +00:00
|
|
|
labelStyle,
|
|
|
|
|
labelText,
|
|
|
|
|
labelTextColor,
|
|
|
|
|
labelTextSize,
|
|
|
|
|
loading,
|
|
|
|
|
onChange,
|
|
|
|
|
options,
|
|
|
|
|
placeholder,
|
|
|
|
|
value,
|
2022-03-11 05:35:05 +00:00
|
|
|
widgetId,
|
2021-11-30 10:38:46 +00:00
|
|
|
width,
|
2021-09-17 09:08:35 +00:00
|
|
|
}: TreeSelectProps): JSX.Element {
|
|
|
|
|
const [key, setKey] = useState(Math.random());
|
2022-03-11 05:35:05 +00:00
|
|
|
const [filter, setFilter] = useState(filterText ?? "");
|
|
|
|
|
const labelRef = useRef<HTMLDivElement>(null);
|
2021-09-17 09:08:35 +00:00
|
|
|
const _menu = useRef<HTMLElement | null>(null);
|
2022-03-11 05:35:05 +00:00
|
|
|
const inputRef = useRef<HTMLInputElement>(null);
|
2022-04-04 05:12:33 +00:00
|
|
|
const [memoDropDownWidth, setMemoDropDownWidth] = useState(0);
|
2021-09-17 09:08:35 +00:00
|
|
|
|
|
|
|
|
// treeDefaultExpandAll is uncontrolled after first render,
|
|
|
|
|
// using this to force render to respond to changes in expandAll
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setKey(Math.random());
|
|
|
|
|
}, [expandAll]);
|
|
|
|
|
|
|
|
|
|
const getDropdownPosition = useCallback(() => {
|
|
|
|
|
const node = _menu.current;
|
|
|
|
|
if (Boolean(node?.closest(`.${MODAL_PORTAL_CLASSNAME}`))) {
|
|
|
|
|
return document.querySelector(
|
|
|
|
|
`.${MODAL_PORTAL_CLASSNAME}`,
|
|
|
|
|
) as HTMLElement;
|
|
|
|
|
}
|
|
|
|
|
return document.querySelector(`.${CANVAS_CLASSNAME}`) as HTMLElement;
|
|
|
|
|
}, []);
|
|
|
|
|
const onClear = useCallback(() => onChange([], []), []);
|
2022-03-11 05:35:05 +00:00
|
|
|
const onOpen = useCallback((open: boolean) => {
|
|
|
|
|
if (open) {
|
|
|
|
|
setTimeout(() => inputRef.current?.focus(), FOCUS_TIMEOUT);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
const clearButton = useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
filter ? (
|
|
|
|
|
<Button icon="cross" minimal onClick={() => setFilter("")} />
|
|
|
|
|
) : null,
|
|
|
|
|
[filter],
|
|
|
|
|
);
|
|
|
|
|
const onQueryChange = useCallback((event: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
event.stopPropagation();
|
|
|
|
|
setFilter(event.target.value);
|
|
|
|
|
}, []);
|
|
|
|
|
|
2022-04-04 05:12:33 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
const parentWidth = width - WidgetContainerDiff;
|
2022-03-11 05:35:05 +00:00
|
|
|
if (compactMode && labelRef.current) {
|
2022-04-04 05:12:33 +00:00
|
|
|
const labelWidth = labelRef.current.getBoundingClientRect().width;
|
|
|
|
|
const widthDiff = parentWidth - labelWidth - labelMargin;
|
|
|
|
|
setMemoDropDownWidth(
|
|
|
|
|
widthDiff > dropDownWidth ? widthDiff : dropDownWidth,
|
|
|
|
|
);
|
|
|
|
|
return;
|
2022-03-11 05:35:05 +00:00
|
|
|
}
|
2022-04-04 05:12:33 +00:00
|
|
|
setMemoDropDownWidth(
|
|
|
|
|
parentWidth > dropDownWidth ? parentWidth : dropDownWidth,
|
|
|
|
|
);
|
|
|
|
|
}, [compactMode, dropDownWidth, width, labelText]);
|
2022-03-11 05:35:05 +00:00
|
|
|
|
|
|
|
|
const dropdownRender = useCallback(
|
|
|
|
|
(
|
|
|
|
|
menu: React.ReactElement<any, string | React.JSXElementConstructor<any>>,
|
|
|
|
|
) => (
|
|
|
|
|
<>
|
|
|
|
|
{isFilterable ? (
|
|
|
|
|
<InputGroup
|
|
|
|
|
autoFocus
|
|
|
|
|
inputRef={inputRef}
|
|
|
|
|
leftIcon="search"
|
|
|
|
|
onChange={onQueryChange}
|
|
|
|
|
onKeyDown={(e) => e.stopPropagation()}
|
|
|
|
|
placeholder="Filter..."
|
|
|
|
|
rightElement={clearButton as JSX.Element}
|
|
|
|
|
small
|
|
|
|
|
type="text"
|
|
|
|
|
value={filter}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
<div className={`${loading ? Classes.SKELETON : ""}`}>{menu}</div>
|
|
|
|
|
</>
|
|
|
|
|
),
|
|
|
|
|
[loading, isFilterable, filter, onQueryChange],
|
|
|
|
|
);
|
2021-09-17 09:08:35 +00:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<TreeSelectContainer
|
|
|
|
|
compactMode={compactMode}
|
2021-11-16 09:27:38 +00:00
|
|
|
isValid={isValid}
|
2021-09-17 09:08:35 +00:00
|
|
|
ref={_menu as React.RefObject<HTMLDivElement>}
|
|
|
|
|
>
|
2022-03-11 05:35:05 +00:00
|
|
|
<DropdownStyles dropDownWidth={memoDropDownWidth} id={widgetId} />
|
2021-09-17 09:08:35 +00:00
|
|
|
{labelText && (
|
2022-03-11 05:35:05 +00:00
|
|
|
<TextLabelWrapper compactMode={compactMode} ref={labelRef}>
|
2021-09-17 09:08:35 +00:00
|
|
|
<StyledLabel
|
|
|
|
|
$compactMode={compactMode}
|
2021-11-16 09:27:38 +00:00
|
|
|
$disabled={disabled}
|
2021-09-17 09:08:35 +00:00
|
|
|
$labelStyle={labelStyle}
|
|
|
|
|
$labelText={labelText}
|
|
|
|
|
$labelTextColor={labelTextColor}
|
|
|
|
|
$labelTextSize={labelTextSize}
|
|
|
|
|
className={`tree-select-label ${
|
|
|
|
|
loading ? Classes.SKELETON : Classes.TEXT_OVERFLOW_ELLIPSIS
|
|
|
|
|
}`}
|
2021-11-15 06:29:06 +00:00
|
|
|
disabled={disabled}
|
2021-09-17 09:08:35 +00:00
|
|
|
>
|
|
|
|
|
{labelText}
|
|
|
|
|
</StyledLabel>
|
|
|
|
|
</TextLabelWrapper>
|
|
|
|
|
)}
|
|
|
|
|
<TreeSelect
|
|
|
|
|
allowClear={allowClear}
|
|
|
|
|
animation="slide-up"
|
|
|
|
|
choiceTransitionName="rc-tree-select-selection__choice-zoom"
|
|
|
|
|
className="rc-tree-select"
|
2021-11-16 09:27:38 +00:00
|
|
|
clearIcon={
|
|
|
|
|
<Icon
|
|
|
|
|
className="clear-icon"
|
|
|
|
|
fillColor={Colors.GREY_10}
|
|
|
|
|
name="close-x"
|
|
|
|
|
/>
|
|
|
|
|
}
|
2021-09-17 09:08:35 +00:00
|
|
|
disabled={disabled}
|
2022-03-11 05:35:05 +00:00
|
|
|
dropdownClassName={`tree-select-dropdown single-tree-select-dropdown treeselect-popover-width-${widgetId}`}
|
|
|
|
|
dropdownRender={dropdownRender}
|
2021-09-17 09:08:35 +00:00
|
|
|
dropdownStyle={dropdownStyle}
|
2022-03-11 05:35:05 +00:00
|
|
|
filterTreeNode
|
2021-09-17 09:08:35 +00:00
|
|
|
getPopupContainer={getDropdownPosition}
|
2021-11-16 09:27:38 +00:00
|
|
|
inputIcon={
|
|
|
|
|
<Icon
|
|
|
|
|
className="dropdown-icon"
|
|
|
|
|
fillColor={disabled ? Colors.GREY_7 : Colors.GREY_10}
|
|
|
|
|
name="dropdown"
|
|
|
|
|
/>
|
|
|
|
|
}
|
2021-09-17 09:08:35 +00:00
|
|
|
key={key}
|
|
|
|
|
loading={loading}
|
|
|
|
|
maxTagCount={"responsive"}
|
|
|
|
|
maxTagPlaceholder={(e) => `+${e.length} more`}
|
2021-10-31 06:25:23 +00:00
|
|
|
notFoundContent="No Results Found"
|
2021-09-17 09:08:35 +00:00
|
|
|
onChange={onChange}
|
|
|
|
|
onClear={onClear}
|
2022-03-11 05:35:05 +00:00
|
|
|
onDropdownVisibleChange={onOpen}
|
2021-09-17 09:08:35 +00:00
|
|
|
placeholder={placeholder}
|
2022-03-11 05:35:05 +00:00
|
|
|
searchValue={filter}
|
2021-09-17 09:08:35 +00:00
|
|
|
showArrow
|
2022-03-11 05:35:05 +00:00
|
|
|
showSearch={false}
|
2021-09-17 09:08:35 +00:00
|
|
|
style={{ width: "100%" }}
|
|
|
|
|
switcherIcon={switcherIcon}
|
|
|
|
|
transitionName="rc-tree-select-dropdown-slide-up"
|
|
|
|
|
treeData={options}
|
|
|
|
|
treeDefaultExpandAll={expandAll}
|
|
|
|
|
treeIcon
|
|
|
|
|
treeNodeFilterProp="label"
|
|
|
|
|
value={value}
|
|
|
|
|
/>
|
|
|
|
|
</TreeSelectContainer>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default SingleSelectTreeComponent;
|