@react-fabric/searchbar 1.0.0-beta.1 → 1.0.0-beta.11

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().required().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,10 +89,12 @@ 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({
92
+ if (filter && "field" in filter && filter.field)
93
+ return formRef.current?.setValues(filter);
94
+ formRef.current?.setValues({
93
95
  id: shortHash(),
94
96
  field: "",
97
+ type: FIELD_TYPE.NONE,
95
98
  operator: OPERATOR.EXISTS,
96
99
  });
97
100
  }, [filter]);
@@ -113,10 +116,27 @@ export const FilterForm = ({
113
116
  return [...TypeOperators[field.type]];
114
117
  }, [field]);
115
118
 
116
- useEffect(() => {
119
+ const valueInput = useMemo(() => {
117
120
  if (values?.operator === OPERATOR.EXISTS && values?.value !== undefined) {
118
121
  formRef.current?.setValue("value", undefined);
119
122
  }
123
+ if (field?.type === FIELD_TYPE.STRING) {
124
+ if (values?.operator === OPERATOR.IN) {
125
+ !isArray(values.value) && formRef.current?.setValue("value", []);
126
+ } else {
127
+ isArray(values.value) && formRef.current?.setValue("value", undefined);
128
+ }
129
+ }
130
+ if (
131
+ field?.type === FIELD_TYPE.NUMBER ||
132
+ field?.type === FIELD_TYPE.DECIMAL
133
+ ) {
134
+ if (values?.operator === OPERATOR.BETWEEN) {
135
+ !isArray(values.value) && formRef.current?.setValue("value", []);
136
+ } else {
137
+ isArray(values.value) && formRef.current?.setValue("value", undefined);
138
+ }
139
+ }
120
140
  if (
121
141
  field?.type === FIELD_TYPE.BOOLEAN &&
122
142
  values?.operator === OPERATOR.IS &&
@@ -124,9 +144,7 @@ export const FilterForm = ({
124
144
  ) {
125
145
  formRef.current?.setValue("value", false);
126
146
  }
127
- }, [values, field]);
128
147
 
129
- const valueInput = useMemo(() => {
130
148
  if (field == null || !values?.operator) return null;
131
149
  if (values?.operator === OPERATOR.EXISTS) return null;
132
150
  if (field?.type === FIELD_TYPE.BOOLEAN) {
@@ -144,6 +162,7 @@ export const FilterForm = ({
144
162
  <Controller name="value">
145
163
  <Select
146
164
  multiple
165
+ searchable
147
166
  allowCreate
148
167
  allowClear
149
168
  options={field.values ?? []}
@@ -171,7 +190,7 @@ export const FilterForm = ({
171
190
  <Controller name="value[0]">
172
191
  <Number step={step} allowClear />
173
192
  </Controller>
174
- <span className="p-2 text-muted">{`<>`}</span>
193
+ <span className="p-2 text-muted">{`≷`}</span>
175
194
  <Controller name="value[1]">
176
195
  <Number step={step} allowClear />
177
196
  </Controller>
@@ -192,6 +211,7 @@ export const FilterForm = ({
192
211
  const filter: FilterObject = {
193
212
  id: model.id,
194
213
  field: model.field,
214
+ type: model.type,
195
215
  negate: model.negate,
196
216
  operator: model.operator,
197
217
  value: model.value as AnyObject,
@@ -211,70 +231,75 @@ 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
+ ref={inputRef}
241
+ label={t("label.field")}
242
+ options={fields}
243
+ labelProperty="label"
244
+ valueProperty="field"
245
+ groupProperty="type"
246
+ onSelect={(field) =>
247
+ formRef.current?.setValue("type", field.type)
248
+ }
249
+ />
250
+ </Controller>
251
+ </Col>
252
+ <Col flex="auto" className="text-center">
253
+ <label className="block py-0.5 text-sm">{t("label.exclude")}</label>
254
+ <Controller name="negate">
255
+ <Switch color="danger" />
256
+ </Controller>
257
+ </Col>
258
+ <Col flex="auto" className="basis-28">
259
+ <Controller name="operator">
260
+ <Select
261
+ label={t("label.operator")}
262
+ options={operators}
263
+ renderer={(v) => t(`operator.${v}`)}
264
+ />
265
+ </Controller>
266
+ </Col>
267
+ </Row>
268
+ <br />
269
+ {valueInput}
270
+ <br />
271
+ <Controller name="label">
272
+ <Input
273
+ label={t("label.label")}
274
+ placeholder={t("label.label")}
275
+ decorateEnd={
276
+ !filter?.label && (
277
+ <span className="text-sm opacity-65 pe-4">Optional</span>
278
+ )
279
+ }
280
+ />
281
+ </Controller>
282
+ <div className="flex pt-4 justify-end gap-1">
283
+ {onRemove && (
284
+ <div className="flex-1">
285
+ <Button
286
+ data-dropdown-dismiss="true"
287
+ variant="link"
288
+ size="sm"
289
+ color="danger"
290
+ onClick={onRemove}
291
+ >
292
+ {t("label.remove")}
293
+ </Button>
294
+ </div>
295
+ )}
296
+ <Button data-dropdown-dismiss="true" variant="link" size="sm">
297
+ {t("label.cancel")}
298
+ </Button>
299
+ <Button type="submit" variant="solid" size="sm">
300
+ {t("label.apply")}
301
+ </Button>
302
+ </div>
278
303
  </div>
279
304
  </Form>
280
305
  );
@@ -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-testid="filter-tag"
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>
@@ -119,8 +119,19 @@ export const QueryForm = ({
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
  )}
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,21 @@ 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
+ onSearch?.({
80
+ query,
81
+ filters,
82
+ });
83
+ },
84
+ [],
85
+ );
82
86
 
83
87
  const [state, dispatch] = useReducer(
84
88
  (state: SearchState, action: SearchActions) => {
89
+ if (action.type === "reset") {
90
+ return { ...state, query: action.query, filters: action.filters };
91
+ }
85
92
  if (action.type === "dirty") {
86
93
  return { ...state, dirty: action.dirty };
87
94
  }
@@ -102,6 +109,10 @@ export const SearchBar = ({
102
109
  },
103
110
  );
104
111
 
112
+ useEffect(() => {
113
+ dispatch({ type: "reset", filters, query });
114
+ }, [filters, query]);
115
+
105
116
  const updateHistory = (value: string | string[]) => {
106
117
  const historyEntry = isArray(value) ? value : [value];
107
118
  setHistory(dedupe([...historyEntry, ...history]).slice(0, historyCount));
@@ -136,7 +147,7 @@ export const SearchBar = ({
136
147
  decorateStart={decorateStart}
137
148
  onInput={() => dispatch({ type: "dirty", dirty: true })}
138
149
  onSelect={updateHistory}
139
- onQuery={onQuery}
150
+ data-testid="searchbar-input"
140
151
  onEnterPressed={() => fireSearch(state.query, state.filters)}
141
152
  />
142
153
  {append}
@@ -148,7 +159,7 @@ export const SearchBar = ({
148
159
  {t(state.dirty ? "label.update" : "label.refresh")}
149
160
  </Button>
150
161
  </Field>
151
- {actions}
162
+ {children}
152
163
  </div>
153
164
  {!hideFilters && !collapsed && (
154
165
  <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,7 +153,7 @@ export type FilterObject = BaseFilter &
151
153
  (
152
154
  | {
153
155
  field: string;
154
- type?: FIELD_TYPE;
156
+ type: FIELD_TYPE;
155
157
  operator: OPERATOR;
156
158
  value?: FilterValue;
157
159
  query?: never;
@@ -165,8 +167,8 @@ export type FilterObject = BaseFilter &
165
167
  }
166
168
  );
167
169
 
168
- export interface SearchObject {
169
- query?: string | string[];
170
+ export interface SearchObject<T = string> {
171
+ query?: T;
170
172
  filters: FilterObject[];
171
173
  }
172
174
 
@@ -183,7 +185,7 @@ export interface FilterField {
183
185
  onSearch?: (q: string) => Promise<FieldValue[]> | FieldValue[];
184
186
  }
185
187
 
186
- interface BaseSearchBarProps {
188
+ interface BaseSearchBarProps extends Partial<ChildrenProp> {
187
189
  /**
188
190
  * Query string
189
191
  */
@@ -214,11 +216,6 @@ interface BaseSearchBarProps {
214
216
  */
215
217
  append?: ReactElement;
216
218
 
217
- /**
218
- * Additional actions menu
219
- */
220
- actions?: ReactElement;
221
-
222
219
  /**
223
220
  * Hide filter bar
224
221
  */
@@ -247,12 +244,6 @@ interface BaseSearchBarProps {
247
244
  * @param collapsed
248
245
  */
249
246
  onCollapsed?: (collapsed: boolean) => void;
250
-
251
- /**
252
- * On search event
253
- * @param queryObject
254
- */
255
- onSearch?: (queryObject: SearchObject) => void;
256
247
  /**
257
248
  * On query string change event
258
249
  * @param query
@@ -262,8 +253,26 @@ interface BaseSearchBarProps {
262
253
 
263
254
  export type SearchBarProps = BaseSearchBarProps &
264
255
  (
265
- | { multiple: true; query?: string[] }
266
- | { multiple?: never | false; query?: string }
256
+ | {
257
+ multiple: true;
258
+ query?: string[];
259
+
260
+ /**
261
+ * On search event
262
+ * @param queryObject
263
+ */
264
+ onSearch?: (queryObject: SearchObject<string[]>) => void;
265
+ }
266
+ | {
267
+ multiple?: never | false;
268
+ query?: string;
269
+
270
+ /**
271
+ * On search event
272
+ * @param queryObject
273
+ */
274
+ onSearch?: (queryObject: SearchObject) => void;
275
+ }
267
276
  );
268
277
 
269
278
  export interface FilterBarProps {