@snack-uikit/chips 0.23.4-preview-91ef6a1d.0 → 0.23.4-preview-e49c63bb.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.
@@ -89,8 +89,12 @@ function ChipChoiceMultiple(_a) {
89
89
  total: Object.keys(flattenOptions).length,
90
90
  allLabel: t('allLabel')
91
91
  });
92
- const fuzzySearch = (0, hooks_1.useFuzzySearch)(options, flatMapOptions);
93
- const result = (0, react_1.useMemo)(() => (!searchable || valueToRender === searchValue) && !disableFuzzySearch ? options : fuzzySearch(searchValue), [fuzzySearch, options, searchValue, searchable, valueToRender, disableFuzzySearch]);
92
+ const fuzzySearch = (0, hooks_1.useFuzzySearch)({
93
+ options,
94
+ flatMapOptions,
95
+ disableFuzzySearch
96
+ });
97
+ const result = (0, react_1.useMemo)(() => !searchable || valueToRender === searchValue ? options : fuzzySearch(searchValue), [fuzzySearch, options, searchValue, searchable, valueToRender]);
94
98
  const items = (0, react_1.useMemo)(() => (0, options_1.transformOptionsToItems)(result, contentRender), [contentRender, result]);
95
99
  const clearValue = () => {
96
100
  setValue([]);
@@ -80,8 +80,12 @@ function ChipChoiceSingle(_a) {
80
80
  label: selectedOption === null || selectedOption === void 0 ? void 0 : selectedOption.label,
81
81
  allLabel: t('allLabel')
82
82
  });
83
- const fuzzySearch = (0, hooks_1.useFuzzySearch)(options, flatMapOptions);
84
- const result = (0, react_1.useMemo)(() => (!searchable || valueToRender === searchValue) && !disableFuzzySearch ? options : fuzzySearch(searchValue), [disableFuzzySearch, fuzzySearch, options, searchValue, searchable, valueToRender]);
83
+ const fuzzySearch = (0, hooks_1.useFuzzySearch)({
84
+ options,
85
+ flatMapOptions,
86
+ disableFuzzySearch
87
+ });
88
+ const result = (0, react_1.useMemo)(() => !searchable || valueToRender === searchValue ? options : fuzzySearch(searchValue), [fuzzySearch, options, searchValue, searchable, valueToRender]);
85
89
  const items = (0, react_1.useMemo)(() => (0, options_1.transformOptionsToItems)(result, contentRender), [contentRender, result]);
