@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,6 +1,6 @@
1
1
  {
2
2
  "name": "@gisce/react-ooui",
3
- "version": "2.0.0-alpha.59",
3
+ "version": "2.0.0-alpha.60",
4
4
  "engines": {
5
5
  "node": "20.5.0"
6
6
  },
@@ -1,4 +1,4 @@
1
- import React, { useState } from "react";
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
- function DropdownButton(props: Props) {
18
- const {
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
- } = props;
27
- const [searchValue, setSearchValue] = useState<string>();
26
+ }: Props) => {
27
+ const [searchValue, setSearchValue] = useState<string>();
28
28
 
29
- const onSearch = (value?: string) => {
30
- if (!value) {
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
- if (sanitizedValue.length === 0) {
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
- return;
40
- }
39
+ }, []);
41
40
 
42
- setSearchValue(sanitizedValue);
43
- };
44
-
45
- function getMenu() {
46
- let itemsData = [];
47
- if (searchValue) {
48
- itemsData = items.filter((item) => {
49
- return item.name.toLowerCase().includes(searchValue.toLowerCase());
50
- });
51
- } else {
52
- itemsData = items;
53
- }
54
-
55
- const menuItems = itemsData.map((item, idx) => {
56
- if (item.name === "divider") {
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 <Menu.Item key={item.id}>{item.name}</Menu.Item>;
60
- });
57
+ return items;
58
+ }, [items, searchValue]);
61
59
 
62
- const mustShowSearch =
63
- searchable === true || (searchable === "auto" && items.length > 3);
64
-
65
- return (
66
- <Menu onClick={handleMenuClick}>
67
- <div>
68
- {mustShowSearch && (
69
- <Search
70
- onChange={(e: any) => onSearch(e.target.value)}
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
- function handleMenuClick(event: any) {
85
- const itemClicked = items.find((item: any) => {
86
- return !isNaN(event.key)
87
- ? item.id === parseInt(event.key)
88
- : item.id === event.key;
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
- onItemClick(itemClicked);
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
- return (
95
- <Dropdown
96
- overlay={getMenu()}
97
- disabled={disabled || !items || items.length === 0}
98
- >
99
- <Button>
100
- {icon} {label} <DownOutlined />
101
- </Button>
102
- </Dropdown>
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;