@redocly/theme 0.35.1 → 0.35.2

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.
@@ -2,3 +2,6 @@ import type { ResolvedNavItem } from '../../types/portal';
2
2
  import type { FilteredCatalog } from '../../types/portal/src/shared/types/catalog';
3
3
  import type { CatalogConfig } from '../../config';
4
4
  export declare function useCatalog(items: ResolvedNavItem[], config: CatalogConfig): FilteredCatalog;
5
+ export declare function fillSearchParams(filterProperty: string, filterValues: Set<string>, searchParams: URLSearchParams): URLSearchParams;
6
+ export declare function handleFilterInput(filterTerm: string): URLSearchParams;
7
+ export declare function getFilterValues(filterValues: any): Set<string>;
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.useCatalog = void 0;
26
+ exports.getFilterValues = exports.handleFilterInput = exports.fillSearchParams = exports.useCatalog = void 0;
27
27
  const React = __importStar(require("react"));
28
28
  const react_router_dom_1 = require("react-router-dom");
29
29
  const telemetry_1 = require("../../mocks/telemetry");
@@ -58,7 +58,10 @@ function useCatalog(items, config) {
58
58
  if (!(newFilterOptions instanceof Set))
59
59
  return prev;
60
60
  if (newFilterOptions.has(option)) {
61
- newFilterOptions.delete(option);
61
+ prev.forEach((element, _) => {
62
+ if (element instanceof Set && element.has(option))
63
+ element.delete(option);
64
+ });
62
65
  }
63
66
  else {
64
67
  newFilterOptions.add(option);
@@ -86,34 +89,25 @@ function useCatalog(items, config) {
86
89
  telemetry_1.telemetry.send('catalog_filter_changed', { type: 'select' });
87
90
  window.scrollTo(0, 0);
88
91
  }, [filtersWithOptions]);
89
- React.useEffect(() => {
92
+ const handleSearchParams = () => {
93
+ let newSearchParams = handleFilterInput(filterTerm);
90
94
  filtersState.forEach((filterValues, filterIdx) => {
91
95
  var _a;
92
96
  const filter = (_a = config.filters) === null || _a === void 0 ? void 0 : _a[filterIdx];
93
97
  if (!filter)
94
98
  return;
95
- searchParams.delete(filter.property);
96
- if (filterValues instanceof Set) {
97
- filterValues.forEach((value) => {
98
- searchParams.append(filter.property, value);
99
- });
100
- }
101
- else if (filterValues.from || filterValues.to) {
102
- searchParams.append(filter.property, `${filterValues.from || ''}--${filterValues.to || ''}`);
103
- return;
104
- }
99
+ const values = getFilterValues(filterValues);
100
+ newSearchParams = fillSearchParams(filter.property, values, newSearchParams);
105
101
  });
106
- if (filterTerm) {
107
- searchParams.set('filter', filterTerm);
108
- }
109
- else {
110
- searchParams.delete('filter');
111
- }
112
- const newSearch = searchParams.toString();
102
+ const newSearch = newSearchParams.toString();
113
103
  if (newSearch === location.search.substring(1))
114
104
  return;
115
105
  navigate({ search: newSearch });
116
- }, [config.filters, searchParams, filtersState, filterTerm, navigate, location]);
106
+ };
107
+ React.useEffect(() => {
108
+ handleSearchParams();
109
+ // eslint-disable-next-line react-hooks/exhaustive-deps
110
+ }, [config.filters, filtersState, filterTerm, navigate, location]);
117
111
  // filterParents[i] is a Set with indexes of parents of this filter
118
112
  const filterParents = React.useMemo(() => collectFilterParents(config.filters), [config.filters]);
119
113
  return React.useMemo(() => {
@@ -314,4 +308,30 @@ function mapFilterValues(value, mapping) {
314
308
  ? value.map((v) => mapping[String(v)] || v)
315
309
  : mapping[String(value)] || value;
316
310
  }
311
+ function fillSearchParams(filterProperty, filterValues, searchParams) {
312
+ filterValues.forEach((value) => {
313
+ if (!searchParams.has(filterProperty, value))
314
+ searchParams.append(filterProperty, value);
315
+ });
316
+ return searchParams;
317
+ }
318
+ exports.fillSearchParams = fillSearchParams;
319
+ function handleFilterInput(filterTerm) {
320
+ const searchParams = new URLSearchParams();
321
+ if (filterTerm)
322
+ searchParams.set('filter', filterTerm);
323
+ else
324
+ searchParams.delete('filter');
325
+ return searchParams;
326
+ }
327
+ exports.handleFilterInput = handleFilterInput;
328
+ function getFilterValues(filterValues) {
329
+ const values = new Set();
330
+ if (filterValues instanceof Set)
331
+ filterValues.forEach((value) => values.add(value));
332
+ else if (filterValues.from || filterValues.to)
333
+ values.add(`${filterValues.from || ''}--${filterValues.to || ''}`);
334
+ return values;
335
+ }
336
+ exports.getFilterValues = getFilterValues;
317
337
  //# sourceMappingURL=useCatalog.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redocly/theme",
3
- "version": "0.35.1",
3
+ "version": "0.35.2",
4
4
  "description": "Shared UI components lib",
5
5
  "keywords": [
6
6
  "theme",
@@ -47,7 +47,9 @@ export function useCatalog(items: ResolvedNavItem[], config: CatalogConfig): Fil
47
47
  const newFilterOptions = prev[filterIdx] ? prev[filterIdx] : new Set<string>();
48
48
  if (!(newFilterOptions instanceof Set)) return prev;
49
49
  if (newFilterOptions.has(option)) {
50
- newFilterOptions.delete(option);
50
+ prev.forEach((element, _) => {
51
+ if (element instanceof Set && element.has(option)) element.delete(option);
52
+ });
51
53
  } else {
52
54
  newFilterOptions.add(option);
53
55
  }
@@ -83,33 +85,25 @@ export function useCatalog(items: ResolvedNavItem[], config: CatalogConfig): Fil
83
85
  [filtersWithOptions],
84
86
  );
85
87
 
86
- React.useEffect(() => {
88
+ const handleSearchParams = () => {
89
+ let newSearchParams = handleFilterInput(filterTerm);
90
+
87
91
  filtersState.forEach((filterValues, filterIdx) => {
88
92
  const filter = config.filters?.[filterIdx];
89
93
  if (!filter) return;
90
- searchParams.delete(filter.property);
91
- if (filterValues instanceof Set) {
92
- filterValues.forEach((value) => {
93
- searchParams.append(filter.property, value);
94
- });
95
- } else if (filterValues.from || filterValues.to) {
96
- searchParams.append(
97
- filter.property,
98
- `${filterValues.from || ''}--${filterValues.to || ''}`,
99
- );
100
- return;
101
- }
94
+ const values = getFilterValues(filterValues);
95
+ newSearchParams = fillSearchParams(filter.property, values, newSearchParams);
102
96
  });
103
97
 
104
- if (filterTerm) {
105
- searchParams.set('filter', filterTerm);
106
- } else {
107
- searchParams.delete('filter');
108
- }
109
- const newSearch = searchParams.toString();
98
+ const newSearch = newSearchParams.toString();
110
99
  if (newSearch === location.search.substring(1)) return;
111
100
  navigate({ search: newSearch });
112
- }, [config.filters, searchParams, filtersState, filterTerm, navigate, location]);
101
+ };
102
+
103
+ React.useEffect(() => {
104
+ handleSearchParams();
105
+ // eslint-disable-next-line react-hooks/exhaustive-deps
106
+ }, [config.filters, filtersState, filterTerm, navigate, location]);
113
107
 
114
108
  // filterParents[i] is a Set with indexes of parents of this filter
115
109
  const filterParents = React.useMemo(() => collectFilterParents(config.filters), [config.filters]);
@@ -371,3 +365,34 @@ function mapFilterValues(value: unknown | unknown[], mapping?: Record<string, st
371
365
  ? value.map((v) => mapping[String(v)] || v)
372
366
  : mapping[String(value)] || value;
373
367
  }
368
+
369
+ export function fillSearchParams(
370
+ filterProperty: string,
371
+ filterValues: Set<string>,
372
+ searchParams: URLSearchParams,
373
+ ) {
374
+ filterValues.forEach((value) => {
375
+ if (!searchParams.has(filterProperty, value)) searchParams.append(filterProperty, value);
376
+ });
377
+
378
+ return searchParams;
379
+ }
380
+
381
+ export function handleFilterInput(filterTerm: string) {
382
+ const searchParams = new URLSearchParams();
383
+
384
+ if (filterTerm) searchParams.set('filter', filterTerm);
385
+ else searchParams.delete('filter');
386
+
387
+ return searchParams;
388
+ }
389
+
390
+ export function getFilterValues(filterValues: any) {
391
+ const values = new Set<string>();
392
+
393
+ if (filterValues instanceof Set) filterValues.forEach((value) => values.add(value));
394
+ else if (filterValues.from || filterValues.to)
395
+ values.add(`${filterValues.from || ''}--${filterValues.to || ''}`);
396
+
397
+ return values;
398
+ }