-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathItem.tsx
More file actions
165 lines (145 loc) · 5.31 KB
/
Item.tsx
File metadata and controls
165 lines (145 loc) · 5.31 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React, { forwardRef, useMemo } from 'react';
import { ChevronRightIcon as TdChevronRightIcon } from 'tdesign-icons-react';
import classNames from 'classnames';
import { isFunction } from 'lodash-es';
import Checkbox from '../../checkbox';
import TLoading from '../../loading';
import useCommonClassName from '../../hooks/useCommonClassName';
import useConfig from '../../hooks/useConfig';
import useDomRefCallback from '../../hooks/useDomRefCallback';
import useGlobalIcon from '../../hooks/useGlobalIcon';
import { getCascaderItemClass, getCascaderItemIconClass } from '../core/className';
import { getFullPathLabel } from '../core/helper';
import type { CascaderContextType, TreeNode, TreeNodeValue } from '../interface';
const Item = forwardRef(
(
props: {
node: TreeNode;
optionChild: React.ReactNode;
cascaderContext: CascaderContextType;
onClick: (ctx: TreeNode) => void;
onChange: (ctx: TreeNode | { e: boolean; node: TreeNode }) => void;
onMouseEnter: (ctx: TreeNode) => void;
},
ref: React.RefObject<HTMLLIElement>,
) => {
const {
node,
optionChild,
cascaderContext: { multiple },
onClick,
onChange,
onMouseEnter,
cascaderContext,
} = props;
const { classPrefix: prefix } = useConfig();
const { ChevronRightIcon } = useGlobalIcon({ ChevronRightIcon: TdChevronRightIcon });
const COMPONENT_NAME = `${prefix}-cascader__item`;
// 暂时去掉动画效果 长列表在safari中有异常
// eslint-disable-next-line @typescript-eslint/no-unused-vars
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;
if (!inputVal && optionChild) {
return optionChild;
}
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 getTitle = () => {
const title = cascaderContext.inputVal ? getFullPathLabel(node) : node.label;
return typeof title !== 'object' ? title : undefined;
};
const labelCont = (
<span
title={getTitle()}
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, isParentFilterable } = 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 && !isParentFilterable}
title={inputVal ? getFullPathLabel(node) : node.label}
onChange={() => {
onChange(node);
}}
{...checkProps}
>
{label}
</Checkbox>
);
};
const isFiltering = useMemo(
() => Boolean(cascaderContext.filterable) || isFunction(cascaderContext.filter),
[cascaderContext.filterable, cascaderContext.filter],
);
return (
<li
ref={ref}
className={itemClass}
onClick={(e: React.MouseEvent) => {
e.stopPropagation();
e?.nativeEvent?.stopImmediatePropagation?.();
if (multiple && cascaderContext.inputVal && isFiltering) return;
onClick(node);
}}
onMouseEnter={(e: React.MouseEvent) => {
e.stopPropagation();
if (cascaderContext.inputVal && isFiltering) return;
onMouseEnter(node);
}}
>
{multiple ? RenderCheckBox(node, cascaderContext) : RenderLabelContent(node, cascaderContext)}
{node.children &&
!cascaderContext.isParentFilterable &&
(node.loading ? (
<TLoading className={iconClass} loading={true} size="small" />
) : (
<ChevronRightIcon className={iconClass} />
))}
</li>
);
},
);
export default Item;