@gisce/react-ooui 2.0.0-alpha.29 → 2.0.0-alpha.30

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.29",
3
+ "version": "2.0.0-alpha.30",
4
4
  "engines": {
5
5
  "node": "20.5.0"
6
6
  },
@@ -24,36 +24,52 @@ function DropdownButton(props: Props) {
24
24
  disabled = false,
25
25
  searchable = "auto",
26
26
  } = props;
27
- const [filteredItems, setFilteredItems] = useState([...items]);
27
+ const [searchValue, setSearchValue] = useState<string>();
28
28
 
29
- const onSearch = (value: string) => {
29
+ const onSearch = (value?: string) => {
30
30
  if (!value) {
31
- setFilteredItems([...items]);
32
- } else {
33
- setFilteredItems(
34
- items.filter(
35
- (item) => item.name.toLowerCase().indexOf(value.toLowerCase()) !== -1,
36
- ),
37
- );
31
+ setSearchValue(undefined);
32
+ return;
38
33
  }
34
+
35
+ const sanitizedValue = value.trim();
36
+
37
+ if (sanitizedValue.length === 0) {
38
+ setSearchValue(undefined);
39
+ return;
40
+ }
41
+
42
+ setSearchValue(sanitizedValue);
39
43
  };
40
44
 
41
45
  function getMenu() {
42
- const menuItems = filteredItems?.map((item, idx) => {
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) => {
43
56
  if (item.name === "divider") {
44
57
  return <Menu.Divider key={"divider" + idx} />;
45
58
  }
46
59
  return <Menu.Item key={item.id}>{item.name}</Menu.Item>;
47
60
  });
48
61
 
62
+ const mustShowSearch =
63
+ searchable === true || (searchable === "auto" && items.length > 3);
64
+
49
65
  return (
50
66
  <Menu onClick={handleMenuClick}>
51
- {((searchable === "auto" && items.length > 3) ||
52
- searchable === true) && (
67
+ {mustShowSearch && (
53
68
  <Search
54
- onChange={(e) => onSearch(e.target.value)}
69
+ onChange={(e: any) => onSearch(e.target.value)}
55
70
  onSearch={onSearch}
56
71
  allowClear
72
+ style={{ padding: 5 }}
57
73
  />
58
74
  )}
59
75
  <Menu.ItemGroup title={tooltip}>{menuItems}</Menu.ItemGroup>