@react-fabric/searchbar 1.0.0-beta.9 → 1.0.1

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 (54) hide show
  1. package/dist/cjs/filterbar/Context.js +1 -1
  2. package/dist/cjs/filterbar/FilterBar.js +28 -23
  3. package/dist/cjs/filterbar/FilterEdit.js +7 -4
  4. package/dist/cjs/filterbar/FilterForm.js +177 -64
  5. package/dist/cjs/filterbar/FilterMenu.js +23 -12
  6. package/dist/cjs/filterbar/FilterTag.js +30 -20
  7. package/dist/cjs/filterbar/FilterView.js +11 -6
  8. package/dist/cjs/filterbar/QueryForm.js +47 -24
  9. package/dist/cjs/i18n/{ar.json.js → ar/searchbar.json.js} +43 -39
  10. package/dist/cjs/i18n/{en.json.js → en/searchbar.json.js} +43 -39
  11. package/dist/cjs/i18n/index.js +4 -4
  12. package/dist/cjs/index.js +28 -0
  13. package/dist/cjs/searchbar/SearchBar.js +58 -16
  14. package/dist/cjs/types/index.js +13 -4
  15. package/dist/esm/filterbar/Context.js +2 -2
  16. package/dist/esm/filterbar/FilterBar.js +30 -25
  17. package/dist/esm/filterbar/FilterEdit.js +7 -4
  18. package/dist/esm/filterbar/FilterForm.js +181 -68
  19. package/dist/esm/filterbar/FilterMenu.js +24 -13
  20. package/dist/esm/filterbar/FilterTag.js +32 -22
  21. package/dist/esm/filterbar/FilterView.js +11 -6
  22. package/dist/esm/filterbar/QueryForm.js +48 -25
  23. package/dist/esm/i18n/{ar.json.js → ar/searchbar.json.js} +43 -39
  24. package/dist/esm/i18n/{en.json.js → en/searchbar.json.js} +43 -39
  25. package/dist/esm/i18n/index.js +2 -2
  26. package/dist/esm/index.js +30 -0
  27. package/dist/esm/searchbar/SearchBar.js +59 -17
  28. package/dist/esm/types/index.js +13 -4
  29. package/dist/types/filterbar/Context.d.ts +2 -3
  30. package/dist/types/filterbar/FilterBar.d.ts +23 -1
  31. package/dist/types/filterbar/FilterEdit.d.ts +2 -2
  32. package/dist/types/filterbar/FilterForm.d.ts +3 -3
  33. package/dist/types/filterbar/FilterMenu.d.ts +3 -2
  34. package/dist/types/filterbar/FilterTag.d.ts +3 -2
  35. package/dist/types/filterbar/FilterView.d.ts +2 -2
  36. package/dist/types/filterbar/QueryForm.d.ts +2 -2
  37. package/dist/types/index.d.ts +27 -1
  38. package/dist/types/searchbar/SearchBar.d.ts +30 -1
  39. package/dist/types/types/index.d.ts +23 -14
  40. package/package.json +16 -19
  41. package/src/filterbar/Context.tsx +5 -5
  42. package/src/filterbar/FilterBar.tsx +58 -75
  43. package/src/filterbar/FilterEdit.tsx +9 -16
  44. package/src/filterbar/FilterForm.tsx +149 -91
  45. package/src/filterbar/FilterMenu.tsx +29 -16
  46. package/src/filterbar/FilterTag.tsx +30 -43
  47. package/src/filterbar/FilterView.tsx +13 -24
  48. package/src/filterbar/QueryForm.tsx +26 -43
  49. package/src/i18n/{ar.json → ar/searchbar.json} +43 -39
  50. package/src/i18n/{en.json → en/searchbar.json} +43 -39
  51. package/src/i18n/index.ts +2 -2
  52. package/src/index.ts +23 -18
  53. package/src/searchbar/SearchBar.tsx +67 -40
  54. package/src/types/index.ts +38 -70
