-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathItem.tsx
144 lines (127 loc) · 4.53 KB
/
Item.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import React, { forwardRef, useMemo } from 'react';
import classNames from 'classnames';
import { ChevronRightIcon as TdChevronRightIcon } from 'tdesign-icons-react';
import TLoading from '../../loading';
import Checkbox from '../../checkbox';
import useConfig from '../../hooks/useConfig';
import useGlobalIcon from '../../hooks/useGlobalIcon';
import useDomRefCallback from '../../hooks/useDomRefCallback';
import useCommonClassName from '../../_util/useCommonClassName';
import useRipple from '../../_util/useRipple';
import { getFullPathLabel } from '../core/helper';
import { getCascaderItemClass, getCascaderItemIconClass } from '../core/className';
import { CascaderContextType, TreeNodeValue, TreeNode } from '../interface';
const Item = forwardRef(
(
props: {
node: TreeNode;
cascaderContext: CascaderContextType;
onClick: (ctx: TreeNode) => void;
onChange: (ctx: TreeNode | { e: boolean; node: TreeNode }) => void;
onMouseEnter: (ctx: TreeNode) => void;
},
ref: React.RefObject<HTMLLIElement>,
) => {
const {
node,
cascaderContext: { multiple },
onClick,
onChange,
onMouseEnter,
cascaderContext,
} = props;
const { classPrefix: prefix } = useConfig();
const { ChevronRightIcon } = useGlobalIcon({ ChevronRightIcon: TdChevronRightIcon });
const COMPONENT_NAME = `${prefix}-cascader__item`;
const [itemDom, setRefCurrent] = useDomRefCallback();
useRipple(ref?.current || itemDom);
/**
* class
*/
const { STATUS, SIZE } = useCommonClassName();
const itemClass = useMemo(
() => classNames(getCascaderItemClass(prefix, node, SIZE, STATUS, cascaderContext)),
[prefix, node, SIZE, STATUS, cascaderContext],
);
const iconClass = useMemo(
() => classNames(getCascaderItemIconClass(prefix, node, STATUS, cascaderContext)),
[prefix, node, STATUS, cascaderContext],
);
const RenderLabelInner = (node: TreeNode, cascaderContext: CascaderContextType) => {
const { inputVal } = cascaderContext;
const labelText = inputVal ? getFullPathLabel(node) : node.label;
if (inputVal) {
const texts = labelText.split(inputVal as string);
const doms = [];
for (let index = 0; index < texts.length; index++) {
doms.push(<span key={index}>{texts[index]}</span>);
if (index === texts.length - 1) break;
doms.push(
<span key={`${index}filter`} className={`${COMPONENT_NAME}-label--filter`}>
{inputVal}
</span>,
);
}
return doms;
}
return labelText;
};
const RenderLabelContent = (node: TreeNode, cascaderContext: CascaderContextType) => {
const label = RenderLabelInner(node, cascaderContext);
const labelCont = (
<span
title={cascaderContext.inputVal ? getFullPathLabel(node) : node.label}
className={classNames(`${COMPONENT_NAME}-label`, `${COMPONENT_NAME}-label--ellipsis`)}
role="label"
>
{label}
</span>
);
return labelCont;
};
const RenderCheckBox = (node: TreeNode, cascaderContext: CascaderContextType) => {
const { checkProps, value, max, inputVal } = cascaderContext;
const label = RenderLabelInner(node, cascaderContext);
return (
<Checkbox
checked={node.checked}
indeterminate={node.indeterminate}
disabled={node.isDisabled() || (value && (value as TreeNodeValue[]).length >= max && max !== 0)}
name={String(node.value)}
stopLabelTrigger={!!node.children}
title={inputVal ? getFullPathLabel(node) : node.label}
onChange={() => {
onChange(node);
}}
{...checkProps}
>
{label}
</Checkbox>
);
};
return (
<li
ref={ref || setRefCurrent}
className={itemClass}
onClick={(e: React.MouseEvent) => {
e.stopPropagation();
e?.nativeEvent?.stopImmediatePropagation?.();
onClick(node);
}}
onMouseEnter={(e: React.MouseEvent) => {
e.stopPropagation();
onMouseEnter(node);
}}
>
{multiple ? RenderCheckBox(node, cascaderContext) : RenderLabelContent(node, cascaderContext)}
{node.children &&
(node.loading ? (
<TLoading className={iconClass} loading={true} size="small" />
) : (
<ChevronRightIcon className={iconClass} />
))}
</li>
);
},
);
export default Item;