@react-fabric/searchbar 1.0.0-beta.0

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.
Files changed (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/filterbar/Context.js +34 -0
  4. package/dist/cjs/filterbar/FilterBar.js +149 -0
  5. package/dist/cjs/filterbar/FilterEdit.js +51 -0
  6. package/dist/cjs/filterbar/FilterForm.js +262 -0
  7. package/dist/cjs/filterbar/FilterMenu.js +80 -0
  8. package/dist/cjs/filterbar/FilterTag.js +93 -0
  9. package/dist/cjs/filterbar/FilterView.js +88 -0
  10. package/dist/cjs/filterbar/QueryForm.js +135 -0
  11. package/dist/cjs/i18n/ar.json.js +84 -0
  12. package/dist/cjs/i18n/en.json.js +84 -0
  13. package/dist/cjs/i18n/index.js +33 -0
  14. package/dist/cjs/index.js +11 -0
  15. package/dist/cjs/searchbar/SearchBar.js +154 -0
  16. package/dist/cjs/types/index.js +70 -0
  17. package/dist/esm/filterbar/Context.js +33 -0
  18. package/dist/esm/filterbar/FilterBar.js +149 -0
  19. package/dist/esm/filterbar/FilterEdit.js +51 -0
  20. package/dist/esm/filterbar/FilterForm.js +262 -0
  21. package/dist/esm/filterbar/FilterMenu.js +80 -0
  22. package/dist/esm/filterbar/FilterTag.js +93 -0
  23. package/dist/esm/filterbar/FilterView.js +88 -0
  24. package/dist/esm/filterbar/QueryForm.js +135 -0
  25. package/dist/esm/i18n/ar.json.js +77 -0
  26. package/dist/esm/i18n/en.json.js +77 -0
  27. package/dist/esm/i18n/index.js +33 -0
  28. package/dist/esm/index.js +5 -0
  29. package/dist/esm/searchbar/SearchBar.js +154 -0
  30. package/dist/esm/types/index.js +68 -0
  31. package/dist/types/filterbar/Context.d.ts +4 -0
  32. package/dist/types/filterbar/FilterBar.d.ts +2 -0
  33. package/dist/types/filterbar/FilterEdit.d.ts +6 -0
  34. package/dist/types/filterbar/FilterForm.d.ts +6 -0
  35. package/dist/types/filterbar/FilterMenu.d.ts +7 -0
  36. package/dist/types/filterbar/FilterTag.d.ts +6 -0
  37. package/dist/types/filterbar/FilterView.d.ts +5 -0
  38. package/dist/types/filterbar/QueryForm.d.ts +6 -0
  39. package/dist/types/i18n/index.d.ts +1 -0
  40. package/dist/types/index.d.ts +3 -0
  41. package/dist/types/searchbar/SearchBar.d.ts +2 -0
  42. package/dist/types/types/index.d.ts +203 -0
  43. package/package.json +56 -0
  44. package/src/filterbar/Context.tsx +31 -0
  45. package/src/filterbar/FilterBar.tsx +195 -0
  46. package/src/filterbar/FilterEdit.tsx +70 -0
  47. package/src/filterbar/FilterForm.tsx +281 -0
  48. package/src/filterbar/FilterMenu.tsx +100 -0
  49. package/src/filterbar/FilterTag.tsx +114 -0
  50. package/src/filterbar/FilterView.tsx +89 -0
  51. package/src/filterbar/QueryForm.tsx +151 -0
  52. package/src/i18n/ar.json +68 -0
  53. package/src/i18n/en.json +68 -0
  54. package/src/i18n/index.ts +28 -0
  55. package/src/index.ts +32 -0
  56. package/src/searchbar/SearchBar.tsx +162 -0
  57. package/src/types/index.ts +315 -0
@@ -0,0 +1,68 @@
1
+ 'use client';
2
+
3
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
4
+
5
+ var _OperatorValueType;
6
+ /*
7
+ * React Fabric
8
+ * @version: 1.0.0
9
+ *
10
+ *
11
+ * The MIT License (MIT)
12
+ * Copyright (c) 2024 Adarsh Pastakia
13
+ *
14
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
15
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
16
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
17
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
18
+ * subject to the following conditions:
19
+ *
20
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
21
+ * portions of the Software.
22
+ *
23
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
24
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ */
28
+
29
+ var FIELD_TYPE = /*#__PURE__*/function (FIELD_TYPE) {
30
+ FIELD_TYPE["ID"] = "ID";
31
+ FIELD_TYPE["STRING"] = "STRING";
32
+ FIELD_TYPE["NUMBER"] = "NUMBER";
33
+ FIELD_TYPE["DECIMAL"] = "DECIMAL";
34
+ FIELD_TYPE["BOOLEAN"] = "BOOLEAN";
35
+ FIELD_TYPE["NONE"] = "NONE";
36
+ return FIELD_TYPE;
37
+ }({});
38
+ var OPERATOR = /*#__PURE__*/function (OPERATOR) {
39
+ OPERATOR["EXISTS"] = "EXISTS";
40
+ OPERATOR["IS"] = "IS";
41
+ OPERATOR["IN"] = "IN";
42
+ OPERATOR["LT"] = "LT";
43
+ OPERATOR["GT"] = "GT";
44
+ OPERATOR["LTE"] = "LTE";
45
+ OPERATOR["GTE"] = "GTE";
46
+ OPERATOR["INCLUDES"] = "INCLUDES";
47
+ OPERATOR["STARTS"] = "STARTS";
48
+ OPERATOR["ENDS"] = "ENDS";
49
+ OPERATOR["WITHIN"] = "WITHIN";
50
+ OPERATOR["BETWEEN"] = "BETWEEN";
51
+ return OPERATOR;
52
+ }({});
53
+
54
+ /**
55
+ * @internal
56
+ */
57
+ var TypeOperators = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, FIELD_TYPE.ID, [OPERATOR.IS]), FIELD_TYPE.STRING, [OPERATOR.EXISTS, OPERATOR.IS, OPERATOR.IN, OPERATOR.INCLUDES, OPERATOR.STARTS, OPERATOR.ENDS]), FIELD_TYPE.NUMBER, [OPERATOR.EXISTS, OPERATOR.IS, OPERATOR.BETWEEN, OPERATOR.LT, OPERATOR.GT, OPERATOR.LTE, OPERATOR.GTE]), FIELD_TYPE.DECIMAL, [OPERATOR.EXISTS, OPERATOR.IS, OPERATOR.BETWEEN, OPERATOR.LT, OPERATOR.GT, OPERATOR.LTE, OPERATOR.GTE]), FIELD_TYPE.BOOLEAN, [OPERATOR.EXISTS, OPERATOR.IS]), FIELD_TYPE.NONE, [OPERATOR.EXISTS]);
58
+
59
+ /**
60
+ * @internal
61
+ */
62
+ (_OperatorValueType = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_OperatorValueType, OPERATOR.EXISTS, 0), OPERATOR.IS, 0), OPERATOR.IN, 1), OPERATOR.BETWEEN, 2), OPERATOR.WITHIN, 0), OPERATOR.STARTS, 0), OPERATOR.ENDS, 0), OPERATOR.INCLUDES, 0), OPERATOR.LT, 0), OPERATOR.GT, 0), _defineProperty(_defineProperty(_OperatorValueType, OPERATOR.LTE, 0), OPERATOR.GTE, 0));
63
+
64
+ /**
65
+ * @internal
66
+ */
67
+
68
+ export { FIELD_TYPE, OPERATOR, TypeOperators };
@@ -0,0 +1,4 @@
1
+ /// <reference types="react" />
2
+ import { type FilterBarProps } from "../types";
3
+ export declare const FilterContext: import("react").Context<Pick<FilterBarProps, "querySchema" | "defaultQuery" | "fields">>;
4
+ export declare const useFilterContext: () => Pick<FilterBarProps, "querySchema" | "defaultQuery" | "fields">;
@@ -0,0 +1,2 @@
1
+ import { type FilterBarProps } from "../types";
2
+ export declare const FilterBar: ({ filters, allowAdd, fields, querySchema, defaultQuery, includedColor, excludedColor, onFilterChanged, }: FilterBarProps) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,6 @@
1
+ import { type FilterObject } from "../types";
2
+ export declare const FilterEdit: ({ filter, onRemove, onChange, }: {
3
+ filter?: FilterObject;
4
+ onRemove?: () => void;
5
+ onChange: (filter: FilterObject) => void;
6
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,6 @@
1
+ import { type FilterObject } from "../types";
2
+ export declare const FilterForm: ({ filter, onChange, onRemove, }: {
3
+ filter?: FilterObject;
4
+ onRemove?: () => void;
5
+ onChange?: (filter: FilterObject) => void;
6
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,7 @@
1
+ import { type FilterField, type FilterObject } from "../types";
2
+ export declare const FilterMenu: ({ filter, fields, onChange, onRemove, }: {
3
+ fields?: FilterField[];
4
+ filter: FilterObject;
5
+ onRemove: () => void;
6
+ onChange: (filter: FilterObject) => void;
7
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,6 @@
1
+ import { type FilterBarProps, type FilterObject } from "../types";
2
+ export declare const FilterTag: ({ filter, fields, includedColor, excludedColor, onChange, onRemove, }: {
3
+ filter: FilterObject;
4
+ onRemove: () => void;
5
+ onChange: (filter: FilterObject) => void;
6
+ } & Pick<FilterBarProps, "fields" | "includedColor" | "excludedColor">) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,5 @@
1
+ import { type FilterObject } from "../types";
2
+ export declare const FilterView: ({ filter, onRemove, }: {
3
+ filter: FilterObject;
4
+ onRemove: () => void;
5
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,6 @@
1
+ import { type FilterObject } from "../types";
2
+ export declare const QueryForm: ({ filter, onChange, onRemove, }: {
3
+ filter?: FilterObject;
4
+ onRemove?: () => void;
5
+ onChange?: (filter: FilterObject) => void;
6
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import "./i18n";
2
+ export { SearchBar } from "./searchbar/SearchBar";
3
+ export { FIELD_TYPE, OPERATOR, type FilterObject, type SearchObject, } from "./types";
@@ -0,0 +1,2 @@
1
+ import { type FilterBarProps, type SearchBarProps } from "../types";
2
+ export declare const SearchBar: ({ historyKey, historyCount, defaultQueryList, prepend, append, decorateEnd, decorateStart, actions, hideFilters, defaultCollapsed, disabled, onCollapsed, onQuery, onSearch, query, multiple, filters, ...filterProps }: SearchBarProps & Omit<FilterBarProps, "onFilterChanged">) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,203 @@
1
+ import { type ReactElement } from "react";
2
+ export declare enum FIELD_TYPE {
3
+ ID = "ID",
4
+ STRING = "STRING",
5
+ NUMBER = "NUMBER",
6
+ DECIMAL = "DECIMAL",
7
+ BOOLEAN = "BOOLEAN",
8
+ NONE = "NONE"
9
+ }
10
+ export declare enum OPERATOR {
11
+ EXISTS = "EXISTS",
12
+ IS = "IS",
13
+ IN = "IN",
14
+ LT = "LT",
15
+ GT = "GT",
16
+ LTE = "LTE",
17
+ GTE = "GTE",
18
+ INCLUDES = "INCLUDES",
19
+ STARTS = "STARTS",
20
+ ENDS = "ENDS",
21
+ WITHIN = "WITHIN",
22
+ BETWEEN = "BETWEEN"
23
+ }
24
+ /**
25
+ * @internal
26
+ */
27
+ export declare const TypeOperators: {
28
+ [key in FIELD_TYPE]: OPERATOR[];
29
+ };
30
+ /**
31
+ * @internal
32
+ */
33
+ export declare const OperatorValueType: {
34
+ [key in OPERATOR]: 0 | 1 | 2;
35
+ };
36
+ /**
37
+ * @internal
38
+ */
39
+ export type FilterValue = undefined | boolean | string | string[] | number | [number, number];
40
+ interface BaseFilter {
41
+ id: string;
42
+ icon?: string;
43
+ label?: string;
44
+ group?: string;
45
+ value?: FilterValue;
46
+ pinned?: boolean;
47
+ negate?: boolean;
48
+ disabled?: boolean;
49
+ required?: boolean;
50
+ canPin?: boolean;
51
+ canEdit?: boolean;
52
+ canInvert?: boolean;
53
+ canDisable?: boolean;
54
+ }
55
+ export type FilterObject = BaseFilter & ({
56
+ field: string;
57
+ type?: FIELD_TYPE;
58
+ operator: OPERATOR;
59
+ value?: FilterValue;
60
+ query?: never;
61
+ } | {
62
+ field?: never;
63
+ type?: never;
64
+ operator?: never;
65
+ label: string;
66
+ query: string;
67
+ });
68
+ export interface SearchObject {
69
+ query?: string | string[];
70
+ filters: FilterObject[];
71
+ }
72
+ export type FieldValue = string | {
73
+ value: string;
74
+ label: string;
75
+ icon?: AnyObject;
76
+ };
77
+ export interface FilterField {
78
+ field: string;
79
+ label: string;
80
+ type: FIELD_TYPE;
81
+ values?: FieldValue[];
82
+ defaultOperator?: OPERATOR;
83
+ onSearch?: (q: string) => Promise<FieldValue[]> | FieldValue[];
84
+ }
85
+ interface BaseSearchBarProps {
86
+ /**
87
+ * Query string
88
+ */
89
+ query?: AnyObject;
90
+ /**
91
+ * Search history count
92
+ * @default 20
93
+ */
94
+ historyCount?: number;
95
+ /**
96
+ * Search history storage key
97
+ * @default: "ax:search"
98
+ */
99
+ historyKey?: string;
100
+ /**
101
+ * Default query items for suggest
102
+ */
103
+ defaultQueryList?: string[];
104
+ /**
105
+ * Add-on before search input
106
+ */
107
+ prepend?: ReactElement;
108
+ /**
109
+ * Add-on after search input
110
+ */
111
+ append?: ReactElement;
112
+ /**
113
+ * Additional actions menu
114
+ */
115
+ actions?: ReactElement;
116
+ /**
117
+ * Hide filter bar
118
+ */
119
+ hideFilters?: boolean;
120
+ /**
121
+ * Disable component
122
+ */
123
+ disabled?: boolean;
124
+ /**
125
+ * Collapse filters
126
+ * @default true
127
+ */
128
+ defaultCollapsed?: boolean;
129
+ /**
130
+ *
131
+ */
132
+ decorateStart?: JSX.Element | string | number | boolean;
133
+ /**
134
+ *
135
+ */
136
+ decorateEnd?: JSX.Element | string | number | boolean;
137
+ /**
138
+ * On filter collapsed
139
+ * @param collapsed
140
+ */
141
+ onCollapsed?: (collapsed: boolean) => void;
142
+ /**
143
+ * On search event
144
+ * @param queryObject
145
+ */
146
+ onSearch?: (queryObject: SearchObject) => void;
147
+ /**
148
+ * On query string change event
149
+ * @param query
150
+ */
151
+ onQuery?: (query: string) => Promise<string[]> | string[];
152
+ }
153
+ export type SearchBarProps = BaseSearchBarProps & ({
154
+ multiple: true;
155
+ query?: string[];
156
+ } | {
157
+ multiple?: never | false;
158
+ query?: string;
159
+ });
160
+ export interface FilterBarProps {
161
+ /**
162
+ * Filters list
163
+ * @default []
164
+ */
165
+ filters?: FilterObject[];
166
+ /**
167
+ * Field list
168
+ * (Required when filter bar enabled)
169
+ */
170
+ fields?: FilterField[];
171
+ /**
172
+ * Editable filters
173
+ */
174
+ isEditable?: boolean;
175
+ /**
176
+ * Disable component
177
+ */
178
+ isDisabled?: boolean;
179
+ /**
180
+ * Message for empty field list
181
+ */
182
+ emptyFields?: string;
183
+ /**
184
+ * included filter chip color
185
+ */
186
+ includedColor?: string;
187
+ /**
188
+ * excluded filter chip color
189
+ */
190
+ excludedColor?: string;
191
+ allowAdd?: boolean;
192
+ querySchema?: Array<{
193
+ uri: string;
194
+ schema: KeyValue;
195
+ }>;
196
+ defaultQuery?: string;
197
+ /**
198
+ * On filters change, (add/update/delete)
199
+ * @param filters
200
+ */
201
+ onFilterChanged?: (filters: FilterObject[]) => void;
202
+ }
203
+ export {};
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "@react-fabric/searchbar",
3
+ "version": "1.0.0-beta.0",
4
+ "description": "RUF: Searchbar with filters package",
5
+ "author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
6
+ "license": "MIT",
7
+ "repository": "https://github.com/adarshpastakia/react-ui-framework/tree/main/packages/searchbar",
8
+ "source": "src/index.ts",
9
+ "main": "dist/cjs/index.js",
10
+ "module": "dist/esm/index.js",
11
+ "types": "dist/types/index.d.ts",
12
+ "directories": {
13
+ "src": "src",
14
+ "test": "tests"
15
+ },
16
+ "files": [
17
+ "src",
18
+ "dist"
19
+ ],
20
+ "publishConfig": {
21
+ "access": "public",
22
+ "registry": "https://registry.npmjs.org/"
23
+ },
24
+ "scripts": {
25
+ "makelink": "yarn link",
26
+ "removelink": "yarn unlink",
27
+ "prebuild": "rm -rf ./dist",
28
+ "build:dts": "tsc -p ./tsconfig.build.json",
29
+ "build:rollup": "rollup -c ../../rollup.config.mjs",
30
+ "build": "npm-run-all -p build:rollup build:dts",
31
+ "watch:dts": "tsc -w -p ./tsconfig.build.json",
32
+ "watch:rollup": "rollup -w -c ../../rollup.config.mjs",
33
+ "watch": "npm-run-all -p watch:rollup watch:dts"
34
+ },
35
+ "dependencies": {
36
+ "@floating-ui/react": "0.26.24",
37
+ "@react-fabric/core": "^1.0.0-beta.0",
38
+ "@react-fabric/date": "^1.0.0-beta.0",
39
+ "@react-fabric/form": "^1.0.0-beta.0",
40
+ "@react-fabric/monaco": "^1.0.0-beta.0",
41
+ "@react-fabric/utilities": "^1.0.0-beta.0",
42
+ "classnames": "^2.5.1",
43
+ "react-hook-form": "^7.52.1"
44
+ },
45
+ "peerDependencies": {
46
+ "i18next": "^23.15.1",
47
+ "react": ">=18.0.0 <20.0.0",
48
+ "react-dom": ">=18.0.0 <20.0.0",
49
+ "react-i18next": "^15.0.2"
50
+ },
51
+ "engines": {
52
+ "node": ">=18.12.0",
53
+ "npm": ">=8.19.0"
54
+ },
55
+ "gitHead": "83fa79064b1d57e71149005432192d506810bbda"
56
+ }
@@ -0,0 +1,31 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { createContext, useContext } from "react";
25
+ import { type FilterBarProps } from "../types";
26
+
27
+ export const FilterContext = createContext<
28
+ Pick<FilterBarProps, "querySchema" | "defaultQuery" | "fields">
29
+ >({});
30
+
31
+ export const useFilterContext = () => useContext(FilterContext);
@@ -0,0 +1,195 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import {
25
+ Chip,
26
+ CoreIcons,
27
+ Divider,
28
+ Dropdown,
29
+ Icon,
30
+ Menu,
31
+ MenuItem,
32
+ } from "@react-fabric/core";
33
+ import { isEmpty, isNil } from "@react-fabric/utilities";
34
+ import { useCallback, useMemo } from "react";
35
+ import { useTranslation } from "react-i18next";
36
+ import { type FilterBarProps, type FilterObject } from "../types";
37
+ import { FilterEdit } from "./FilterEdit";
38
+ import { FilterTag } from "./FilterTag";
39
+ import classNames from "classnames";
40
+ import { FilterContext } from "./Context";
41
+
42
+ export const FilterBar = ({
43
+ filters = [],
44
+ allowAdd,
45
+ fields,
46
+ querySchema,
47
+ defaultQuery,
48
+ includedColor,
49
+ excludedColor,
50
+ onFilterChanged,
51
+ }: FilterBarProps) => {
52
+ const { t } = useTranslation("searchbar");
53
+
54
+ const handleRemove = useCallback(
55
+ (index: number) => {
56
+ const list = [...filters];
57
+ list.splice(index, 1);
58
+ onFilterChanged?.(list);
59
+ },
60
+ [filters],
61
+ );
62
+ const handleChange = useCallback(
63
+ (filter: FilterObject, index?: number) => {
64
+ const list = [...filters];
65
+ isNil(index) ? list.push(filter) : list.splice(index, 1, filter);
66
+ onFilterChanged?.(list);
67
+ },
68
+ [filters],
69
+ );
70
+ const handleToggle = useCallback(
71
+ (
72
+ type:
73
+ | "disableAll"
74
+ | "enableAll"
75
+ | "toggleDisable"
76
+ | "excludeAll"
77
+ | "includeAll"
78
+ | "invertAll"
79
+ | "removeAll",
80
+ ) => {
81
+ const list = [];
82
+ if (type === "removeAll") {
83
+ list.push(...filters.filter((filter) => filter.required));
84
+ }
85
+ if (type !== "removeAll") {
86
+ list.push(
87
+ ...filters.map((filter) => {
88
+ if (type === "disableAll") filter.disabled = true;
89
+ if (type === "enableAll") filter.disabled = false;
90
+ if (type === "toggleDisable") filter.disabled = !filter.disabled;
91
+ if (type === "excludeAll") filter.negate = true;
92
+ if (type === "includeAll") filter.negate = false;
93
+ if (type === "invertAll") filter.negate = !filter.negate;
94
+ return { ...filter };
95
+ }),
96
+ );
97
+ }
98
+ onFilterChanged?.(list);
99
+ },
100
+ [filters],
101
+ );
102
+
103
+ const menuItems = useMemo(() => {
104
+ const key = {
105
+ someDisabled: false,
106
+ someEnabled: false,
107
+ someExcluded: false,
108
+ someIncluded: false,
109
+ canRemoveAll: false,
110
+ };
111
+ filters.forEach((filter) => {
112
+ if (filter.disabled && filter.canDisable !== false && !key.someDisabled)
113
+ key.someDisabled = true;
114
+ if (!filter.disabled && filter.canDisable !== false && !key.someEnabled)
115
+ key.someEnabled = true;
116
+ if (filter.negate && filter.canInvert !== false && !key.someExcluded)
117
+ key.someExcluded = true;
118
+ if (!filter.negate && filter.canInvert !== false && !key.someIncluded)
119
+ key.someIncluded = true;
120
+ if (!filter.required && !key.canRemoveAll) key.canRemoveAll = true;
121
+ });
122
+ return key;
123
+ }, [filters]);
124
+
125
+ return (
126
+ <FilterContext.Provider value={{ defaultQuery, querySchema, fields }}>
127
+ <div className="flex flex-wrap gap-1 items-center">
128
+ <Dropdown showArrow disabled={isEmpty(filters)}>
129
+ <Icon
130
+ icon={CoreIcons.config}
131
+ className={classNames(
132
+ isEmpty(filters) && "pointer-events-none opacity-30",
133
+ )}
134
+ color={!isEmpty(filters) ? "primary" : undefined}
135
+ />
136
+ <Menu className="text-sm" onClick={handleToggle as AnyObject}>
137
+ {menuItems.someDisabled && (
138
+ <MenuItem id="enableAll" label={t("label.enableAll")} />
139
+ )}
140
+ {menuItems.someEnabled && (
141
+ <MenuItem id="disableAll" label={t("label.disableAll")} />
142
+ )}
143
+ <MenuItem
144
+ icon={CoreIcons.invertDisable}
145
+ id="toggleDisable"
146
+ label={t("label.toggleDisable")}
147
+ />
148
+ <Divider />
149
+ {menuItems.someExcluded && (
150
+ <MenuItem id="includeAll" label={t("label.includeAll")} />
151
+ )}
152
+ {menuItems.someIncluded && (
153
+ <MenuItem id="excludeAll" label={t("label.excludeAll")} />
154
+ )}
155
+ <MenuItem
156
+ id="invertAll"
157
+ icon={CoreIcons.invertExclude}
158
+ label={t("label.invertAll")}
159
+ />
160
+ <Divider />
161
+ <MenuItem
162
+ id="removeAll"
163
+ color="danger"
164
+ icon={CoreIcons.remove}
165
+ label={t("label.removeAll")}
166
+ disabled={!menuItems.canRemoveAll}
167
+ />
168
+ </Menu>
169
+ </Dropdown>
170
+ {filters?.map((filter, idx) => (
171
+ <FilterTag
172
+ key={idx}
173
+ filter={filter}
174
+ includedColor={includedColor}
175
+ excludedColor={excludedColor}
176
+ onRemove={() => handleRemove(idx)}
177
+ onChange={(filter) => handleChange(filter, idx)}
178
+ />
179
+ ))}
180
+ {allowAdd && (
181
+ <Dropdown showArrow>
182
+ <Chip
183
+ size="sm"
184
+ className="!outline-dashed !outline-tint-400"
185
+ icon={CoreIcons.insert}
186
+ >
187
+ {t("label.add")}
188
+ </Chip>
189
+ <FilterEdit onChange={handleChange} />
190
+ </Dropdown>
191
+ )}
192
+ </div>
193
+ </FilterContext.Provider>
194
+ );
195
+ };