@react-fabric/searchbar 1.0.0-beta.4 → 1.0.0-beta.40

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.
@@ -33,7 +33,7 @@ import {
33
33
  Switch,
34
34
  type FormRef,
35
35
  } from "@react-fabric/form";
36
- import { shortHash, yup } from "@react-fabric/utilities";
36
+ import { isArray, shortHash, yup } from "@react-fabric/utilities";
37
37
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
38
38
  import { useTranslation } from "react-i18next";
39
39
  import {
@@ -48,6 +48,7 @@ import { useFilterContext } from "./Context";
48
48
  const FilterSchema = new yup.ObjectSchema({
49
49
  id: yup.string().required(),
50
50
  field: yup.string().required(),
51
+ // type: yup.string().oneOf(Object.values(FIELD_TYPE)),
51
52
  operator: yup.string().required().oneOf(Object.values(OPERATOR)),
52
53
  negate: yup.boolean(),
53
54
  label: yup.string(),
@@ -88,12 +89,15 @@ export const FilterForm = ({
88
89
  const [values, setValues] = useState<FilterSchemaType>(filter as AnyObject);
89
90
 
90
91
  useEffect(() => {
91
- if (filter && "field" in filter && filter.field) return setValues(filter);
92
- setValues({
93
- id: shortHash(),
94
- field: "",
95
- operator: OPERATOR.EXISTS,
96
- });
92
+ if (filter && "field" in filter && filter.field) {
93
+ formRef.current?.setValues(filter);
94
+ } else {
95
+ formRef.current?.setValues({
96
+ id: shortHash(),
97
+ field: "",
98
+ operator: OPERATOR.EXISTS,
99
+ });
100
+ }
97
101
  }, [filter]);
98
102
 
99
103
  useEffect(() => {
@@ -113,10 +117,27 @@ export const FilterForm = ({
113
117
  return [...TypeOperators[field.type]];
114
118
  }, [field]);
115
119
 
116
- useEffect(() => {
120
+ const valueInput = useMemo(() => {
117
121
  if (values?.operator === OPERATOR.EXISTS && values?.value !== undefined) {
118
122
  formRef.current?.setValue("value", undefined);
119
123
  }
124
+ if (field?.type === FIELD_TYPE.STRING) {
125
+ if (values?.operator === OPERATOR.IN) {
126
+ !isArray(values.value) && formRef.current?.setValue("value", []);
127
+ } else {
128
+ isArray(values.value) && formRef.current?.setValue("value", undefined);
129
+ }
130
+ }
131
+ if (
132
+ field?.type === FIELD_TYPE.NUMBER ||
133
+ field?.type === FIELD_TYPE.DECIMAL
134
+ ) {
135
+ if (values?.operator === OPERATOR.BETWEEN) {
136
+ !isArray(values.value) && formRef.current?.setValue("value", []);
137
+ } else {
138
+ isArray(values.value) && formRef.current?.setValue("value", undefined);
139
+ }
140
+ }
120
141
  if (
121
142
  field?.type === FIELD_TYPE.BOOLEAN &&
122
143
  values?.operator === OPERATOR.IS &&
@@ -124,9 +145,7 @@ export const FilterForm = ({
124
145
  ) {
125
146
  formRef.current?.setValue("value", false);
126
147
  }
127
- }, [values, field]);
128
148
 
129
- const valueInput = useMemo(() => {
130
149
  if (field == null || !values?.operator) return null;
131
150
  if (values?.operator === OPERATOR.EXISTS) return null;
132
151
  if (field?.type === FIELD_TYPE.BOOLEAN) {
@@ -144,6 +163,7 @@ export const FilterForm = ({
144
163
  <Controller name="value">
145
164
  <Select
146
165
  multiple
166
+ searchable
147
167
  allowCreate
148
168
  allowClear
149
169
  options={field.values ?? []}
@@ -171,7 +191,7 @@ export const FilterForm = ({
171
191
  <Controller name="value[0]">
172
192
  <Number step={step} allowClear />
173
193
  </Controller>
174
- <span className="p-2 text-muted">{`<>`}</span>
194
+ <span className="p-2 text-muted">{`≷`}</span>
175
195
  <Controller name="value[1]">
176
196
  <Number step={step} allowClear />
177
197
  </Controller>
@@ -211,70 +231,73 @@ export const FilterForm = ({
211
231
  onChange={setValues}
212
232
  onSubmit={handleSubmit}
213
233
  >
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>
234
+ <div className="w-[32rem]">
235
+ <Row>
236
+ <Col flex="fill">
237
+ <Controller name="field">
238
+ <Select<FilterField>
239
+ required
240
+ searchable
241
+ ref={inputRef}
242
+ label={t("label.field")}
243
+ options={fields}
244
+ labelProperty="label"
245
+ valueProperty="field"
246
+ groupProperty="type"
247
+ />
248
+ </Controller>
249
+ </Col>
250
+ <Col flex="auto" className="text-center">
251
+ <label className="block py-0.5 text-sm">{t("label.exclude")}</label>
252
+ <Controller name="negate">
253
+ <Switch color="danger" />
254
+ </Controller>
255
+ </Col>
256
+ <Col flex="auto" className="basis-28">
257
+ <Controller name="operator">
258
+ <Select
259
+ label={t("label.operator")}
260
+ options={operators}
261
+ renderer={(v) => t(`operator.${v}`)}
262
+ />
263
+ </Controller>
264
+ </Col>
265
+ </Row>
266
+ <br />
267
+ {valueInput}
268
+ <br />
269
+ <Controller name="label">
270
+ <Input
271
+ label={t("label.label")}
272
+ placeholder={t("label.label")}
273
+ decorateEnd={
274
+ !filter?.label && (
275
+ <span className="text-sm opacity-65 pe-4">Optional</span>
276
+ )
277
+ }
278
+ />
279
+ </Controller>
280
+ <div className="flex mt-4 justify-end gap-1 sticky bottom-1 bg-base">
281
+ {onRemove && (
282
+ <div className="flex-1">
283
+ <Button
284
+ data-dropdown-dismiss="true"
285
+ variant="link"
286
+ size="sm"
287
+ color="danger"
288
+ onClick={onRemove}
289
+ >
290
+ {t("label.remove")}
291
+ </Button>
292
+ </div>
293
+ )}
294
+ <Button data-dropdown-dismiss="true" variant="link" size="sm">
295
+ {t("label.cancel")}
296
+ </Button>
297
+ <Button type="submit" variant="solid" size="sm">
298
+ {t("label.apply")}
299
+ </Button>
300
+ </div>
278
301
  </div>
279
302
  </Form>
280
303
  );
@@ -68,7 +68,7 @@ export const FilterTag = ({
68
68
  }, [filter]);
69
69
 
70
70
  const color = useMemo(
71
- () => (filter.negate ? excludedColor : includedColor),
71
+ () => filter.color ?? (filter.negate ? excludedColor : includedColor),
72
72
  [filter, includedColor, excludedColor],
73
73
  );
74
74
 
@@ -90,16 +90,23 @@ export const FilterTag = ({
90
90
  onChange({ ...filter, disabled: !filter.disabled }),
91
91
  e.stopPropagation(),
92
92
  ]}
93
+ data-ref="filterTag"
94
+ data-test-value={filter.id}
93
95
  stopPropagation
94
96
  onRemove={filter.required ? undefined : () => onRemove()}
95
97
  >
96
98
  <div
97
99
  className={classNames(
98
- "flex gap-1",
100
+ "flex gap-1 items-center",
99
101
  filter.disabled && "line-through opacity-65",
100
102
  )}
101
103
  >
102
- {filter.pinned && <Icon icon={filter.icon ?? CoreIcons.pin} />}
104
+ {filter.pinned && (
105
+ <Icon
106
+ className="flex-content"
107
+ icon={filter.icon ?? CoreIcons.pin}
108
+ />
109
+ )}
103
110
  {label}
104
111
  </div>
105
112
  </Chip>
@@ -112,20 +112,31 @@ export const QueryForm = ({
112
112
  />
113
113
  </Controller>
114
114
  <Controller name="query">
115
- {({ checked, error, invalid, ...field }) => (
115
+ {({ checked, error, invalid, ref, ...field }) => (
116
116
  <div className="my-2">
117
117
  <label
118
118
  className={classNames("block px-2", invalid && "text-danger-600")}
119
119
  >
120
120
  {t("label.query")}
121
121
  </label>
122
- <div className="h-[380px] w-[600px] grid outline">
123
- <CodeEditor minimal required {...field} schema={querySchema} />
122
+ <div
123
+ className="h-[24rem] w-[36rem] grid outline place-content-start"
124
+ style={{
125
+ gridTemplate: '"content" 1fr/1fr',
126
+ }}
127
+ >
128
+ <CodeEditor
129
+ language="json"
130
+ minimal
131
+ required
132
+ {...field}
133
+ schema={querySchema}
134
+ />
124
135
  </div>
125
136
  </div>
126
137
  )}
127
138
  </Controller>
128
- <div className="flex justify-end gap-1">
139
+ <div className="flex mt-4 justify-end gap-1 sticky bottom-1 bg-base">
129
140
  {onRemove && (
130
141
  <div className="flex-1">
131
142
  <Button
package/src/index.ts CHANGED
@@ -23,10 +23,6 @@
23
23
 
24
24
  import "./i18n";
25
25
 
26
+ export { FilterBar } from "./filterbar/FilterBar";
26
27
  export { SearchBar } from "./searchbar/SearchBar";
27
- export {
28
- FIELD_TYPE,
29
- OPERATOR,
30
- type FilterObject,
31
- type SearchObject,
32
- } from "./types";
28
+ export { FIELD_TYPE, OPERATOR, type FilterObject } from "./types";
@@ -24,7 +24,7 @@
24
24
  import { Button, useDebounce, useLocalStorage } from "@react-fabric/core";
25
25
  import { AutoComplete, Field } from "@react-fabric/form";
26
26
  import { dedupe, EMPTY_ARRAY, isArray } from "@react-fabric/utilities";
27
- import { useMemo, useReducer, useState } from "react";
27
+ import { useEffect, useMemo, useReducer, useState } from "react";
28
28
  import { useTranslation } from "react-i18next";
29
29
  import { FilterBar } from "../filterbar/FilterBar";
30
30
  import {
@@ -40,6 +40,7 @@ interface SearchState {
40
40
  }
41
41
 
42
42
  type SearchActions =
43
+ | { type: "reset"; query: string | string[]; filters: FilterObject[] }
43
44
  | { type: "dirty"; dirty: boolean }
44
45
  | { type: "query"; query: string | string[] }
45
46
  | { type: "filter"; filters: FilterObject[] };
@@ -52,7 +53,7 @@ export const SearchBar = ({
52
53
  append,
53
54
  decorateEnd,
54
55
  decorateStart,
55
- actions,
56
+ children,
56
57
  hideFilters,
57
58
  defaultCollapsed = false,
58
59
  disabled,
@@ -73,15 +74,27 @@ export const SearchBar = ({
73
74
  const [collapsed, setCollapsed] = useState(defaultCollapsed);
74
75
  const emptyValue = useMemo(() => (multiple ? EMPTY_ARRAY : ""), [multiple]);
75
76
 
76
- const fireSearch = useDebounce((query: string[], filters: FilterObject[]) => {
77
- onSearch?.({
78
- query,
79
- filters,
80
- });
81
- }, []);
77
+ const fireSearch = useDebounce(
78
+ (query: AnyObject, filters: FilterObject[]) => {
79
+ dispatch({ type: "dirty", dirty: false });
80
+ onSearch?.({
81
+ query,
82
+ filters,
83
+ });
84
+ },
85
+ [],
86
+ );
82
87
 
83
88
  const [state, dispatch] = useReducer(
84
89
  (state: SearchState, action: SearchActions) => {
90
+ if (action.type === "reset") {
91
+ return {
92
+ ...state,
93
+ dirty: false,
94
+ query: action.query,
95
+ filters: action.filters,
96
+ };
97
+ }
85
98
  if (action.type === "dirty") {
86
99
  return { ...state, dirty: action.dirty };
87
100
  }
@@ -102,6 +115,10 @@ export const SearchBar = ({
102
115
  },
103
116
  );
104
117
 
118
+ useEffect(() => {
119
+ dispatch({ type: "reset", filters, query });
120
+ }, [filters, query]);
121
+
105
122
  const updateHistory = (value: string | string[]) => {
106
123
  const historyEntry = isArray(value) ? value : [value];
107
124
  setHistory(dedupe([...historyEntry, ...history]).slice(0, historyCount));
@@ -136,7 +153,7 @@ export const SearchBar = ({
136
153
  decorateStart={decorateStart}
137
154
  onInput={() => dispatch({ type: "dirty", dirty: true })}
138
155
  onSelect={updateHistory}
139
- onQuery={onQuery}
156
+ data-ref="searchbarInput"
140
157
  onEnterPressed={() => fireSearch(state.query, state.filters)}
141
158
  />
142
159
  {append}
@@ -148,7 +165,7 @@ export const SearchBar = ({
148
165
  {t(state.dirty ? "label.update" : "label.refresh")}
149
166
  </Button>
150
167
  </Field>
151
- {actions}
168
+ {children}
152
169
  </div>
153
170
  {!hideFilters && !collapsed && (
154
171
  <FilterBar
@@ -21,6 +21,7 @@
21
21
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
+ import { type ChildrenProp } from "@react-fabric/core/dist/types/types";
24
25
  import { type ReactElement } from "react";
25
26
 
26
27
  export enum FIELD_TYPE {
@@ -54,7 +55,7 @@ export enum OPERATOR {
54
55
  * @internal
55
56
  */
56
57
  export const TypeOperators: { [key in FIELD_TYPE]: OPERATOR[] } = {
57
- [FIELD_TYPE.ID]: [OPERATOR.IS],
58
+ [FIELD_TYPE.ID]: [OPERATOR.IS, OPERATOR.IN],
58
59
  [FIELD_TYPE.STRING]: [
59
60
  OPERATOR.EXISTS,
60
61
  OPERATOR.IS,
@@ -136,6 +137,7 @@ interface BaseFilter {
136
137
  icon?: string;
137
138
  label?: string;
138
139
  group?: string;
140
+ color?: string;
139
141
  value?: FilterValue;
140
142
  pinned?: boolean;
141
143
  negate?: boolean;
@@ -151,22 +153,20 @@ export type FilterObject = BaseFilter &
151
153
  (
152
154
  | {
153
155
  field: string;
154
- type?: FIELD_TYPE;
155
156
  operator: OPERATOR;
156
157
  value?: FilterValue;
157
158
  query?: never;
158
159
  }
159
160
  | {
160
161
  field?: never;
161
- type?: never;
162
162
  operator?: never;
163
163
  label: string;
164
164
  query: string;
165
165
  }
166
166
  );
167
167
 
168
- export interface SearchObject {
169
- query?: string | string[];
168
+ export interface SearchObject<T = string> {
169
+ query?: T;
170
170
  filters: FilterObject[];
171
171
  }
172
172
 
@@ -183,7 +183,7 @@ export interface FilterField {
183
183
  onSearch?: (q: string) => Promise<FieldValue[]> | FieldValue[];
184
184
  }
185
185
 
186
- interface BaseSearchBarProps {
186
+ interface BaseSearchBarProps extends Partial<ChildrenProp> {
187
187
  /**
188
188
  * Query string
189
189
  */
@@ -214,11 +214,6 @@ interface BaseSearchBarProps {
214
214
  */
215
215
  append?: ReactElement;
216
216
 
217
- /**
218
- * Additional actions menu
219
- */
220
- actions?: ReactElement;
221
-
222
217
  /**
223
218
  * Hide filter bar
224
219
  */
@@ -247,12 +242,6 @@ interface BaseSearchBarProps {
247
242
  * @param collapsed
248
243
  */
249
244
  onCollapsed?: (collapsed: boolean) => void;
250
-
251
- /**
252
- * On search event
253
- * @param queryObject
254
- */
255
- onSearch?: (queryObject: SearchObject) => void;
256
245
  /**
257
246
  * On query string change event
258
247
  * @param query
@@ -262,8 +251,26 @@ interface BaseSearchBarProps {
262
251
 
263
252
  export type SearchBarProps = BaseSearchBarProps &
264
253
  (
265
- | { multiple: true; query?: string[] }
266
- | { multiple?: never | false; query?: string }
254
+ | {
255
+ multiple: true;
256
+ query?: string[];
257
+
258
+ /**
259
+ * On search event
260
+ * @param queryObject
261
+ */
262
+ onSearch?: (queryObject: SearchObject<string[]>) => void;
263
+ }
264
+ | {
265
+ multiple?: never | false;
266
+ query?: string;
267
+
268
+ /**
269
+ * On search event
270
+ * @param queryObject
271
+ */
272
+ onSearch?: (queryObject: SearchObject) => void;
273
+ }
267
274
  );
268
275
 
269
276
  export interface FilterBarProps {