86
90
  const clearValue = () => {
87
91
  setValue(undefined);
@@ -9,7 +9,12 @@ export declare function useHandleOnKeyDown({ setOpen }: UseHandleOnKeyDownProps)
9
9
  /**
10
10
  * Нечеткий поиск среди айтемов по полям 'content.option', 'content.caption', 'content.description', 'label'
11
11
  */
12
- export declare function useFuzzySearch<T extends ContentRenderProps = ContentRenderProps>(options: FilterOption<T>[], flatMapOptions: (BaseOption<T> | AccordionOption<T> | NestListOption<T>)[], minSearchInputLength?: number): (search: string) => FilterOption<T>[];
12
+ export declare function useFuzzySearch<T extends ContentRenderProps = ContentRenderProps>({ options, flatMapOptions, minSearchInputLength, disableFuzzySearch, }: {
13
+ options: FilterOption<T>[];
14
+ flatMapOptions: (BaseOption<T> | AccordionOption<T> | NestListOption<T>)[];
15
+ minSearchInputLength?: number;
16
+ disableFuzzySearch?: boolean;
17
+ }): (search: string) => FilterOption<T>[];
13
18
  type UseAutoApplyProps = {
14
19
  autoApply: boolean;
15
20
  size: Size;
@@ -12,6 +12,7 @@ exports.useHandleOnKeyDown = useHandleOnKeyDown;
12
12
  exports.useFuzzySearch = useFuzzySearch;
13
13
  exports.useAutoApply = useAutoApply;
14
14
  const jsx_runtime_1 = require("react/jsx-runtime");
15
+ /* eslint-disable no-console */
15
16
  const fuzzy_search_1 = __importDefault(require("fuzzy-search"));
16
17
  const react_1 = require("react");
17
18
  const button_1 = require("@snack-uikit/button");
@@ -46,19 +47,38 @@ const DEFAULT_MIN_SEARCH_INPUT_LENGTH = 2;
46
47
  /**
47
48
  * Нечеткий поиск среди айтемов по полям 'content.option', 'content.caption', 'content.description', 'label'
48
49
  */
49
- function useFuzzySearch(options, flatMapOptions, minSearchInputLength) {
50
+ function useFuzzySearch(_ref2) {
51
+ let {
52
+ options,
53
+ flatMapOptions,
54
+ minSearchInputLength,
55
+ disableFuzzySearch
56
+ } = _ref2;
50
57
  return (0, react_1.useCallback)(search => {
51
58
  const searcher = new fuzzy_search_1.default(flatMapOptions, ['label', 'contentRenderProps.description', 'contentRenderProps.caption'], {});
52
- return search.length > (minSearchInputLength !== null && minSearchInputLength !== void 0 ? minSearchInputLength : DEFAULT_MIN_SEARCH_INPUT_LENGTH) ? searcher.search(search) : options;
53
- }, [flatMapOptions, minSearchInputLength, options]);
59
+ console.log('disableFuzzySearch', disableFuzzySearch);
60
+ const plainSearcher = searchQuery => options.filter(option => {
61
+ var _a, _b, _c, _d, _e;
62
+ let matchesQuery = false;
63
+ matchesQuery || (matchesQuery = Boolean((_a = option.label) === null || _a === void 0 ? void 0 : _a.toString().includes(String(searchQuery))));
64
+ if ('contentRenderProps' in option) {
65
+ matchesQuery || (matchesQuery = Boolean((_c = (_b = option === null || option === void 0 ? void 0 : option.contentRenderProps) === null || _b === void 0 ? void 0 : _b.description) === null || _c === void 0 ? void 0 : _c.toString().includes(String(searchQuery))));
66
+ matchesQuery || (matchesQuery = Boolean((_e = (_d = option === null || option === void 0 ? void 0 : option.contentRenderProps) === null || _d === void 0 ? void 0 : _d.caption) === null || _e === void 0 ? void 0 : _e.toString().includes(String(searchQuery))));
67
+ }
68
+ console.log('matchesQuery', matchesQuery);
69
+ return matchesQuery;
70
+ });
71
+ const searcherFunction = disableFuzzySearch ? plainSearcher : searcher.search;
72
+ return search.length > (minSearchInputLength !== null && minSearchInputLength !== void 0 ? minSearchInputLength : DEFAULT_MIN_SEARCH_INPUT_LENGTH) ? searcherFunction(search) : options;
73
+ }, [disableFuzzySearch, flatMapOptions, minSearchInputLength, options]);
54
74
  }
55
- function useAutoApply(_ref2) {
75
+ function useAutoApply(_ref3) {
56
76
  let {
57
77
  autoApply,
58
78
  size,
59
79
  onApprove,
60
80
  onCancel
61
- } = _ref2;
81
+ } = _ref3;
62
82
  const {
63
83
  t
64
84
  } = (0, locale_1.useLocale)('Chips');
@@ -58,8 +58,8 @@ export function ChipChoiceMultiple(_a) {
58
58
  total: Object.keys(flattenOptions).length,
59
59
  allLabel: t('allLabel'),
60
60
  });
61
- const fuzzySearch = useFuzzySearch(options, flatMapOptions);
62
- const result = useMemo(() => ((!searchable || valueToRender === searchValue) && !disableFuzzySearch ? options : fuzzySearch(searchValue)), [fuzzySearch, options, searchValue, searchable, valueToRender, disableFuzzySearch]);
61
+ const fuzzySearch = useFuzzySearch({ options, flatMapOptions, disableFuzzySearch });
62
+ const result = useMemo(() => (!searchable || valueToRender === searchValue ? options : fuzzySearch(searchValue)), [fuzzySearch, options, searchValue, searchable, valueToRender]);
63
63
  const items = useMemo(() => transformOptionsToItems(result, contentRender), [contentRender, result]);
64
64
  const clearValue = () => {
65
65
  setValue([]);
@@ -47,8 +47,8 @@ export function ChipChoiceSingle(_a) {
47
47
  const valueToRender = valueRender
48
48
  ? valueRender(selectedOption)
49
49
  : defaultSingleValueFormatter({ label: selectedOption === null || selectedOption === void 0 ? void 0 : selectedOption.label, allLabel: t('allLabel') });
50
- const fuzzySearch = useFuzzySearch(options, flatMapOptions);
51
- const result = useMemo(() => ((!searchable || valueToRender === searchValue) && !disableFuzzySearch ? options : fuzzySearch(searchValue)), [disableFuzzySearch, fuzzySearch, options, searchValue, searchable, valueToRender]);
50
+ const fuzzySearch = useFuzzySearch({ options, flatMapOptions, disableFuzzySearch });
51
+ const result = useMemo(() => (!searchable || valueToRender === searchValue ? options : fuzzySearch(searchValue)), [fuzzySearch, options, searchValue, searchable, valueToRender]);
52
52
  const items = useMemo(() => transformOptionsToItems(result, contentRender), [contentRender, result]);
53
53
  const clearValue = () => {
54
54
  setValue(undefined);
@@ -9,7 +9,12 @@ export declare function useHandleOnKeyDown({ setOpen }: UseHandleOnKeyDownProps)
9
9
  /**
10
10
  * Нечеткий поиск среди айтемов по полям 'content.option', 'content.caption', 'content.description', 'label'
11
11
  */
12
- export declare function useFuzzySearch<T extends ContentRenderProps = ContentRenderProps>(options: FilterOption<T>[], flatMapOptions: (BaseOption<T> | AccordionOption<T> | NestListOption<T>)[], minSearchInputLength?: number): (search: string) => FilterOption<T>[];
12
+ export declare function useFuzzySearch<T extends ContentRenderProps = ContentRenderProps>({ options, flatMapOptions, minSearchInputLength, disableFuzzySearch, }: {
13
+ options: FilterOption<T>[];
14
+ flatMapOptions: (BaseOption<T> | AccordionOption<T> | NestListOption<T>)[];
15
+ minSearchInputLength?: number;
16
+ disableFuzzySearch?: boolean;
17
+ }): (search: string) => FilterOption<T>[];
13
18
  type UseAutoApplyProps = {
14
19
  autoApply: boolean;
15
20
  size: Size;
@@ -1,4 +1,5 @@
1
1
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /* eslint-disable no-console */
2
3
  import FuzzySearch from 'fuzzy-search';
3
4
  import { useCallback } from 'react';
4
5
  import { ButtonFilled, ButtonFunction } from '@snack-uikit/button';
@@ -31,13 +32,26 @@ const DEFAULT_MIN_SEARCH_INPUT_LENGTH = 2;
31
32
  /**
32
33
  * Нечеткий поиск среди айтемов по полям 'content.option', 'content.caption', 'content.description', 'label'
33
34
  */
34
- export function useFuzzySearch(options, flatMapOptions, minSearchInputLength) {
35
+ export function useFuzzySearch({ options, flatMapOptions, minSearchInputLength, disableFuzzySearch, }) {
35
36
  return useCallback((search) => {
36
37
  const searcher = new FuzzySearch(flatMapOptions, ['label', 'contentRenderProps.description', 'contentRenderProps.caption'], {});
38
+ console.log('disableFuzzySearch', disableFuzzySearch);
39
+ const plainSearcher = (searchQuery) => options.filter(option => {
40
+ var _a, _b, _c, _d, _e;
41
+ let matchesQuery = false;
42
+ matchesQuery || (matchesQuery = Boolean((_a = option.label) === null || _a === void 0 ? void 0 : _a.toString().includes(String(searchQuery))));
43
+ if ('contentRenderProps' in option) {
44
+ matchesQuery || (matchesQuery = Boolean((_c = (_b = option === null || option === void 0 ? void 0 : option.contentRenderProps) === null || _b === void 0 ? void 0 : _b.description) === null || _c === void 0 ? void 0 : _c.toString().includes(String(searchQuery))));
45
+ matchesQuery || (matchesQuery = Boolean((_e = (_d = option === null || option === void 0 ? void 0 : option.contentRenderProps) === null || _d === void 0 ? void 0 : _d.caption) === null || _e === void 0 ? void 0 : _e.toString().includes(String(searchQuery))));
46
+ }
47
+ console.log('matchesQuery', matchesQuery);
48
+ return matchesQuery;
49
+ });
50
+ const searcherFunction = disableFuzzySearch ? plainSearcher : searcher.search;
37
51
  return search.length > (minSearchInputLength !== null && minSearchInputLength !== void 0 ? minSearchInputLength : DEFAULT_MIN_SEARCH_INPUT_LENGTH)
38
- ? searcher.search(search)
52
+ ? searcherFunction(search)
39
53
  : options;
40
- }, [flatMapOptions, minSearchInputLength, options]);
54
+ }, [disableFuzzySearch, flatMapOptions, minSearchInputLength, options]);
41
55
  }
42
56
  export function useAutoApply({ autoApply, size, onApprove, onCancel, }) {
43
57
  const { t } = useLocale('Chips');
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "title": "Chips",
7
- "version": "0.23.4-preview-91ef6a1d.0",
7
+ "version": "0.23.4-preview-e49c63bb.0",
8
8
  "sideEffects": [
9
9
  "*.css",
10
10
  "*.woff",
@@ -37,10 +37,10 @@
37
37
  "scripts": {},
38
38
  "dependencies": {
39
39
  "@snack-uikit/button": "0.19.5",
40
- "@snack-uikit/calendar": "0.11.15-preview-91ef6a1d.0",
40
+ "@snack-uikit/calendar": "0.11.15-preview-e49c63bb.0",
41
41
  "@snack-uikit/dropdown": "0.4.2",
42
42
  "@snack-uikit/icons": "0.24.1",
43
- "@snack-uikit/list": "0.22.3-preview-91ef6a1d.0",
43
+ "@snack-uikit/list": "0.22.3-preview-e49c63bb.0",
44
44
  "@snack-uikit/loaders": "0.9.0",
45
45
  "@snack-uikit/utils": "3.6.0",
46
46
  "classnames": "2.5.1",
@@ -54,5 +54,5 @@
54
54
  "peerDependencies": {
55
55
  "@snack-uikit/locale": "*"
56
56
  },
57
- "gitHead": "a9285b91576f7c1b51d9e9150ecf95b1e403fc47"
57
+ "gitHead": "416ce7ad5f712c6934aba2374bce1d2c68618499"
58
58
  }
@@ -90,11 +90,11 @@ export function ChipChoiceMultiple<T extends ContentRenderProps = ContentRenderP
90
90
  allLabel: t('allLabel'),
91
91
  });
92
92
 
93
- const fuzzySearch = useFuzzySearch(options, flatMapOptions);
93
+ const fuzzySearch = useFuzzySearch({ options, flatMapOptions, disableFuzzySearch });
94
94
 
95
95
  const result = useMemo(
96
- () => ((!searchable || valueToRender === searchValue) && !disableFuzzySearch ? options : fuzzySearch(searchValue)),
97
- [fuzzySearch, options, searchValue, searchable, valueToRender, disableFuzzySearch],
96
+ () => (!searchable || valueToRender === searchValue ? options : fuzzySearch(searchValue)),
97
+ [fuzzySearch, options, searchValue, searchable, valueToRender],
98
98
  );
99
99
  const items = useMemo(() => transformOptionsToItems<T>(result, contentRender), [contentRender, result]);
100
100
 
@@ -74,11 +74,11 @@ export function ChipChoiceSingle<T extends ContentRenderProps = ContentRenderPro
74
74
  ? valueRender(selectedOption)
75
75
  : defaultSingleValueFormatter({ label: selectedOption?.label, allLabel: t('allLabel') });
76
76
 
77
- const fuzzySearch = useFuzzySearch(options, flatMapOptions);
77
+ const fuzzySearch = useFuzzySearch({ options, flatMapOptions, disableFuzzySearch });
78
78
 
79
79
  const result = useMemo(
80
- () => ((!searchable || valueToRender === searchValue) && !disableFuzzySearch ? options : fuzzySearch(searchValue)),
81
- [disableFuzzySearch, fuzzySearch, options, searchValue, searchable, valueToRender],
80
+ () => (!searchable || valueToRender === searchValue ? options : fuzzySearch(searchValue)),
81
+ [fuzzySearch, options, searchValue, searchable, valueToRender],
82
82
  );
83
83
  const items = useMemo(() => transformOptionsToItems<T>(result, contentRender), [contentRender, result]);
84
84
 
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-console */
1
2
  import FuzzySearch from 'fuzzy-search';
2
3
  import { KeyboardEvent, KeyboardEventHandler, useCallback } from 'react';
3
4
 
@@ -47,11 +48,17 @@ const DEFAULT_MIN_SEARCH_INPUT_LENGTH = 2;
47
48
  /**
48
49
  * Нечеткий поиск среди айтемов по полям 'content.option', 'content.caption', 'content.description', 'label'
49
50
  */
50
- export function useFuzzySearch<T extends ContentRenderProps = ContentRenderProps>(
51
- options: FilterOption<T>[],
52
- flatMapOptions: (BaseOption<T> | AccordionOption<T> | NestListOption<T>)[],
53
- minSearchInputLength?: number,
54
- ) {
51
+ export function useFuzzySearch<T extends ContentRenderProps = ContentRenderProps>({
52
+ options,
53
+ flatMapOptions,
54
+ minSearchInputLength,
55
+ disableFuzzySearch,
56
+ }: {
57
+ options: FilterOption<T>[];
58
+ flatMapOptions: (BaseOption<T> | AccordionOption<T> | NestListOption<T>)[];
59
+ minSearchInputLength?: number;
60
+ disableFuzzySearch?: boolean;
61
+ }) {
55
62
  return useCallback(
56
63
  (search: string) => {
57
64
  const searcher = new FuzzySearch(
@@ -60,11 +67,27 @@ export function useFuzzySearch<T extends ContentRenderProps = ContentRenderProps
60
67
  {},
61
68
  );
62
69
 
70
+ console.log('disableFuzzySearch', disableFuzzySearch);
71
+
72
+ const plainSearcher = (searchQuery: string) =>
73
+ options.filter(option => {
74
+ let matchesQuery = false;
75
+ matchesQuery ||= Boolean(option.label?.toString().includes(String(searchQuery)));
76
+ if ('contentRenderProps' in option) {
77
+ matchesQuery ||= Boolean(option?.contentRenderProps?.description?.toString().includes(String(searchQuery)));
78
+ matchesQuery ||= Boolean(option?.contentRenderProps?.caption?.toString().includes(String(searchQuery)));
79
+ }
80
+ console.log('matchesQuery', matchesQuery);
81
+ return matchesQuery;
82
+ });
83
+
84
+ const searcherFunction = disableFuzzySearch ? plainSearcher : searcher.search;
85
+
63
86
  return search.length > (minSearchInputLength ?? DEFAULT_MIN_SEARCH_INPUT_LENGTH)
64
- ? searcher.search(search)
87
+ ? searcherFunction(search)
65
88
  : options;
66
89
  },
67
- [flatMapOptions, minSearchInputLength, options],
90
+ [disableFuzzySearch, flatMapOptions, minSearchInputLength, options],
68
91
  );
69
92
  }
70
93