@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.
- package/LICENSE +21 -0
- package/README.md +10 -0
- package/dist/cjs/filterbar/Context.js +34 -0
- package/dist/cjs/filterbar/FilterBar.js +149 -0
- package/dist/cjs/filterbar/FilterEdit.js +51 -0
- package/dist/cjs/filterbar/FilterForm.js +262 -0
- package/dist/cjs/filterbar/FilterMenu.js +80 -0
- package/dist/cjs/filterbar/FilterTag.js +93 -0
- package/dist/cjs/filterbar/FilterView.js +88 -0
- package/dist/cjs/filterbar/QueryForm.js +135 -0
- package/dist/cjs/i18n/ar.json.js +84 -0
- package/dist/cjs/i18n/en.json.js +84 -0
- package/dist/cjs/i18n/index.js +33 -0
- package/dist/cjs/index.js +11 -0
- package/dist/cjs/searchbar/SearchBar.js +154 -0
- package/dist/cjs/types/index.js +70 -0
- package/dist/esm/filterbar/Context.js +33 -0
- package/dist/esm/filterbar/FilterBar.js +149 -0
- package/dist/esm/filterbar/FilterEdit.js +51 -0
- package/dist/esm/filterbar/FilterForm.js +262 -0
- package/dist/esm/filterbar/FilterMenu.js +80 -0
- package/dist/esm/filterbar/FilterTag.js +93 -0
- package/dist/esm/filterbar/FilterView.js +88 -0
- package/dist/esm/filterbar/QueryForm.js +135 -0
- package/dist/esm/i18n/ar.json.js +77 -0
- package/dist/esm/i18n/en.json.js +77 -0
- package/dist/esm/i18n/index.js +33 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/searchbar/SearchBar.js +154 -0
- package/dist/esm/types/index.js +68 -0
- package/dist/types/filterbar/Context.d.ts +4 -0
- package/dist/types/filterbar/FilterBar.d.ts +2 -0
- package/dist/types/filterbar/FilterEdit.d.ts +6 -0
- package/dist/types/filterbar/FilterForm.d.ts +6 -0
- package/dist/types/filterbar/FilterMenu.d.ts +7 -0
- package/dist/types/filterbar/FilterTag.d.ts +6 -0
- package/dist/types/filterbar/FilterView.d.ts +5 -0
- package/dist/types/filterbar/QueryForm.d.ts +6 -0
- package/dist/types/i18n/index.d.ts +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/searchbar/SearchBar.d.ts +2 -0
- package/dist/types/types/index.d.ts +203 -0
- package/package.json +56 -0
- package/src/filterbar/Context.tsx +31 -0
- package/src/filterbar/FilterBar.tsx +195 -0
- package/src/filterbar/FilterEdit.tsx +70 -0
- package/src/filterbar/FilterForm.tsx +281 -0
- package/src/filterbar/FilterMenu.tsx +100 -0
- package/src/filterbar/FilterTag.tsx +114 -0
- package/src/filterbar/FilterView.tsx +89 -0
- package/src/filterbar/QueryForm.tsx +151 -0
- package/src/i18n/ar.json +68 -0
- package/src/i18n/en.json +68 -0
- package/src/i18n/index.ts +28 -0
- package/src/index.ts +32 -0
- package/src/searchbar/SearchBar.tsx +162 -0
- package/src/types/index.ts +315 -0
|
@@ -0,0 +1,70 @@
|
|
|
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 { isEmpty } from "@react-fabric/utilities";
|
|
25
|
+
import { useEffect, useState } from "react";
|
|
26
|
+
import { useTranslation } from "react-i18next";
|
|
27
|
+
import { type FilterObject } from "../types";
|
|
28
|
+
import { useFilterContext } from "./Context";
|
|
29
|
+
import { FilterForm } from "./FilterForm";
|
|
30
|
+
import { QueryForm } from "./QueryForm";
|
|
31
|
+
|
|
32
|
+
export const FilterEdit = ({
|
|
33
|
+
filter,
|
|
34
|
+
onRemove,
|
|
35
|
+
onChange,
|
|
36
|
+
}: {
|
|
37
|
+
filter?: FilterObject;
|
|
38
|
+
onRemove?: () => void;
|
|
39
|
+
onChange: (filter: FilterObject) => void;
|
|
40
|
+
}) => {
|
|
41
|
+
const { t } = useTranslation("searchbar");
|
|
42
|
+
const { fields } = useFilterContext();
|
|
43
|
+
const [queryEdit, setQueryEdit] = useState(isEmpty(fields));
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
setQueryEdit(!!filter && "query" in filter);
|
|
47
|
+
}, [filter]);
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<div className="px-2 py-1 bg-base outline min-w-[420px]">
|
|
51
|
+
{!isEmpty(fields) && (
|
|
52
|
+
<div className="pb-4 flex justify-end">
|
|
53
|
+
<span
|
|
54
|
+
role="none"
|
|
55
|
+
className="text-sm link"
|
|
56
|
+
onClick={() => setQueryEdit(!queryEdit)}
|
|
57
|
+
>
|
|
58
|
+
{t(queryEdit ? "label.basicFilter" : "label.customQuery")}
|
|
59
|
+
</span>
|
|
60
|
+
</div>
|
|
61
|
+
)}
|
|
62
|
+
{!queryEdit && (
|
|
63
|
+
<FilterForm filter={filter} onRemove={onRemove} onChange={onChange} />
|
|
64
|
+
)}
|
|
65
|
+
{queryEdit && (
|
|
66
|
+
<QueryForm filter={filter} onRemove={onRemove} onChange={onChange} />
|
|
67
|
+
)}
|
|
68
|
+
</div>
|
|
69
|
+
);
|
|
70
|
+
};
|
|
@@ -0,0 +1,281 @@
|
|
|
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 { useFloatingTree } from "@floating-ui/react";
|
|
25
|
+
import { Button, Col, Row } from "@react-fabric/core";
|
|
26
|
+
import {
|
|
27
|
+
Controller,
|
|
28
|
+
Field,
|
|
29
|
+
Form,
|
|
30
|
+
Input,
|
|
31
|
+
Number,
|
|
32
|
+
Select,
|
|
33
|
+
Switch,
|
|
34
|
+
type FormRef,
|
|
35
|
+
} from "@react-fabric/form";
|
|
36
|
+
import { shortHash, yup } from "@react-fabric/utilities";
|
|
37
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
38
|
+
import { useTranslation } from "react-i18next";
|
|
39
|
+
import {
|
|
40
|
+
FIELD_TYPE,
|
|
41
|
+
OPERATOR,
|
|
42
|
+
TypeOperators,
|
|
43
|
+
type FilterField,
|
|
44
|
+
type FilterObject,
|
|
45
|
+
} from "../types";
|
|
46
|
+
import { useFilterContext } from "./Context";
|
|
47
|
+
|
|
48
|
+
const FilterSchema = new yup.ObjectSchema({
|
|
49
|
+
id: yup.string().required(),
|
|
50
|
+
field: yup.string().required(),
|
|
51
|
+
operator: yup.string().required().oneOf(Object.values(OPERATOR)),
|
|
52
|
+
negate: yup.boolean(),
|
|
53
|
+
label: yup.string(),
|
|
54
|
+
value: yup
|
|
55
|
+
.mixed()
|
|
56
|
+
.when("operator", {
|
|
57
|
+
is: OPERATOR.EXISTS,
|
|
58
|
+
then: (schema) => schema.optional(),
|
|
59
|
+
otherwise: (schema) => schema.required(),
|
|
60
|
+
})
|
|
61
|
+
.when("operator", {
|
|
62
|
+
is: OPERATOR.IN,
|
|
63
|
+
then: (schema) =>
|
|
64
|
+
schema.test({
|
|
65
|
+
name: "array-check",
|
|
66
|
+
message: `\${path} is a required field`,
|
|
67
|
+
test: (val: AnyObject = []) => val?.length > 0,
|
|
68
|
+
}),
|
|
69
|
+
}),
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
type FilterSchemaType = yup.InferType<typeof FilterSchema>;
|
|
73
|
+
|
|
74
|
+
export const FilterForm = ({
|
|
75
|
+
filter,
|
|
76
|
+
onChange,
|
|
77
|
+
onRemove,
|
|
78
|
+
}: {
|
|
79
|
+
filter?: FilterObject;
|
|
80
|
+
onRemove?: () => void;
|
|
81
|
+
onChange?: (filter: FilterObject) => void;
|
|
82
|
+
}) => {
|
|
83
|
+
const { t } = useTranslation("searchbar");
|
|
84
|
+
const tree = useFloatingTree();
|
|
85
|
+
const { fields = [] } = useFilterContext();
|
|
86
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
87
|
+
const formRef = useRef<FormRef<FilterSchemaType>>(null);
|
|
88
|
+
const [values, setValues] = useState<FilterSchemaType>(filter as AnyObject);
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (filter && "field" in filter && filter.field) return setValues(filter);
|
|
92
|
+
setValues({
|
|
93
|
+
id: shortHash(),
|
|
94
|
+
field: "",
|
|
95
|
+
operator: OPERATOR.EXISTS,
|
|
96
|
+
});
|
|
97
|
+
}, [filter]);
|
|
98
|
+
|
|
99
|
+
useEffect(() => {
|
|
100
|
+
setTimeout(() => {
|
|
101
|
+
inputRef.current?.focus();
|
|
102
|
+
}, 100);
|
|
103
|
+
}, []);
|
|
104
|
+
|
|
105
|
+
const field = useMemo<FilterField | undefined>(() => {
|
|
106
|
+
if (!values?.field) return undefined;
|
|
107
|
+
return fields.find((f) => f.field === values?.field);
|
|
108
|
+
}, [values]);
|
|
109
|
+
|
|
110
|
+
const operators = useMemo(() => {
|
|
111
|
+
if (field == null) return [OPERATOR.EXISTS];
|
|
112
|
+
formRef.current?.setValue("operator", TypeOperators[field.type][0]);
|
|
113
|
+
return [...TypeOperators[field.type]];
|
|
114
|
+
}, [field]);
|
|
115
|
+
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (values?.operator === OPERATOR.EXISTS && values?.value !== undefined) {
|
|
118
|
+
formRef.current?.setValue("value", undefined);
|
|
119
|
+
}
|
|
120
|
+
if (
|
|
121
|
+
field?.type === FIELD_TYPE.BOOLEAN &&
|
|
122
|
+
values?.operator === OPERATOR.IS &&
|
|
123
|
+
values?.value === undefined
|
|
124
|
+
) {
|
|
125
|
+
formRef.current?.setValue("value", false);
|
|
126
|
+
}
|
|
127
|
+
}, [values, field]);
|
|
128
|
+
|
|
129
|
+
const valueInput = useMemo(() => {
|
|
130
|
+
if (field == null || !values?.operator) return null;
|
|
131
|
+
if (values?.operator === OPERATOR.EXISTS) return null;
|
|
132
|
+
if (field?.type === FIELD_TYPE.BOOLEAN) {
|
|
133
|
+
return (
|
|
134
|
+
<Field label={t("label.value")} data-plain="true">
|
|
135
|
+
<Controller name="value">
|
|
136
|
+
<Switch />
|
|
137
|
+
</Controller>
|
|
138
|
+
</Field>
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
if (field?.type === FIELD_TYPE.STRING || field?.type === FIELD_TYPE.ID) {
|
|
142
|
+
if (values?.operator === OPERATOR.IN) {
|
|
143
|
+
return (
|
|
144
|
+
<Controller name="value">
|
|
145
|
+
<Select
|
|
146
|
+
multiple
|
|
147
|
+
allowCreate
|
|
148
|
+
allowClear
|
|
149
|
+
options={field.values ?? []}
|
|
150
|
+
label={t("label.value")}
|
|
151
|
+
onQuery={field.onSearch}
|
|
152
|
+
/>
|
|
153
|
+
</Controller>
|
|
154
|
+
);
|
|
155
|
+
} else {
|
|
156
|
+
return (
|
|
157
|
+
<Controller name="value">
|
|
158
|
+
<Input label={t("label.value")} allowClear />
|
|
159
|
+
</Controller>
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (
|
|
164
|
+
field?.type === FIELD_TYPE.DECIMAL ||
|
|
165
|
+
field?.type === FIELD_TYPE.NUMBER
|
|
166
|
+
) {
|
|
167
|
+
const step = field.type === FIELD_TYPE.DECIMAL ? 0.1 : 1;
|
|
168
|
+
if (values?.operator === OPERATOR.BETWEEN) {
|
|
169
|
+
return (
|
|
170
|
+
<Field label={t("label.value")}>
|
|
171
|
+
<Controller name="value[0]">
|
|
172
|
+
<Number step={step} allowClear />
|
|
173
|
+
</Controller>
|
|
174
|
+
<span className="p-2 text-muted">{`<>`}</span>
|
|
175
|
+
<Controller name="value[1]">
|
|
176
|
+
<Number step={step} allowClear />
|
|
177
|
+
</Controller>
|
|
178
|
+
</Field>
|
|
179
|
+
);
|
|
180
|
+
} else {
|
|
181
|
+
return (
|
|
182
|
+
<Controller name="value">
|
|
183
|
+
<Number label={t("label.value")} step={step} allowClear />
|
|
184
|
+
</Controller>
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}, [field, values?.operator]);
|
|
189
|
+
|
|
190
|
+
const handleSubmit = useCallback(
|
|
191
|
+
(model: FilterSchemaType) => {
|
|
192
|
+
const filter: FilterObject = {
|
|
193
|
+
id: model.id,
|
|
194
|
+
field: model.field,
|
|
195
|
+
negate: model.negate,
|
|
196
|
+
operator: model.operator,
|
|
197
|
+
value: model.value as AnyObject,
|
|
198
|
+
label: model.label,
|
|
199
|
+
};
|
|
200
|
+
onChange?.(filter);
|
|
201
|
+
tree?.events.emit("close");
|
|
202
|
+
},
|
|
203
|
+
[onChange],
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
return (
|
|
207
|
+
<Form<FilterSchemaType>
|
|
208
|
+
formRef={formRef}
|
|
209
|
+
schema={FilterSchema}
|
|
210
|
+
defaultValues={values}
|
|
211
|
+
onChange={setValues}
|
|
212
|
+
onSubmit={handleSubmit}
|
|
213
|
+
>
|
|
214
|
+
<Row>
|
|
215
|
+
<Col flex="fill">
|
|
216
|
+
<Controller name="field">
|
|
217
|
+
<Select<FilterField>
|
|
218
|
+
required
|
|
219
|
+
ref={inputRef}
|
|
220
|
+
label={t("label.field")}
|
|
221
|
+
options={fields}
|
|
222
|
+
labelProperty="label"
|
|
223
|
+
valueProperty="field"
|
|
224
|
+
groupProperty="type"
|
|
225
|
+
/>
|
|
226
|
+
</Controller>
|
|
227
|
+
</Col>
|
|
228
|
+
<Col flex="auto" className="text-center">
|
|
229
|
+
<label className="block py-0.5 text-sm">{t("label.exclude")}</label>
|
|
230
|
+
<Controller name="negate">
|
|
231
|
+
<Switch color="danger" />
|
|
232
|
+
</Controller>
|
|
233
|
+
</Col>
|
|
234
|
+
<Col flex="auto" className="basis-28">
|
|
235
|
+
<Controller name="operator">
|
|
236
|
+
<Select
|
|
237
|
+
label={t("label.operator")}
|
|
238
|
+
options={operators}
|
|
239
|
+
renderer={(v) => t(`operator.${v}`)}
|
|
240
|
+
/>
|
|
241
|
+
</Controller>
|
|
242
|
+
</Col>
|
|
243
|
+
</Row>
|
|
244
|
+
<br />
|
|
245
|
+
{valueInput}
|
|
246
|
+
<br />
|
|
247
|
+
<Controller name="label">
|
|
248
|
+
<Input
|
|
249
|
+
label={t("label.label")}
|
|
250
|
+
placeholder={t("label.label")}
|
|
251
|
+
decorateEnd={
|
|
252
|
+
!filter?.label && (
|
|
253
|
+
<span className="text-sm opacity-65 pe-4">Optional</span>
|
|
254
|
+
)
|
|
255
|
+
}
|
|
256
|
+
/>
|
|
257
|
+
</Controller>
|
|
258
|
+
<div className="flex pt-4 justify-end gap-1">
|
|
259
|
+
{onRemove && (
|
|
260
|
+
<div className="flex-1">
|
|
261
|
+
<Button
|
|
262
|
+
data-dropdown-dismiss="true"
|
|
263
|
+
variant="link"
|
|
264
|
+
size="sm"
|
|
265
|
+
color="danger"
|
|
266
|
+
onClick={onRemove}
|
|
267
|
+
>
|
|
268
|
+
{t("label.remove")}
|
|
269
|
+
</Button>
|
|
270
|
+
</div>
|
|
271
|
+
)}
|
|
272
|
+
<Button data-dropdown-dismiss="true" variant="link" size="sm">
|
|
273
|
+
{t("label.cancel")}
|
|
274
|
+
</Button>
|
|
275
|
+
<Button type="submit" variant="solid" size="sm">
|
|
276
|
+
{t("label.apply")}
|
|
277
|
+
</Button>
|
|
278
|
+
</div>
|
|
279
|
+
</Form>
|
|
280
|
+
);
|
|
281
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
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 { CoreIcons, Divider, Menu, MenuItem } from "@react-fabric/core";
|
|
25
|
+
import { useState } from "react";
|
|
26
|
+
import { useTranslation } from "react-i18next";
|
|
27
|
+
import { type FilterField, type FilterObject } from "../types";
|
|
28
|
+
import { FilterEdit } from "./FilterEdit";
|
|
29
|
+
import { FilterView } from "./FilterView";
|
|
30
|
+
|
|
31
|
+
export const FilterMenu = ({
|
|
32
|
+
filter,
|
|
33
|
+
fields,
|
|
34
|
+
onChange,
|
|
35
|
+
onRemove,
|
|
36
|
+
}: {
|
|
37
|
+
fields?: FilterField[];
|
|
38
|
+
filter: FilterObject;
|
|
39
|
+
onRemove: () => void;
|
|
40
|
+
onChange: (filter: FilterObject) => void;
|
|
41
|
+
}) => {
|
|
42
|
+
const { t } = useTranslation("searchbar");
|
|
43
|
+
const [view, setView] = useState<"view" | "edit">();
|
|
44
|
+
|
|
45
|
+
return view === "edit" ? (
|
|
46
|
+
<FilterEdit filter={filter} onChange={onChange} onRemove={onRemove} />
|
|
47
|
+
) : view === "view" ? (
|
|
48
|
+
<FilterView filter={filter} onRemove={onRemove} />
|
|
49
|
+
) : (
|
|
50
|
+
<Menu className="text-sm">
|
|
51
|
+
{filter.canEdit !== false ? (
|
|
52
|
+
<MenuItem
|
|
53
|
+
icon={CoreIcons.edit}
|
|
54
|
+
label={t("label.edit")}
|
|
55
|
+
data-dropdown-dismiss="false"
|
|
56
|
+
onClick={() => {
|
|
57
|
+
setView("edit");
|
|
58
|
+
}}
|
|
59
|
+
/>
|
|
60
|
+
) : (
|
|
61
|
+
<MenuItem
|
|
62
|
+
icon={CoreIcons.eye}
|
|
63
|
+
label={t("label.view")}
|
|
64
|
+
data-dropdown-dismiss="false"
|
|
65
|
+
onClick={() => {
|
|
66
|
+
setView("view");
|
|
67
|
+
}}
|
|
68
|
+
/>
|
|
69
|
+
)}
|
|
70
|
+
<Divider />
|
|
71
|
+
{filter.canPin !== false && (
|
|
72
|
+
<MenuItem
|
|
73
|
+
label={t(filter.pinned ? "label.unpin" : "label.pin")}
|
|
74
|
+
onClick={() => onChange({ ...filter, pinned: !filter.pinned })}
|
|
75
|
+
/>
|
|
76
|
+
)}
|
|
77
|
+
{filter.canInvert !== false && (
|
|
78
|
+
<MenuItem
|
|
79
|
+
label={t(filter.negate ? "label.include" : "label.exclude")}
|
|
80
|
+
onClick={() => onChange({ ...filter, negate: !filter.negate })}
|
|
81
|
+
/>
|
|
82
|
+
)}
|
|
83
|
+
{filter.canDisable !== false && (
|
|
84
|
+
<MenuItem
|
|
85
|
+
label={t(filter.disabled ? "label.enable" : "label.disable")}
|
|
86
|
+
onClick={() => onChange({ ...filter, disabled: !filter.disabled })}
|
|
87
|
+
/>
|
|
88
|
+
)}
|
|
89
|
+
{!filter.required && <Divider />}
|
|
90
|
+
{!filter.required && (
|
|
91
|
+
<MenuItem
|
|
92
|
+
color="danger"
|
|
93
|
+
icon={CoreIcons.remove}
|
|
94
|
+
label={t("label.remove")}
|
|
95
|
+
onClick={() => onRemove()}
|
|
96
|
+
/>
|
|
97
|
+
)}
|
|
98
|
+
</Menu>
|
|
99
|
+
);
|
|
100
|
+
};
|
|
@@ -0,0 +1,114 @@
|
|
|
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 { Chip, CoreIcons, Dropdown, Icon } from "@react-fabric/core";
|
|
25
|
+
import { isNil } from "@react-fabric/utilities";
|
|
26
|
+
import classNames from "classnames";
|
|
27
|
+
import { Fragment, useMemo } from "react";
|
|
28
|
+
import { useTranslation } from "react-i18next";
|
|
29
|
+
import { type FilterBarProps, type FilterObject } from "../types";
|
|
30
|
+
import { FilterMenu } from "./FilterMenu";
|
|
31
|
+
|
|
32
|
+
export const FilterTag = ({
|
|
33
|
+
filter,
|
|
34
|
+
fields,
|
|
35
|
+
includedColor = "primary-500",
|
|
36
|
+
excludedColor = "danger-500",
|
|
37
|
+
onChange,
|
|
38
|
+
onRemove,
|
|
39
|
+
}: {
|
|
40
|
+
filter: FilterObject;
|
|
41
|
+
onRemove: () => void;
|
|
42
|
+
onChange: (filter: FilterObject) => void;
|
|
43
|
+
} & Pick<FilterBarProps, "fields" | "includedColor" | "excludedColor">) => {
|
|
44
|
+
const { t } = useTranslation("searchbar");
|
|
45
|
+
const label = useMemo(() => {
|
|
46
|
+
if (filter.label)
|
|
47
|
+
return (
|
|
48
|
+
<Fragment>
|
|
49
|
+
{filter.negate && <span className="font-semibold">NOT</span>}
|
|
50
|
+
<span>{filter.label}</span>
|
|
51
|
+
</Fragment>
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<Fragment>
|
|
56
|
+
{filter.negate && <span className="font-semibold">NOT</span>}
|
|
57
|
+
<span>{filter.field}</span>
|
|
58
|
+
<span className="font-semibold">
|
|
59
|
+
{t(`operator.${filter.operator}`, { defaultValue: filter.operator })}
|
|
60
|
+
</span>
|
|
61
|
+
{"value" in filter && !isNil(filter.value) && (
|
|
62
|
+
<span className="truncate block">
|
|
63
|
+
{filter.value?.toString().substring(0, 24)}
|
|
64
|
+
</span>
|
|
65
|
+
)}
|
|
66
|
+
</Fragment>
|
|
67
|
+
);
|
|
68
|
+
}, [filter]);
|
|
69
|
+
|
|
70
|
+
const color = useMemo(
|
|
71
|
+
() => (filter.negate ? excludedColor : includedColor),
|
|
72
|
+
[filter, includedColor, excludedColor],
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const icon = useMemo(() => {
|
|
76
|
+
if (filter.canDisable === false) return undefined;
|
|
77
|
+
return filter.disabled ? CoreIcons.checkboxOff : CoreIcons.checkboxOn;
|
|
78
|
+
}, [filter]);
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<Dropdown showArrow>
|
|
82
|
+
<Chip
|
|
83
|
+
size="sm"
|
|
84
|
+
color={color}
|
|
85
|
+
className="outline outline-1 max-w-60"
|
|
86
|
+
variant={filter.pinned ? "solid" : undefined}
|
|
87
|
+
icon={icon}
|
|
88
|
+
iconColor={color}
|
|
89
|
+
onIconClick={(e) => [
|
|
90
|
+
onChange({ ...filter, disabled: !filter.disabled }),
|
|
91
|
+
e.stopPropagation(),
|
|
92
|
+
]}
|
|
93
|
+
stopPropagation
|
|
94
|
+
onRemove={filter.required ? undefined : () => onRemove()}
|
|
95
|
+
>
|
|
96
|
+
<div
|
|
97
|
+
className={classNames(
|
|
98
|
+
"flex gap-1",
|
|
99
|
+
filter.disabled && "line-through opacity-65",
|
|
100
|
+
)}
|
|
101
|
+
>
|
|
102
|
+
{filter.pinned && <Icon icon={filter.icon ?? CoreIcons.pin} />}
|
|
103
|
+
{label}
|
|
104
|
+
</div>
|
|
105
|
+
</Chip>
|
|
106
|
+
<FilterMenu
|
|
107
|
+
filter={filter}
|
|
108
|
+
fields={fields}
|
|
109
|
+
onChange={onChange}
|
|
110
|
+
onRemove={onRemove}
|
|
111
|
+
/>
|
|
112
|
+
</Dropdown>
|
|
113
|
+
);
|
|
114
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
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 { Button, Copy } from "@react-fabric/core";
|
|
25
|
+
import { isArray } from "@react-fabric/utilities";
|
|
26
|
+
import { Fragment, useMemo } from "react";
|
|
27
|
+
import { useTranslation } from "react-i18next";
|
|
28
|
+
import { type FilterObject, OPERATOR } from "../types";
|
|
29
|
+
|
|
30
|
+
export const FilterView = ({
|
|
31
|
+
filter,
|
|
32
|
+
onRemove,
|
|
33
|
+
}: {
|
|
34
|
+
filter: FilterObject;
|
|
35
|
+
onRemove: () => void;
|
|
36
|
+
}) => {
|
|
37
|
+
const { t } = useTranslation("searchbar");
|
|
38
|
+
|
|
39
|
+
const view = useMemo(() => {
|
|
40
|
+
if ("query" in filter)
|
|
41
|
+
return JSON.stringify(JSON.parse(filter.query ?? "{}"), null, 2);
|
|
42
|
+
|
|
43
|
+
if ("field" in filter) {
|
|
44
|
+
let value = filter.value?.toString();
|
|
45
|
+
|
|
46
|
+
if (filter.operator === OPERATOR.BETWEEN && isArray(filter.value)) {
|
|
47
|
+
value = `[${filter.value?.join(" - ")}]`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (filter.operator === OPERATOR.IN && isArray(filter.value)) {
|
|
51
|
+
value = `[${filter.value?.join(", ")}]`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return (
|
|
55
|
+
<Fragment>
|
|
56
|
+
{filter.negate && <b>NOT </b>}
|
|
57
|
+
<span>{filter.field}</span>
|
|
58
|
+
<b> {t(`operator.${filter.operator}`)} </b>
|
|
59
|
+
<div>{value}</div>
|
|
60
|
+
</Fragment>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return "";
|
|
64
|
+
}, [filter]);
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<div className="p-2 bg-base relative">
|
|
68
|
+
{"query" in filter && (
|
|
69
|
+
<div className="absolute top-2 end-2 z-10">
|
|
70
|
+
<Copy size="sm" text={view as AnyObject} />
|
|
71
|
+
</div>
|
|
72
|
+
)}
|
|
73
|
+
<pre className="overflow-auto bg-base outline text-sm p-2 max-h-[24rem] max-w-[48rem] min-w-[24rem]">
|
|
74
|
+
{view}
|
|
75
|
+
</pre>
|
|
76
|
+
{!filter.required && (
|
|
77
|
+
<Button
|
|
78
|
+
size="sm"
|
|
79
|
+
variant="link"
|
|
80
|
+
color="danger"
|
|
81
|
+
data-dropdown-dismiss="true"
|
|
82
|
+
onClick={onRemove}
|
|
83
|
+
>
|
|
84
|
+
{t("label.remove")}
|
|
85
|
+
</Button>
|
|
86
|
+
)}
|
|
87
|
+
</div>
|
|
88
|
+
);
|
|
89
|
+
};
|