@gisce/react-ooui 2.0.0-alpha.59 → 2.0.0-alpha.60
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { memo, useState, useMemo, useCallback } from "react";
|
|
2
2
|
import { Menu, Dropdown, Button, Input } from "antd";
|
|
3
3
|
import { DownOutlined } from "@ant-design/icons";
|
|
4
4
|
|
|
@@ -14,8 +14,8 @@ type Props = {
|
|
|
14
14
|
searchable?: true | false | "auto";
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
const DropdownButton: React.FC<Props> = memo(
|
|
18
|
+
({
|
|
19
19
|
icon,
|
|
20
20
|
tooltip,
|
|
21
21
|
items = [],
|
|
@@ -23,84 +23,102 @@ function DropdownButton(props: Props) {
|
|
|
23
23
|
label,
|
|
24
24
|
disabled = false,
|
|
25
25
|
searchable = "auto",
|
|
26
|
-
}
|
|
27
|
-
|
|
26
|
+
}: Props) => {
|
|
27
|
+
const [searchValue, setSearchValue] = useState<string>();
|
|
28
28
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
setSearchValue(undefined);
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const sanitizedValue = value.trim();
|
|
29
|
+
const mustShowSearch =
|
|
30
|
+
searchable === true || (searchable === "auto" && items.length > 3);
|
|
36
31
|
|
|
37
|
-
|
|
32
|
+
const onSearch = useCallback((value?: string) => {
|
|
33
|
+
const sanitizedValue = value?.trim();
|
|
34
|
+
if (sanitizedValue && sanitizedValue.length > 0) {
|
|
35
|
+
setSearchValue(sanitizedValue);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
38
|
setSearchValue(undefined);
|
|
39
|
-
|
|
40
|
-
}
|
|
39
|
+
}, []);
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return <Menu.Divider key={"divider" + idx} />;
|
|
41
|
+
const filteredItems = useMemo(() => {
|
|
42
|
+
if (searchValue) {
|
|
43
|
+
return items
|
|
44
|
+
.map((item) => {
|
|
45
|
+
if (item.name.toLowerCase().includes(searchValue.toLowerCase())) {
|
|
46
|
+
return { ...item };
|
|
47
|
+
}
|
|
48
|
+
return { ...item, name: "no_match" };
|
|
49
|
+
})
|
|
50
|
+
.sort((a, b) => {
|
|
51
|
+
// all the items with name "no_match" have to be in the end of the array
|
|
52
|
+
if (a.name === "no_match") return 1;
|
|
53
|
+
if (b.name === "no_match") return -1;
|
|
54
|
+
return 0;
|
|
55
|
+
});
|
|
58
56
|
}
|
|
59
|
-
return
|
|
60
|
-
});
|
|
57
|
+
return items;
|
|
58
|
+
}, [items, searchValue]);
|
|
61
59
|
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
onSearch={onSearch}
|
|
72
|
-
allowClear
|
|
73
|
-
style={{ padding: 5 }}
|
|
74
|
-
/>
|
|
75
|
-
)}
|
|
76
|
-
</div>
|
|
77
|
-
<div style={{ maxHeight: 300, overflowY: "auto" }}>
|
|
78
|
-
<Menu.ItemGroup title={tooltip}>{menuItems}</Menu.ItemGroup>
|
|
79
|
-
</div>
|
|
80
|
-
</Menu>
|
|
60
|
+
const handleMenuClick = useCallback(
|
|
61
|
+
(event: any) => {
|
|
62
|
+
const itemClicked = filteredItems.find(
|
|
63
|
+
(item: any) => item.id.toString() === event.key,
|
|
64
|
+
);
|
|
65
|
+
if (itemClicked && itemClicked.name !== "no_match")
|
|
66
|
+
onItemClick(itemClicked);
|
|
67
|
+
},
|
|
68
|
+
[onItemClick, filteredItems],
|
|
81
69
|
);
|
|
82
|
-
}
|
|
83
70
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
71
|
+
const getMenu = useMemo(() => {
|
|
72
|
+
const menuItems = filteredItems.map((item, idx) => {
|
|
73
|
+
if (item.name === "divider")
|
|
74
|
+
return <Menu.Divider key={"divider" + idx} />;
|
|
75
|
+
if (item.name === "no_match")
|
|
76
|
+
return (
|
|
77
|
+
<Menu.Item
|
|
78
|
+
key={"no_match" + idx}
|
|
79
|
+
style={{ cursor: "auto", pointerEvents: "none", opacity: 0 }}
|
|
80
|
+
>
|
|
81
|
+
{item.name}
|
|
82
|
+
</Menu.Item>
|
|
83
|
+
);
|
|
84
|
+
return <Menu.Item key={item.id}>{item.name}</Menu.Item>;
|
|
85
|
+
});
|
|
90
86
|
|
|
91
|
-
|
|
92
|
-
|
|
87
|
+
return (
|
|
88
|
+
<Menu onClick={handleMenuClick}>
|
|
89
|
+
{mustShowSearch && (
|
|
90
|
+
<div style={{ padding: 5 }}>
|
|
91
|
+
<Search
|
|
92
|
+
onChange={(e) => onSearch(e.target.value)}
|
|
93
|
+
onSearch={onSearch}
|
|
94
|
+
allowClear
|
|
95
|
+
/>
|
|
96
|
+
</div>
|
|
97
|
+
)}
|
|
98
|
+
<div
|
|
99
|
+
style={{
|
|
100
|
+
width: 300,
|
|
101
|
+
maxHeight: 300,
|
|
102
|
+
overflowY: "auto",
|
|
103
|
+
}}
|
|
104
|
+
>
|
|
105
|
+
<Menu.ItemGroup title={tooltip}>{menuItems}</Menu.ItemGroup>
|
|
106
|
+
</div>
|
|
107
|
+
</Menu>
|
|
108
|
+
);
|
|
109
|
+
}, [filteredItems, handleMenuClick, mustShowSearch, onSearch, tooltip]);
|
|
93
110
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
111
|
+
return (
|
|
112
|
+
<>
|
|
113
|
+
<Dropdown overlay={getMenu} disabled={disabled || items.length === 0}>
|
|
114
|
+
<Button>
|
|
115
|
+
{icon} {label} <DownOutlined />
|
|
116
|
+
</Button>
|
|
117
|
+
</Dropdown>
|
|
118
|
+
</>
|
|
119
|
+
);
|
|
120
|
+
},
|
|
121
|
+
);
|
|
122
|
+
DropdownButton.displayName = "DropdownButton";
|
|
105
123
|
|
|
106
124
|
export default DropdownButton;
|