-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathItem.tsx
More file actions
executable file
·108 lines (95 loc) · 3.79 KB
/
Item.tsx
File metadata and controls
executable file
·108 lines (95 loc) · 3.79 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
import React, { useCallback, useMemo } from "react";
import { COLORS, DEFAULT_THEME, THEME_DATA } from "../constants";
import DisabledItem from "./DisabledItem";
import { useSelectContext } from "./SelectProvider";
import { Option } from "./type";
interface ItemProps {
item: Option;
primaryColor: string;
searchInputValue?: string;
noHighLigthLabel: boolean;
}
const Item: React.FC<ItemProps> = ({ item, primaryColor, searchInputValue, noHighLigthLabel }) => {
const { classNames, value, handleValueChange, formatOptionLabel } = useSelectContext();
const isSelected = useMemo(() => {
return value !== null && !Array.isArray(value) && value.value === item.value;
}, [item.value, value]);
const textHoverColor = useMemo(() => {
if (COLORS.includes(primaryColor)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return THEME_DATA.textHover[primaryColor];
}
return THEME_DATA.textHover[DEFAULT_THEME];
}, [primaryColor]);
const bgColor = useMemo(() => {
if (COLORS.includes(primaryColor)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return THEME_DATA.bg[primaryColor];
}
return THEME_DATA.bg[DEFAULT_THEME];
}, [primaryColor]);
const bgHoverColor = useMemo(() => {
if (COLORS.includes(primaryColor)) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
return THEME_DATA.bgHover[primaryColor];
}
return THEME_DATA.bgHover[DEFAULT_THEME];
}, [primaryColor]);
const getItemClass = useCallback(() => {
const baseClass =
"block transition duration-200 px-2 py-2 cursor-pointer select-none truncate rounded";
const selectedClass = isSelected
? `text-white ${bgColor}`
: `text-gray-500 ${bgHoverColor} ${textHoverColor}`;
return classNames && classNames.listItem
? classNames.listItem({ isSelected })
: `${baseClass} ${selectedClass}`;
}, [bgColor, bgHoverColor, classNames, isSelected, textHoverColor]);
const getLabel = useCallback(() => {
if (!noHighLigthLabel && searchInputValue) {
const start = item.label.toUpperCase().indexOf(searchInputValue.toUpperCase());
const end = start + searchInputValue.length;
return item.label.split("").map((label, idx) => {
if (idx >= start && idx < end) {
return (
<span key={idx} className={`font-bold text-${primaryColor}-500`}>
{label}
</span>
);
} else {
return <span key={idx}>{label}</span>;
}
});
} else {
return <span>{item.label}</span>;
}
}, [item.label, noHighLigthLabel, primaryColor, searchInputValue]);
return (
<>
{formatOptionLabel ? (
<div onClick={() => handleValueChange(item)}>
{formatOptionLabel({ ...item, isSelected })}
</div>
) : (
<>
{item.disabled ? (
<DisabledItem>{item.label}</DisabledItem>
) : (
<li
aria-selected={isSelected}
role={"option"}
onClick={() => handleValueChange(item)}
className={getItemClass()}
>
{getLabel()}
</li>
)}
</>
)}
</>
);
};
export default Item;