@@ -21,31 +21,56 @@
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 { Button, useDebounce, useLocalStorage } from "@react-fabric/core";
24
+ import { Badge, 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
27
  import { useEffect, useMemo, useReducer, useState } from "react";
28
28
  import { useTranslation } from "react-i18next";
29
29
  import { FilterBar } from "../filterbar/FilterBar";
30
- import {
31
- type FilterBarProps,
32
- type FilterObject,
33
- type SearchBarProps,
34
- } from "../types";
30
+ import { type FilterBarProps, type FilterObject, type SearchBarProps } from "../types";
35
31
 
36
32
  interface SearchState {
37
33
  dirty: boolean;
38
- query: string | string[];
34
+ query?: string | string[];
39
35
  filters: FilterObject[];
40
36
  }
41
37
 
42
38
  type SearchActions =
43
- | { type: "reset"; query: string | string[]; filters: FilterObject[] }
39
+ | { type: "reset"; query?: string | string[]; filters: FilterObject[] }
44
40
  | { type: "dirty"; dirty: boolean }
45
- | { type: "query"; query: string | string[] }
41
+ | { type: "query"; query?: string | string[] }
46
42
  | { type: "filter"; filters: FilterObject[] };
47
43
 
48
- export const SearchBar = ({
44
+ /**
45
+ * This component provides a search input with autocomplete functionality,
46
+ * a button to trigger search, and an optional filter bar.
47
+ * It supports history of search queries, multiple selection, and custom decorations.
48
+ * It can be collapsed to hide filters and has options for customization.
49
+ *
50
+ * @example
51
+ * ```jsx
52
+ * <SearchBar
53
+ * historyKey="myApp:searchHistory"
54
+ * historyCount={10}
55
+ * defaultQueryList={["example", "test"]}
56
+ * prepend={<span>Prepend</span>}
57
+ * append={<span>Append</span>}
58
+ * decorateEnd={<span>End Decoration</span>}
59
+ * decorateStart={<span>Start Decoration</span>}
60
+ * hideFilters={false}
61
+ * defaultCollapsed={false}
62
+ * disabled={false}
63
+ * onCollapsed={(collapsed) => console.log("Collapsed:", collapsed)}
64
+ * onQuery={(query) => console.log("Query:", query)}
65
+ * onSearch={({ query, filters }) => console.log("Search:", query, filters)}
66
+ * query={""}
67
+ * ref={searchInputRef}
68
+ * multiple={false}
69
+ * filters={[{ name: "filter1", value: "value1" }]}
70
+ * />
71
+ * ```
72
+ */
73
+ export function SearchBar({
49
74
  historyKey = "ruf:search",
50
75
  historyCount = 20,
51
76
  defaultQueryList,
@@ -57,44 +82,47 @@ export const SearchBar = ({
57
82
  hideFilters,
58
83
  defaultCollapsed = false,
59
84
  disabled,
60
- onCollapsed,
61
- onQuery,
62
85
  onSearch,
63
86
  query,
87
+ ref,
64
88
  multiple,
65
89
  filters = EMPTY_ARRAY,
66
90
  ...filterProps
67
- }: SearchBarProps & Omit<FilterBarProps, "onFilterChanged">) => {
91
+ }: SearchBarProps & Omit<FilterBarProps, "onFilterChanged">) {
68
92
  const { t } = useTranslation("searchbar");
69
- const [history, setHistory] = useLocalStorage<string[]>(
70
- historyKey,
71
- defaultQueryList ?? [],
72
- );
93
+ const [history, setHistory] = useLocalStorage<string[]>(historyKey, defaultQueryList ?? []);
73
94
 
74
- const [collapsed, setCollapsed] = useState(defaultCollapsed);
95
+ const [collapsed, setCollapsed] = useState(() => defaultCollapsed);
75
96
  const emptyValue = useMemo(() => (multiple ? EMPTY_ARRAY : ""), [multiple]);
76
97
 
77
- const fireSearch = useDebounce(
78
- (query: AnyObject, filters: FilterObject[]) => {
79
- onSearch?.({
80
- query,
81
- filters,
82
- });
83
- },
84
- [],
85
- );
98
+ const fireSearch = useDebounce((query: AnyObject, filters: FilterObject[]) => {
99
+ dispatch({ type: "dirty", dirty: false });
100
+ onSearch?.({
101
+ query: query as never,
102
+ filters,
103
+ });
104
+ }, []);
86
105
 
87
106
  const [state, dispatch] = useReducer(
88
107
  (state: SearchState, action: SearchActions) => {
89
108
  if (action.type === "reset") {
90
- return { ...state, query: action.query, filters: action.filters };
109
+ return {
110
+ ...state,
111
+ dirty: false,
112
+ query: action.query,
113
+ filters: action.filters,
114
+ };
91
115
  }
92
116
  if (action.type === "dirty") {
93
117
  return { ...state, dirty: action.dirty };
94
118
  }
95
119
  if (action.type === "query") {
96
- !multiple && fireSearch(action.query ?? emptyValue, state.filters);
97
- return { ...state, dirty: false, query: action.query ?? emptyValue };
120
+ if (!multiple) fireSearch(action.query ?? emptyValue, state.filters);
121
+ return {
122
+ ...state,
123
+ dirty: !!multiple,
124
+ query: action.query ?? emptyValue,
125
+ };
98
126
  }
99
127
  if (action.type === "filter") {
100
128
  fireSearch(state.query, action.filters ?? []);
@@ -129,8 +157,9 @@ export const SearchBar = ({
129
157
  <Field data-inner={true} className="flex-1">
130
158
  {!hideFilters && (
131
159
  <Button
132
- variant="link"
133
- badge={filters.length}
160
+ color="primary"
161
+ variant={collapsed ? "soft" : "link"}
162
+ altIcon={<Badge inline value={filters.length} />}
134
163
  onClick={() => setCollapsed(!collapsed)}
135
164
  >
136
165
  {t("label.filters")}
@@ -140,14 +169,16 @@ export const SearchBar = ({
140
169
  <AutoComplete
141
170
  allowClear
142
171
  expandOnEdit
172
+ ref={ref}
173
+ disabled={disabled}
143
174
  multiple={multiple}
144
175
  history={history}
145
176
  value={state.query}
146
177
  decorateEnd={decorateEnd}
147
178
  decorateStart={decorateStart}
148
- onInput={() => dispatch({ type: "dirty", dirty: true })}
179
+ onInput={() => dispatch({ type: "dirty", dirty: !multiple })}
149
180
  onSelect={updateHistory}
150
- data-testid="searchbar-input"
181
+ data-ref="searchbarInput"
151
182
  onEnterPressed={() => fireSearch(state.query, state.filters)}
152
183
  />
153
184
  {append}
@@ -162,12 +193,8 @@ export const SearchBar = ({
162
193
  {children}
163
194
  </div>
164
195
  {!hideFilters && !collapsed && (
165
- <FilterBar
166
- {...filterProps}
167
- filters={state.filters}
168
- onFilterChanged={handleFilterChange}
169
- />
196
+ <FilterBar {...filterProps} disabled={disabled} filters={state.filters} onFilterChanged={handleFilterChange} />
170
197
  )}
171
198
  </div>
172
199
  );
173
- };
200
+ }
@@ -21,17 +21,18 @@
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
+ import { type ChildrenProp, type RefProp } from "@react-fabric/core/dist/types/types";
25
25
  import { type ReactElement } from "react";
26
26
 
27
27
  export enum FIELD_TYPE {
28
28
  ID = "ID",
29
29
  STRING = "STRING",
30
+ TEXT = "TEXT",
30
31
  NUMBER = "NUMBER",
31
32
  DECIMAL = "DECIMAL",
32
33
  BOOLEAN = "BOOLEAN",
33
- // DATE = "DATE",
34
- // DATETIME = "DATETIME",
34
+ DATE = "DATE",
35
+ DATETIME = "DATETIME",
35
36
  // GEO = "GEO",
36
37
  NONE = "NONE",
37
38
  }
@@ -39,12 +40,14 @@ export enum FIELD_TYPE {
39
40
  export enum OPERATOR {
40
41
  EXISTS = "EXISTS",
41
42
  IS = "IS",
42
- IN = "IN",
43
+ ALL = "ALL",
44
+ ANY = "ANY",
45
+ EQ = "EQ",
43
46
  LT = "LT",
44
47
  GT = "GT",
45
48
  LTE = "LTE",
46
49
  GTE = "GTE",
47
- INCLUDES = "INCLUDES",
50
+ LIKE = "LIKE",
48
51
  STARTS = "STARTS",
49
52
  ENDS = "ENDS",
50
53
  WITHIN = "WITHIN",
@@ -55,66 +58,37 @@ export enum OPERATOR {
55
58
  * @internal
56
59
  */
57
60
  export const TypeOperators: { [key in FIELD_TYPE]: OPERATOR[] } = {
58
- [FIELD_TYPE.ID]: [OPERATOR.IS, OPERATOR.IN],
59
- [FIELD_TYPE.STRING]: [
60
- OPERATOR.EXISTS,
61
- OPERATOR.IS,
62
- OPERATOR.IN,
63
- OPERATOR.INCLUDES,
64
- OPERATOR.STARTS,
65
- OPERATOR.ENDS,
66
- ],
67
- [FIELD_TYPE.NUMBER]: [
68
- OPERATOR.EXISTS,
69
- OPERATOR.IS,
70
- OPERATOR.BETWEEN,
71
- OPERATOR.LT,
72
- OPERATOR.GT,
73
- OPERATOR.LTE,
74
- OPERATOR.GTE,
75
- ],
76
- [FIELD_TYPE.DECIMAL]: [
77
- OPERATOR.EXISTS,
78
- OPERATOR.IS,
79
- OPERATOR.BETWEEN,
80
- OPERATOR.LT,
81
- OPERATOR.GT,
82
- OPERATOR.LTE,
83
- OPERATOR.GTE,
84
- ],
85
- [FIELD_TYPE.BOOLEAN]: [OPERATOR.EXISTS, OPERATOR.IS],
86
- // [FIELD_TYPE.DATE]: [
87
- // OPERATOR.BETWEEN,
88
- // OPERATOR.LT,
89
- // OPERATOR.GT,
90
- // OPERATOR.LTE,
91
- // OPERATOR.GTE,
92
- // ],
93
- // [FIELD_TYPE.DATETIME]: [
94
- // OPERATOR.BETWEEN,
95
- // OPERATOR.LT,
96
- // OPERATOR.GT,
97
- // OPERATOR.LTE,
98
- // OPERATOR.GTE,
99
- // ],
61
+ [FIELD_TYPE.ID]: [OPERATOR.IS, OPERATOR.ANY],
62
+ [FIELD_TYPE.STRING]: [OPERATOR.EXISTS, OPERATOR.IS, OPERATOR.ANY, OPERATOR.ALL],
63
+ [FIELD_TYPE.TEXT]: [OPERATOR.EXISTS, OPERATOR.LIKE, OPERATOR.STARTS, OPERATOR.ENDS],
64
+ [FIELD_TYPE.NUMBER]: [OPERATOR.EXISTS, OPERATOR.EQ, OPERATOR.BETWEEN, OPERATOR.LT, OPERATOR.GT, OPERATOR.LTE, OPERATOR.GTE],
65
+ [FIELD_TYPE.DECIMAL]: [OPERATOR.EXISTS, OPERATOR.EQ, OPERATOR.BETWEEN, OPERATOR.LT, OPERATOR.GT, OPERATOR.LTE, OPERATOR.GTE],
66
+ [FIELD_TYPE.BOOLEAN]: [OPERATOR.EXISTS, OPERATOR.EQ],
67
+ [FIELD_TYPE.DATE]: [OPERATOR.EXISTS, OPERATOR.EQ, OPERATOR.BETWEEN, OPERATOR.LT, OPERATOR.GT, OPERATOR.LTE, OPERATOR.GTE],
68
+ [FIELD_TYPE.DATETIME]: [OPERATOR.EXISTS, OPERATOR.EQ, OPERATOR.BETWEEN, OPERATOR.LT, OPERATOR.GT, OPERATOR.LTE, OPERATOR.GTE],
100
69
  // [FIELD_TYPE.GEO]: [],
101
70
  [FIELD_TYPE.NONE]: [OPERATOR.EXISTS],
102
71
  };
103
72
 
104
73
  /**
105
74
  * @internal
75
+ * 0 single value
76
+ * 1 many values
77
+ * 2 two values
106
78
  */
107
79
  export const OperatorValueType: {
108
80
  [key in OPERATOR]: 0 | 1 | 2;
109
81
  } = {
110
82
  [OPERATOR.EXISTS]: 0,
111
83
  [OPERATOR.IS]: 0,
112
- [OPERATOR.IN]: 1,
84
+ [OPERATOR.ANY]: 1,
85
+ [OPERATOR.ALL]: 1,
113
86
  [OPERATOR.BETWEEN]: 2,
114
87
  [OPERATOR.WITHIN]: 0,
115
88
  [OPERATOR.STARTS]: 0,
116
89
  [OPERATOR.ENDS]: 0,
117
- [OPERATOR.INCLUDES]: 0,
90
+ [OPERATOR.LIKE]: 0,
91
+ [OPERATOR.EQ]: 0,
118
92
  [OPERATOR.LT]: 0,
119
93
  [OPERATOR.GT]: 0,
120
94
  [OPERATOR.LTE]: 0,
@@ -124,13 +98,7 @@ export const OperatorValueType: {
124
98
  /**
125
99
  * @internal
126
100
  */
127
- export type FilterValue =
128
- | undefined
129
- | boolean
130
- | string
131
- | string[]
132
- | number
133
- | [number, number];
101
+ export type FilterValue = undefined | boolean | string | string[] | number | [number, number];
134
102
 
135
103
  interface BaseFilter {
136
104
  id: string;
@@ -140,12 +108,10 @@ interface BaseFilter {
140
108
  color?: string;
141
109
  value?: FilterValue;
142
110
  pinned?: boolean;
143
- negate?: boolean;
144
111
  disabled?: boolean;
145
112
  required?: boolean;
146
113
  canPin?: boolean;
147
114
  canEdit?: boolean;
148
- canInvert?: boolean;
149
115
  canDisable?: boolean;
150
116
  }
151
117
 
@@ -153,16 +119,18 @@ export type FilterObject = BaseFilter &
153
119
  (
154
120
  | {
155
121
  field: string;
156
- type: FIELD_TYPE;
157
122
  operator: OPERATOR;
158
123
  value?: FilterValue;
124
+ negate?: boolean;
125
+ canInvert?: boolean;
159
126
  query?: never;
127
+ type?: string;
160
128
  }
161
129
  | {
162
130
  field?: never;
163
- type?: never;
164
131
  operator?: never;
165
132
  label: string;
133
+ canInvert?: false;
166
134
  query: string;
167
135
  }
168
136
  );
@@ -172,9 +140,7 @@ export interface SearchObject<T = string> {
172
140
  filters: FilterObject[];
173
141
  }
174
142
 
175
- export type FieldValue =
176
- | string
177
- | { value: string; label: string; icon?: AnyObject };
143
+ export type FieldValue = string | { value: string; label: string; icon?: AnyObject };
178
144
 
179
145
  export interface FilterField {
180
146
  field: string;
@@ -185,11 +151,11 @@ export interface FilterField {
185
151
  onSearch?: (q: string) => Promise<FieldValue[]> | FieldValue[];
186
152
  }
187
153
 
188
- interface BaseSearchBarProps extends Partial<ChildrenProp> {
154
+ interface BaseSearchBarProps extends Partial<ChildrenProp>, RefProp<HTMLTextAreaElement> {
189
155
  /**
190
156
  * Query string
191
157
  */
192
- query?: AnyObject;
158
+ query?: string | string[];
193
159
  /**
194
160
  * Search history count
195
161
  * @default 20
@@ -234,11 +200,11 @@ interface BaseSearchBarProps extends Partial<ChildrenProp> {
234
200
  /**
235
201
  *
236
202
  */
237
- decorateStart?: JSX.Element | string | number | boolean;
203
+ decorateStart?: React.ReactElement | string | number | boolean;
238
204
  /**
239
205
  *
240
206
  */
241
- decorateEnd?: JSX.Element | string | number | boolean;
207
+ decorateEnd?: React.ReactElement | string | number | boolean;
242
208
  /**
243
209
  * On filter collapsed
244
210
  * @param collapsed
@@ -264,7 +230,7 @@ export type SearchBarProps = BaseSearchBarProps &
264
230
  onSearch?: (queryObject: SearchObject<string[]>) => void;
265
231
  }
266
232
  | {
267
- multiple?: never | false;
233
+ multiple?: false;
268
234
  query?: string;
269
235
 
270
236
  /**
@@ -291,12 +257,12 @@ export interface FilterBarProps {
291
257
  /**
292
258
  * Editable filters
293
259
  */
294
- isEditable?: boolean;
260
+ editable?: boolean;
295
261
 
296
262
  /**
297
263
  * Disable component
298
264
  */
299
- isDisabled?: boolean;
265
+ disabled?: boolean;
300
266
 
301
267
  /**
302
268
  * Message for empty field list
@@ -313,6 +279,8 @@ export interface FilterBarProps {
313
279
  excludedColor?: string;
314
280
 
315
281
  allowAdd?: boolean;
282
+
283
+ queryLanguage?: "text" | "json";
316
284
  querySchema?: Array<{ uri: string; schema: KeyValue }>;
317
285
  defaultQuery?: string;
318
286