@hh.ru/magritte-ui-tree-selector 5.5.80 → 5.5.82

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 (86) hide show
  1. package/Action.d.ts +1 -1
  2. package/Action.js +15 -12
  3. package/Action.js.map +1 -1
  4. package/Item.d.ts +3 -3
  5. package/Item.js +106 -54
  6. package/Item.js.map +1 -1
  7. package/ItemContent.d.ts +2 -2
  8. package/ItemContent.js +69 -15
  9. package/ItemContent.js.map +1 -1
  10. package/ItemsList.d.ts +3 -3
  11. package/ItemsList.js +243 -159
  12. package/ItemsList.js.map +1 -1
  13. package/MobileDelimiter.js +1 -1
  14. package/MobileDelimiter.js.map +1 -1
  15. package/MobileItem.d.ts +2 -2
  16. package/MobileItem.js +26 -5
  17. package/MobileItem.js.map +1 -1
  18. package/MobileItemsList.d.ts +2 -2
  19. package/MobileItemsList.js +259 -132
  20. package/MobileItemsList.js.map +1 -1
  21. package/MobileParentItem.d.ts +3 -3
  22. package/MobileParentItem.js +33 -14
  23. package/MobileParentItem.js.map +1 -1
  24. package/TreeSelector.d.ts +2 -2
  25. package/TreeSelector.js +49 -20
  26. package/TreeSelector.js.map +1 -1
  27. package/TreeSelectorDummy.d.ts +2 -2
  28. package/TreeSelectorDummy.js +178 -109
  29. package/TreeSelectorDummy.js.map +1 -1
  30. package/TreeSelectorItemBase.d.ts +2 -2
  31. package/TreeSelectorItemBase.js +12 -6
  32. package/TreeSelectorItemBase.js.map +1 -1
  33. package/TreeSelectorWrapper.js +38 -24
  34. package/TreeSelectorWrapper.js.map +1 -1
  35. package/collection/treeCollection.d.ts +1 -1
  36. package/collection/treeCollection.js +178 -184
  37. package/collection/treeCollection.js.map +1 -1
  38. package/collection/treeCollectionHelper.d.ts +9 -3
  39. package/collection/treeCollectionHelper.js +68 -105
  40. package/collection/treeCollectionHelper.js.map +1 -1
  41. package/collection/types.d.ts +1 -1
  42. package/index.css +45 -45
  43. package/index.d.ts +9 -9
  44. package/index.js +1 -1
  45. package/index.mock.d.ts +6 -6
  46. package/index.mock.js +14 -6
  47. package/index.mock.js.map +1 -1
  48. package/package.json +23 -22
  49. package/strategy/createSingleValueToggler.d.ts +1 -1
  50. package/strategy/createSingleValueToggler.js +17 -41
  51. package/strategy/createSingleValueToggler.js.map +1 -1
  52. package/strategy/createTreeCollectionToggler.d.ts +3 -3
  53. package/strategy/createTreeCollectionToggler.js +42 -58
  54. package/strategy/createTreeCollectionToggler.js.map +1 -1
  55. package/strategy/dummyToggle.d.ts +1 -1
  56. package/strategy/dummyToggle.js +10 -13
  57. package/strategy/dummyToggle.js.map +1 -1
  58. package/strategy/immutableSelectionStrategy.d.ts +3 -3
  59. package/strategy/immutableSelectionStrategy.js +27 -26
  60. package/strategy/immutableSelectionStrategy.js.map +1 -1
  61. package/strategy/selectionStrategy.d.ts +3 -3
  62. package/strategy/selectionStrategy.js +47 -56
  63. package/strategy/selectionStrategy.js.map +1 -1
  64. package/strategy/types.d.ts +1 -1
  65. package/tree-selector-item-BYBFlI_G.js +5 -0
  66. package/tree-selector-item-BYBFlI_G.js.map +1 -0
  67. package/types.d.ts +2 -2
  68. package/useAnimationTimeout.js +14 -14
  69. package/useAnimationTimeout.js.map +1 -1
  70. package/useDisabled.d.ts +2 -2
  71. package/useDisabled.js +7 -10
  72. package/useDisabled.js.map +1 -1
  73. package/useExpanded.d.ts +2 -2
  74. package/useExpanded.js +65 -54
  75. package/useExpanded.js.map +1 -1
  76. package/useIndeterminate.d.ts +2 -2
  77. package/useIndeterminate.js +17 -18
  78. package/useIndeterminate.js.map +1 -1
  79. package/useRenderInput.d.ts +1 -1
  80. package/useRenderInput.js +44 -32
  81. package/useRenderInput.js.map +1 -1
  82. package/useSelected.d.ts +3 -3
  83. package/useSelected.js +36 -31
  84. package/useSelected.js.map +1 -1
  85. package/tree-selector-item-BOEDcwrO.js +0 -5
  86. package/tree-selector-item-BOEDcwrO.js.map +0 -1
@@ -1,22 +1,22 @@
1
1
  import './index.css';
2
2
  import { useState, useLayoutEffect } from 'react';
3
3
 
4
- var styles = {"item-animation-timeout":"magritte-item-animation-timeout___Pdli9_5-5-80","itemAnimationTimeout":"magritte-item-animation-timeout___Pdli9_5-5-80","fade-in":"magritte-fade-in___FDoho_5-5-80","fadeIn":"magritte-fade-in___FDoho_5-5-80"};
4
+ var styles = {"item-animation-timeout":"magritte-item-animation-timeout___Pdli9_5-5-82","itemAnimationTimeout":"magritte-item-animation-timeout___Pdli9_5-5-82","fade-in":"magritte-fade-in___FDoho_5-5-82","fadeIn":"magritte-fade-in___FDoho_5-5-82"};
5
5
 
6
6
  const useAnimationTimeout = () => {
7
- const [animationTimeout, setAnimationTimeout] = useState(0);
8
- useLayoutEffect(() => {
9
- const animationTimeoutElement = document.createElement('div');
10
- animationTimeoutElement.classList.add(styles.itemAnimationTimeout);
11
- document.body.appendChild(animationTimeoutElement);
12
- const style = window.getComputedStyle(animationTimeoutElement);
13
- const animationTimeout = parseInt(style.getPropertyValue(`--animation-duration`), 10);
14
- if (Number.isInteger(animationTimeout)) {
15
- setAnimationTimeout(animationTimeout);
16
- }
17
- document.body.removeChild(animationTimeoutElement);
18
- }, []);
19
- return { animationTimeout };
7
+ const [animationTimeout, setAnimationTimeout] = useState(0);
8
+ useLayoutEffect(() => {
9
+ const animationTimeoutElement = document.createElement("div");
10
+ animationTimeoutElement.classList.add(styles.itemAnimationTimeout);
11
+ document.body.appendChild(animationTimeoutElement);
12
+ const style = window.getComputedStyle(animationTimeoutElement);
13
+ const animationTimeout2 = parseInt(style.getPropertyValue(`--animation-duration`), 10);
14
+ if (Number.isInteger(animationTimeout2)) {
15
+ setAnimationTimeout(animationTimeout2);
16
+ }
17
+ document.body.removeChild(animationTimeoutElement);
18
+ }, []);
19
+ return { animationTimeout };
20
20
  };
21
21
 
22
22
  export { useAnimationTimeout };
@@ -1 +1 @@
1
- {"version":3,"file":"useAnimationTimeout.js","sources":["src/useAnimationTimeout.ts"],"sourcesContent":["import { useLayoutEffect, useState } from 'react';\n\nimport styles from './animation.less';\n\ninterface UseAnimationTimeoutHook {\n (): { animationTimeout: number };\n}\n\nexport const useAnimationTimeout: UseAnimationTimeoutHook = () => {\n const [animationTimeout, setAnimationTimeout] = useState(0);\n\n useLayoutEffect(() => {\n const animationTimeoutElement = document.createElement('div');\n animationTimeoutElement.classList.add(styles.itemAnimationTimeout);\n document.body.appendChild(animationTimeoutElement);\n const style = window.getComputedStyle(animationTimeoutElement);\n const animationTimeout = parseInt(style.getPropertyValue(`--animation-duration`), 10);\n if (Number.isInteger(animationTimeout)) {\n setAnimationTimeout(animationTimeout);\n }\n document.body.removeChild(animationTimeoutElement);\n }, []);\n\n return { animationTimeout };\n};\n"],"names":[],"mappings":";;;;AAQO,MAAM,mBAAmB,GAA4B,MAAK;IAC7D,MAAM,CAAC,gBAAgB,EAAE,mBAAmB,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC;IAE5D,eAAe,CAAC,MAAK;QACjB,MAAM,uBAAuB,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;QAC9D,uBAAuB,CAAC,SAAS,CAAC,GAAG,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;AACnE,QAAA,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,uBAAuB,CAAC,CAAC;QACnD,MAAM,KAAK,GAAG,MAAM,CAAC,gBAAgB,CAAC,uBAAuB,CAAC,CAAC;AAC/D,QAAA,MAAM,gBAAgB,GAAG,QAAQ,CAAC,KAAK,CAAC,gBAAgB,CAAC,CAAsB,oBAAA,CAAA,CAAC,EAAE,EAAE,CAAC,CAAC;AACtF,QAAA,IAAI,MAAM,CAAC,SAAS,CAAC,gBAAgB,CAAC,EAAE;YACpC,mBAAmB,CAAC,gBAAgB,CAAC,CAAC;SACzC;AACD,QAAA,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,uBAAuB,CAAC,CAAC;KACtD,EAAE,EAAE,CAAC,CAAC;IAEP,OAAO,EAAE,gBAAgB,EAAE,CAAC;AAChC;;;;"}
1
+ {"version":3,"file":"useAnimationTimeout.js","sources":["src/useAnimationTimeout.ts"],"sourcesContent":["import { useLayoutEffect, useState } from 'react';\n\nimport styles from './animation.less';\n\ninterface UseAnimationTimeoutHook {\n (): { animationTimeout: number };\n}\n\nexport const useAnimationTimeout: UseAnimationTimeoutHook = () => {\n const [animationTimeout, setAnimationTimeout] = useState(0);\n\n useLayoutEffect(() => {\n const animationTimeoutElement = document.createElement('div');\n animationTimeoutElement.classList.add(styles.itemAnimationTimeout);\n document.body.appendChild(animationTimeoutElement);\n const style = window.getComputedStyle(animationTimeoutElement);\n const animationTimeout = parseInt(style.getPropertyValue(`--animation-duration`), 10);\n if (Number.isInteger(animationTimeout)) {\n setAnimationTimeout(animationTimeout);\n }\n document.body.removeChild(animationTimeoutElement);\n }, []);\n\n return { animationTimeout };\n};\n"],"names":["animationTimeout"],"mappings":";;;;AAQO,MAAM,sBAA+C,MAAM;AAC9D,EAAA,MAAM,CAAC,gBAAA,EAAkB,mBAAmB,CAAA,GAAI,SAAS,CAAC,CAAA,CAAA;AAE1D,EAAA,eAAA,CAAgB,MAAM;AAClB,IAAM,MAAA,uBAAA,GAA0B,QAAS,CAAA,aAAA,CAAc,KAAK,CAAA,CAAA;AAC5D,IAAwB,uBAAA,CAAA,SAAA,CAAU,GAAI,CAAA,MAAA,CAAO,oBAAoB,CAAA,CAAA;AACjE,IAAS,QAAA,CAAA,IAAA,CAAK,YAAY,uBAAuB,CAAA,CAAA;AACjD,IAAM,MAAA,KAAA,GAAQ,MAAO,CAAA,gBAAA,CAAiB,uBAAuB,CAAA,CAAA;AAC7D,IAAA,MAAMA,oBAAmB,QAAS,CAAA,KAAA,CAAM,gBAAiB,CAAA,CAAA,oBAAA,CAAsB,GAAG,EAAE,CAAA,CAAA;AACpF,IAAI,IAAA,MAAA,CAAO,SAAUA,CAAAA,iBAAgB,CAAG,EAAA;AACpC,MAAA,mBAAA,CAAoBA,iBAAgB,CAAA,CAAA;AAAA,KACxC;AACA,IAAS,QAAA,CAAA,IAAA,CAAK,YAAY,uBAAuB,CAAA,CAAA;AAAA,GACrD,EAAG,EAAE,CAAA,CAAA;AAEL,EAAA,OAAO,EAAE,gBAAiB,EAAA,CAAA;AAC9B;;;;"}
package/useDisabled.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { AdditionalDefault } from '@hh.ru/magritte-ui-tree-selector/collection/types';
2
- import { TreeSelectorProps } from '@hh.ru/magritte-ui-tree-selector/types';
1
+ import { AdditionalDefault } from './collection/types';
2
+ import { TreeSelectorProps } from './types';
3
3
  type UseDisabledHookProps<Additional extends AdditionalDefault> = {
4
4
  selected: string[];
5
5
  } & Pick<TreeSelectorProps<Additional>, 'disabled' | 'collection' | 'maxSelected'>;
package/useDisabled.js CHANGED
@@ -4,16 +4,13 @@ import { getIdsWithNoParentsInSameList } from './collection/treeCollectionHelper
4
4
  import './collection/treeCollection.js';
5
5
 
6
6
  const useDisabled = ({ selected, disabled, collection, maxSelected }) => {
7
- return useMemo(() => {
8
- const valueIds = getIdsWithNoParentsInSameList(collection, selected);
9
- if (maxSelected && valueIds.length >= maxSelected) {
10
- return collection
11
- .toList()
12
- .map(({ id }) => id)
13
- .filter((id) => !selected.includes(id));
14
- }
15
- return disabled;
16
- }, [selected, collection, disabled, maxSelected]);
7
+ return useMemo(() => {
8
+ const valueIds = getIdsWithNoParentsInSameList(collection, selected);
9
+ if (maxSelected && valueIds.length >= maxSelected) {
10
+ return collection.toList().map(({ id }) => id).filter((id) => !selected.includes(id));
11
+ }
12
+ return disabled;
13
+ }, [selected, collection, disabled, maxSelected]);
17
14
  };
18
15
 
19
16
  export { useDisabled };
@@ -1 +1 @@
1
- {"version":3,"file":"useDisabled.js","sources":["src/useDisabled.ts"],"sourcesContent":["import { useMemo } from 'react';\n\nimport { getIdsWithNoParentsInSameList } from '@hh.ru/magritte-ui-tree-selector/collection/treeCollectionHelper';\nimport { AdditionalDefault } from '@hh.ru/magritte-ui-tree-selector/collection/types';\nimport { TreeSelectorProps } from '@hh.ru/magritte-ui-tree-selector/types';\n\ntype UseDisabledHookProps<Additional extends AdditionalDefault> = {\n selected: string[];\n} & Pick<TreeSelectorProps<Additional>, 'disabled' | 'collection' | 'maxSelected'>;\ninterface UseDisabledHook {\n <Additional extends AdditionalDefault>(props: UseDisabledHookProps<Additional>): string[] | undefined;\n}\n\nexport const useDisabled: UseDisabledHook = ({ selected, disabled, collection, maxSelected }) => {\n return useMemo(() => {\n const valueIds = getIdsWithNoParentsInSameList(collection, selected);\n if (maxSelected && valueIds.length >= maxSelected) {\n return collection\n .toList()\n .map(({ id }) => id)\n .filter((id) => !selected.includes(id));\n }\n return disabled;\n }, [selected, collection, disabled, maxSelected]);\n};\n"],"names":[],"mappings":";;;;AAaO,MAAM,WAAW,GAAoB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,KAAI;IAC5F,OAAO,OAAO,CAAC,MAAK;QAChB,MAAM,QAAQ,GAAG,6BAA6B,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;QACrE,IAAI,WAAW,IAAI,QAAQ,CAAC,MAAM,IAAI,WAAW,EAAE;AAC/C,YAAA,OAAO,UAAU;AACZ,iBAAA,MAAM,EAAE;iBACR,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC;AACnB,iBAAA,MAAM,CAAC,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC;SAC/C;AACD,QAAA,OAAO,QAAQ,CAAC;KACnB,EAAE,CAAC,QAAQ,EAAE,UAAU,EAAE,QAAQ,EAAE,WAAW,CAAC,CAAC,CAAC;AACtD;;;;"}
1
+ {"version":3,"file":"useDisabled.js","sources":["src/useDisabled.ts"],"sourcesContent":["import { useMemo } from 'react';\n\nimport { getIdsWithNoParentsInSameList } from '@hh.ru/magritte-ui-tree-selector/collection/treeCollectionHelper';\nimport { AdditionalDefault } from '@hh.ru/magritte-ui-tree-selector/collection/types';\nimport { TreeSelectorProps } from '@hh.ru/magritte-ui-tree-selector/types';\n\ntype UseDisabledHookProps<Additional extends AdditionalDefault> = {\n selected: string[];\n} & Pick<TreeSelectorProps<Additional>, 'disabled' | 'collection' | 'maxSelected'>;\ninterface UseDisabledHook {\n <Additional extends AdditionalDefault>(props: UseDisabledHookProps<Additional>): string[] | undefined;\n}\n\nexport const useDisabled: UseDisabledHook = ({ selected, disabled, collection, maxSelected }) => {\n return useMemo(() => {\n const valueIds = getIdsWithNoParentsInSameList(collection, selected);\n if (maxSelected && valueIds.length >= maxSelected) {\n return collection\n .toList()\n .map(({ id }) => id)\n .filter((id) => !selected.includes(id));\n }\n return disabled;\n }, [selected, collection, disabled, maxSelected]);\n};\n"],"names":[],"mappings":";;;;AAaO,MAAM,cAA+B,CAAC,EAAE,UAAU,QAAU,EAAA,UAAA,EAAY,aAAkB,KAAA;AAC7F,EAAA,OAAO,QAAQ,MAAM;AACjB,IAAM,MAAA,QAAA,GAAW,6BAA8B,CAAA,UAAA,EAAY,QAAQ,CAAA,CAAA;AACnE,IAAI,IAAA,WAAA,IAAe,QAAS,CAAA,MAAA,IAAU,WAAa,EAAA;AAC/C,MAAA,OAAO,WACF,MAAO,EAAA,CACP,GAAI,CAAA,CAAC,EAAE,EAAG,EAAA,KAAM,EAAE,CAAA,CAClB,OAAO,CAAC,EAAA,KAAO,CAAC,QAAS,CAAA,QAAA,CAAS,EAAE,CAAC,CAAA,CAAA;AAAA,KAC9C;AACA,IAAO,OAAA,QAAA,CAAA;AAAA,KACR,CAAC,QAAA,EAAU,UAAY,EAAA,QAAA,EAAU,WAAW,CAAC,CAAA,CAAA;AACpD;;;;"}
package/useExpanded.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import TreeCollection from '@hh.ru/magritte-ui-tree-selector/collection/treeCollection';
2
- import { AdditionalDefault } from '@hh.ru/magritte-ui-tree-selector/collection/types';
1
+ import TreeCollection from './collection/treeCollection';
2
+ import { AdditionalDefault } from './collection/types';
3
3
  export declare const areEqualArrays: (arr1: string[], arr2: string[]) => boolean;
4
4
  export declare const calculateInitialExpanded: <Additional extends AdditionalDefault>(initialExpanded: string[], selected: string[], expandTreeOnSelected: boolean, collection: TreeCollection<Additional>) => string[];
5
5
  interface UseExpandedHookProps<Additional extends AdditionalDefault> {
package/useExpanded.js CHANGED
@@ -2,64 +2,75 @@ import './index.css';
2
2
  import { useMemo, useState, useRef, useCallback, useEffect } from 'react';
3
3
 
4
4
  const areEqualArrays = (arr1, arr2) => {
5
- if (arr1.length !== arr2.length) {
6
- return false;
7
- }
8
- const sortArr2 = arr2.slice().sort();
9
- return arr1
10
- .slice()
11
- .sort()
12
- .every((item, index) => item === sortArr2[index]);
5
+ if (arr1.length !== arr2.length) {
6
+ return false;
7
+ }
8
+ const sortArr2 = arr2.slice().sort();
9
+ return arr1.slice().sort().every((item, index) => item === sortArr2[index]);
13
10
  };
14
11
  const calculateInitialExpanded = (initialExpanded, selected, expandTreeOnSelected, collection) => {
15
- const expanded = initialExpanded.slice();
16
- if (expandTreeOnSelected) {
17
- selected.forEach((id) => {
18
- expanded.push(...collection.getParentIds(id));
19
- });
20
- }
21
- return [...new Set(expanded)];
12
+ const expanded = initialExpanded.slice();
13
+ if (expandTreeOnSelected) {
14
+ selected.forEach((id) => {
15
+ expanded.push(...collection.getParentIds(id));
16
+ });
17
+ }
18
+ return [...new Set(expanded)];
22
19
  };
23
- const useExpanded = ({ initialValue, selected, expandTreeOnSelected, collection, onExpand, }) => {
24
- const calculatedInitialValue = useMemo(() => calculateInitialExpanded(initialValue, selected, expandTreeOnSelected, collection),
20
+ const useExpanded = ({
21
+ initialValue,
22
+ selected,
23
+ expandTreeOnSelected,
24
+ collection,
25
+ onExpand
26
+ }) => {
27
+ const calculatedInitialValue = useMemo(
28
+ () => calculateInitialExpanded(initialValue, selected, expandTreeOnSelected, collection),
25
29
  // eslint-disable-next-line react-hooks/exhaustive-deps
26
- []);
27
- const [expanded, setExpanded] = useState(calculatedInitialValue);
28
- const expandedRef = useRef(calculatedInitialValue);
29
- const selectedRef = useRef(selected);
30
- selectedRef.current = selected;
31
- const handlerRef = useRef(onExpand);
32
- const handleSetExpanded = useCallback((updatedExpanded) => {
33
- handlerRef.current?.(updatedExpanded.slice());
34
- if (!areEqualArrays(expandedRef.current, updatedExpanded)) {
35
- setExpanded(updatedExpanded);
36
- }
37
- }, []);
38
- const handleExpansion = useCallback((id) => {
39
- let updatedExpanded;
40
- if (expandedRef.current.includes(id)) {
41
- updatedExpanded = expandedRef.current.filter((itemId) => itemId !== id);
42
- }
43
- else {
44
- updatedExpanded = expandedRef.current.slice();
45
- updatedExpanded.push(id);
46
- }
47
- handleSetExpanded(updatedExpanded);
48
- }, [handleSetExpanded]);
49
- const handleResetExpanded = useCallback(() => {
50
- const calculatedResetValue = calculateInitialExpanded(initialValue, selectedRef.current, expandTreeOnSelected, collection);
51
- if (!areEqualArrays(expandedRef.current, calculatedResetValue)) {
52
- setExpanded(calculatedResetValue);
53
- }
54
- // eslint-disable-next-line react-hooks/exhaustive-deps
55
- }, []);
56
- useEffect(() => {
57
- handlerRef.current = onExpand;
58
- }, [onExpand]);
59
- useEffect(() => {
60
- expandedRef.current = expanded;
61
- }, [expanded]);
62
- return { expanded, setExpanded: handleSetExpanded, handleExpansion, handleResetExpanded };
30
+ []
31
+ );
32
+ const [expanded, setExpanded] = useState(calculatedInitialValue);
33
+ const expandedRef = useRef(calculatedInitialValue);
34
+ const selectedRef = useRef(selected);
35
+ selectedRef.current = selected;
36
+ const handlerRef = useRef(onExpand);
37
+ const handleSetExpanded = useCallback((updatedExpanded) => {
38
+ handlerRef.current?.(updatedExpanded.slice());
39
+ if (!areEqualArrays(expandedRef.current, updatedExpanded)) {
40
+ setExpanded(updatedExpanded);
41
+ }
42
+ }, []);
43
+ const handleExpansion = useCallback(
44
+ (id) => {
45
+ let updatedExpanded;
46
+ if (expandedRef.current.includes(id)) {
47
+ updatedExpanded = expandedRef.current.filter((itemId) => itemId !== id);
48
+ } else {
49
+ updatedExpanded = expandedRef.current.slice();
50
+ updatedExpanded.push(id);
51
+ }
52
+ handleSetExpanded(updatedExpanded);
53
+ },
54
+ [handleSetExpanded]
55
+ );
56
+ const handleResetExpanded = useCallback(() => {
57
+ const calculatedResetValue = calculateInitialExpanded(
58
+ initialValue,
59
+ selectedRef.current,
60
+ expandTreeOnSelected,
61
+ collection
62
+ );
63
+ if (!areEqualArrays(expandedRef.current, calculatedResetValue)) {
64
+ setExpanded(calculatedResetValue);
65
+ }
66
+ }, []);
67
+ useEffect(() => {
68
+ handlerRef.current = onExpand;
69
+ }, [onExpand]);
70
+ useEffect(() => {
71
+ expandedRef.current = expanded;
72
+ }, [expanded]);
73
+ return { expanded, setExpanded: handleSetExpanded, handleExpansion, handleResetExpanded };
63
74
  };
64
75
 
65
76
  export { areEqualArrays, calculateInitialExpanded, useExpanded };
@@ -1 +1 @@
1
- {"version":3,"file":"useExpanded.js","sources":["src/useExpanded.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState, useMemo } from 'react';\n\nimport TreeCollection from '@hh.ru/magritte-ui-tree-selector/collection/treeCollection';\nimport { AdditionalDefault } from '@hh.ru/magritte-ui-tree-selector/collection/types';\n\nexport const areEqualArrays = (arr1: string[], arr2: string[]): boolean => {\n if (arr1.length !== arr2.length) {\n return false;\n }\n const sortArr2 = arr2.slice().sort();\n return arr1\n .slice()\n .sort()\n .every((item, index) => item === sortArr2[index]);\n};\n\nexport const calculateInitialExpanded = <Additional extends AdditionalDefault>(\n initialExpanded: string[],\n selected: string[],\n expandTreeOnSelected: boolean,\n collection: TreeCollection<Additional>\n): string[] => {\n const expanded: string[] = initialExpanded.slice();\n if (expandTreeOnSelected) {\n selected.forEach((id) => {\n expanded.push(...collection.getParentIds(id));\n });\n }\n return [...new Set(expanded)];\n};\n\ninterface UseExpandedHookProps<Additional extends AdditionalDefault> {\n initialValue: string[];\n onExpand?: (expanded: string[]) => void;\n expandTreeOnSelected: boolean;\n selected: string[];\n collection: TreeCollection<Additional>;\n}\n\ninterface UseExpandedHookReturn {\n expanded: string[];\n setExpanded: (expanded: string[]) => void;\n handleExpansion: (id: string) => void;\n handleResetExpanded: () => void;\n}\ninterface UseExpandedHook {\n <Additional extends AdditionalDefault>(props: UseExpandedHookProps<Additional>): UseExpandedHookReturn;\n}\n\nexport const useExpanded: UseExpandedHook = ({\n initialValue,\n selected,\n expandTreeOnSelected,\n collection,\n onExpand,\n}) => {\n const calculatedInitialValue = useMemo(\n () => calculateInitialExpanded(initialValue, selected, expandTreeOnSelected, collection),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n []\n );\n const [expanded, setExpanded] = useState(calculatedInitialValue);\n const expandedRef = useRef(calculatedInitialValue);\n const selectedRef = useRef(selected);\n selectedRef.current = selected;\n const handlerRef = useRef(onExpand);\n\n const handleSetExpanded = useCallback((updatedExpanded: string[]) => {\n handlerRef.current?.(updatedExpanded.slice());\n\n if (!areEqualArrays(expandedRef.current, updatedExpanded)) {\n setExpanded(updatedExpanded);\n }\n }, []);\n\n const handleExpansion = useCallback(\n (id: string): void => {\n let updatedExpanded;\n if (expandedRef.current.includes(id)) {\n updatedExpanded = expandedRef.current.filter((itemId) => itemId !== id);\n } else {\n updatedExpanded = expandedRef.current.slice();\n updatedExpanded.push(id);\n }\n handleSetExpanded(updatedExpanded);\n },\n [handleSetExpanded]\n );\n\n const handleResetExpanded = useCallback(() => {\n const calculatedResetValue = calculateInitialExpanded(\n initialValue,\n selectedRef.current,\n expandTreeOnSelected,\n collection\n );\n if (!areEqualArrays(expandedRef.current, calculatedResetValue)) {\n setExpanded(calculatedResetValue);\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n useEffect(() => {\n handlerRef.current = onExpand;\n }, [onExpand]);\n\n useEffect(() => {\n expandedRef.current = expanded;\n }, [expanded]);\n\n return { expanded, setExpanded: handleSetExpanded, handleExpansion, handleResetEx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C,CAAC;SACrC;;KAEJ,EAAE,EAAE,CAAC,CAAC;IAEP,SAAS,CAAC,MAAK;AACX,QAAA,UAAU,CAAC,OAAO,GAAG,QAAQ,CAAC;AAClC,KAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,SAAS,CAAC,MAAK;AACX,QAAA,WAAW,CAAC,OAAO,GAAG,QAAQ,CAAC;AACnC,KAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,iBAAiB,EAAE,eAAe,EAAE,mBAAmB,EAAE,CAAC;AAC9F;;;;"}
1
+ {"version":3,"file":"useExpanded.js","sources":["src/useExpanded.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState, useMemo } from 'react';\n\nimport TreeCollection from '@hh.ru/magritte-ui-tree-selector/collection/treeCollection';\nimport { AdditionalDefault } from '@hh.ru/magritte-ui-tree-selector/collection/types';\n\nexport const areEqualArrays = (arr1: string[], arr2: string[]): boolean => {\n if (arr1.length !== arr2.length) {\n return false;\n }\n const sortArr2 = arr2.slice().sort();\n return arr1\n .slice()\n .sort()\n .every((item, index) => item === sortArr2[index]);\n};\n\nexport const calculateInitialExpanded = <Additional extends AdditionalDefault>(\n initialExpanded: string[],\n selected: string[],\n expandTreeOnSelected: boolean,\n collection: TreeCollection<Additional>\n): string[] => {\n const expanded: string[] = initialExpanded.slice();\n if (expandTreeOnSelected) {\n selected.forEach((id) => {\n expanded.push(...collection.getParentIds(id));\n });\n }\n return [...new Set(expanded)];\n};\n\ninterface UseExpandedHookProps<Additional extends AdditionalDefault> {\n initialValue: string[];\n onExpand?: (expanded: string[]) => void;\n expandTreeOnSelected: boolean;\n selected: string[];\n collection: TreeCollection<Additional>;\n}\n\ninterface UseExpandedHookReturn {\n expanded: string[];\n setExpanded: (expanded: string[]) => void;\n handleExpansion: (id: string) => void;\n handleResetExpanded: () => void;\n}\ninterface UseExpandedHook {\n <Additional extends AdditionalDefault>(props: UseExpandedHookProps<Additional>): UseExpandedHookReturn;\n}\n\nexport const useExpanded: UseExpandedHook = ({\n initialValue,\n selected,\n expandTreeOnSelected,\n collection,\n onExpand,\n}) => {\n const calculatedInitialValue = useMemo(\n () => calculateInitialExpanded(initialValue, selected, expandTreeOnSelected, collection),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n []\n );\n const [expanded, setExpanded] = useState(calculatedInitialValue);\n const expandedRef = useRef(calculatedInitialValue);\n const selectedRef = useRef(selected);\n selectedRef.current = selected;\n const handlerRef = useRef(onExpand);\n\n const handleSetExpanded = useCallback((updatedExpanded: string[]) => {\n handlerRef.current?.(updatedExpanded.slice());\n\n if (!areEqualArrays(expandedRef.current, updatedExpanded)) {\n setExpanded(updatedExpanded);\n }\n }, []);\n\n const handleExpansion = useCallback(\n (id: string): void => {\n let updatedExpanded;\n if (expandedRef.current.includes(id)) {\n updatedExpanded = expandedRef.current.filter((itemId) => itemId !== id);\n } else {\n updatedExpanded = expandedRef.current.slice();\n updatedExpanded.push(id);\n }\n handleSetExpanded(updatedExpanded);\n },\n [handleSetExpanded]\n );\n\n const handleResetExpanded = useCallback(() => {\n const calculatedResetValue = calculateInitialExpanded(\n initialValue,\n selectedRef.current,\n expandTreeOnSelected,\n collection\n );\n if (!areEqualArrays(expandedRef.current, calculatedResetValue)) {\n setExpanded(calculatedResetValue);\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n useEffect(() => {\n handlerRef.current = onExpand;\n }, [onExpand]);\n\n useEffect(() => {\n expandedRef.current = expanded;\n }, [expanded]);\n\n return { expanded, setExpanded: handleSetExpanded, handleExpansion, handleResetExpanded };\n};\n"],"names":[],"mappings":";;AAKa,MAAA,cAAA,GAAiB,CAAC,IAAA,EAAgB,IAA4B,KAAA;AACvE,EAAI,IAAA,IAAA,CAAK,MAAW,KAAA,IAAA,CAAK,MAAQ,EAAA;AAC7B,IAAO,OAAA,KAAA,CAAA;AAAA,GACX;AACA,EAAA,MAAM,Q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UAAA,CAAW,OAAU,GAAA,QAAA,CAAA;AAAA,GACzB,EAAG,CAAC,QAAQ,CAAC,CAAA,CAAA;AAEb,EAAA,SAAA,CAAU,MAAM;AACZ,IAAA,WAAA,CAAY,OAAU,GAAA,QAAA,CAAA;AAAA,GAC1B,EAAG,CAAC,QAAQ,CAAC,CAAA,CAAA;AAEb,EAAA,OAAO,EAAE,QAAA,EAAU,WAAa,EAAA,iBAAA,EAAmB,iBAAiB,mBAAoB,EAAA,CAAA;AAC5F;;;;"}
@@ -1,5 +1,5 @@
1
- import TreeCollection from '@hh.ru/magritte-ui-tree-selector/collection/treeCollection';
2
- import { AdditionalDefault } from '@hh.ru/magritte-ui-tree-selector/collection/types';
1
+ import TreeCollection from './collection/treeCollection';
2
+ import { AdditionalDefault } from './collection/types';
3
3
  /**
4
4
  * Возвращает массив ID элементов дерева, у которых есть и выбранные, и невыбранные потомки.
5
5
  */
@@ -2,27 +2,26 @@ import './index.css';
2
2
  import { useState, useEffect } from 'react';
3
3
  import { areEqualArrays } from './useExpanded.js';
4
4
 
5
- /**
6
- * Возвращает массив ID элементов дерева, у которых есть и выбранные, и невыбранные потомки.
7
- */
8
5
  function getIndeterminateParentIds(collection, selected) {
9
- const result = selected.reduce((currentResult, id) => {
10
- collection.getParentIds(id).forEach((parentId) => {
11
- if (!selected.includes(parentId)) {
12
- currentResult.add(parentId);
13
- }
14
- });
15
- return currentResult;
16
- }, new Set());
17
- return [...result];
6
+ const result = selected.reduce((currentResult, id) => {
7
+ collection.getParentIds(id).forEach((parentId) => {
8
+ if (!selected.includes(parentId)) {
9
+ currentResult.add(parentId);
10
+ }
11
+ });
12
+ return currentResult;
13
+ }, /* @__PURE__ */ new Set());
14
+ return [...result];
18
15
  }
19
16
  const useIndeterminate = ({ collection, selected }) => {
20
- const [indeterminate, setIndeterminate] = useState([]);
21
- useEffect(() => {
22
- const updatedIndeterminateIds = getIndeterminateParentIds(collection, selected);
23
- setIndeterminate((current) => areEqualArrays(current, updatedIndeterminateIds) ? current : updatedIndeterminateIds);
24
- }, [selected, collection]);
25
- return { indeterminate };
17
+ const [indeterminate, setIndeterminate] = useState([]);
18
+ useEffect(() => {
19
+ const updatedIndeterminateIds = getIndeterminateParentIds(collection, selected);
20
+ setIndeterminate(
21
+ (current) => areEqualArrays(current, updatedIndeterminateIds) ? current : updatedIndeterminateIds
22
+ );
23
+ }, [selected, collection]);
24
+ return { indeterminate };
26
25
  };
27
26
 
28
27
  export { getIndeterminateParentIds, useIndeterminate };
@@ -1 +1 @@
1
- {"version":3,"file":"useIndeterminate.js","sources":["src/useIndeterminate.ts"],"sourcesContent":["import { useState, useEffect } from 'react';\n\nimport TreeCollection from '@hh.ru/magritte-ui-tree-selector/collection/treeCollection';\nimport { AdditionalDefault } from '@hh.ru/magritte-ui-tree-selector/collection/types';\nimport { areEqualArrays } from '@hh.ru/magritte-ui-tree-selector/useExpanded';\n\n/**\n * Возвращает массив ID элементов дерева, у которых есть и выбранные, и невыбранные потомки.\n */\nexport function getIndeterminateParentIds<Additional extends AdditionalDefault>(\n collection: TreeCollection<Additional>,\n selected: string[]\n): string[] {\n const result = selected.reduce((currentResult, id) => {\n collection.getParentIds(id).forEach((parentId) => {\n if (!selected.includes(parentId)) {\n currentResult.add(parentId);\n }\n });\n return currentResult;\n }, new Set<string>());\n return [...result];\n}\n\ninterface UseIndeterminateHookProps<Additional extends AdditionalDefault> {\n collection: TreeCollection<Additional>;\n selected: string[];\n}\ninterface UseIndeterminateHook {\n <Additional extends AdditionalDefault>(props: UseIndeterminateHookProps<Additional>): { indeterminate: string[] };\n}\n\nexport const useIndeterminate: UseIndeterminateHook = ({ collection, selected }) => {\n const [indeterminate, setIndeterminate] = useState<string[]>([]);\n\n useEffect(() => {\n const updatedIndeterminateIds = getIndeterminateParentIds(collection, selected);\n setIndeterminate((current) =>\n areEqualArrays(current, updatedIndeterminateIds) ? current : updatedIndeterminateIds\n );\n }, [selected, collection]);\n return { indeterminate };\n};\n"],"names":[],"mappings":";;;AAMA;;AAEG;AACa,SAAA,yBAAyB,CACrC,UAAsC,EACtC,QAAkB,EAAA;IAElB,MAAM,MAAM,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,aAAa,EAAE,EAAE,KAAI;QACjD,UAAU,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,QAAQ,KAAI;YAC7C,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE;AAC9B,gBAAA,aAAa,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;aAC/B;AACL,SAAC,CAAC,CAAC;AACH,QAAA,OAAO,aAAa,CAAC;AACzB,KAAC,EAAE,IAAI,GAAG,EAAU,CAAC,CAAC;AACtB,IAAA,OAAO,CAAC,GAAG,MAAM,CAAC,CAAC;AACvB,CAAC;AAUY,MAAA,gBAAgB,GAAyB,CAAC,EAAE,UAAU,EAAE,QAAQ,EAAE,KAAI;IAC/E,MAAM,CAAC,aAAa,EAAE,gBAAgB,CAAC,GAAG,QAAQ,CAAW,EAAE,CAAC,CAAC;IAEjE,SAAS,CAAC,MAAK;QACX,MAAM,uBAAuB,GAAG,yBAAyB,CAAC,UAAU,EAAE,QAAQ,CAAC,CAAC;QAChF,gBAAgB,CAAC,CAAC,OAAO,KACrB,cAAc,CAAC,OAAO,EAAE,uBAAuB,CAAC,GAAG,OAAO,GAAG,uBAAuB,CACvF,CAAC;AACN,KAAC,EAAE,CAAC,QAAQ,EAAE,UAAU,CAAC,CAAC,CAAC;IAC3B,OAAO,EAAE,aAAa,EAAE,CAAC;AAC7B;;;;"}
1
+ {"version":3,"file":"useIndeterminate.js","sources":["src/useIndeterminate.ts"],"sourcesContent":["import { useState, useEffect } from 'react';\n\nimport TreeCollection from '@hh.ru/magritte-ui-tree-selector/collection/treeCollection';\nimport { AdditionalDefault } from '@hh.ru/magritte-ui-tree-selector/collection/types';\nimport { areEqualArrays } from '@hh.ru/magritte-ui-tree-selector/useExpanded';\n\n/**\n * Возвращает массив ID элементов дерева, у которых есть и выбранные, и невыбранные потомки.\n */\nexport function getIndeterminateParentIds<Additional extends AdditionalDefault>(\n collection: TreeCollection<Additional>,\n selected: string[]\n): string[] {\n const result = selected.reduce((currentResult, id) => {\n collection.getParentIds(id).forEach((parentId) => {\n if (!selected.includes(parentId)) {\n currentResult.add(parentId);\n }\n });\n return currentResult;\n }, new Set<string>());\n return [...result];\n}\n\ninterface UseIndeterminateHookProps<Additional extends AdditionalDefault> {\n collection: TreeCollection<Additional>;\n selected: string[];\n}\ninterface UseIndeterminateHook {\n <Additional extends AdditionalDefault>(props: UseIndeterminateHookProps<Additional>): { indeterminate: string[] };\n}\n\nexport const useIndeterminate: UseIndeterminateHook = ({ collection, selected }) => {\n const [indeterminate, setIndeterminate] = useState<string[]>([]);\n\n useEffect(() => {\n const updatedIndeterminateIds = getIndeterminateParentIds(collection, selected);\n setIndeterminate((current) =>\n areEqualArrays(current, updatedIndeterminateIds) ? current : updatedIndeterminateIds\n );\n }, [selected, collection]);\n return { indeterminate };\n};\n"],"names":[],"mappings":";;;AASO,SAAS,yBAAA,CACZ,YACA,QACQ,EAAA;AACR,EAAA,MAAM,MAAS,GAAA,QAAA,CAAS,MAAO,CAAA,CAAC,eAAe,EAAO,KAAA;AAClD,IAAA,UAAA,CAAW,YAAa,CAAA,EAAE,CAAE,CAAA,OAAA,CAAQ,CAAC,QAAa,KAAA;AAC9C,MAAA,IAAI,CAAC,QAAA,CAAS,QAAS,CAAA,QAAQ,CAAG,EAAA;AAC9B,QAAA,aAAA,CAAc,IAAI,QAAQ,CAAA,CAAA;AAAA,OAC9B;AAAA,KACH,CAAA,CAAA;AACD,IAAO,OAAA,aAAA,CAAA;AAAA,GACX,kBAAO,IAAA,GAAA,EAAa,CAAA,CAAA;AACpB,EAAO,OAAA,CAAC,GAAG,MAAM,CAAA,CAAA;AACrB,CAAA;AAUO,MAAM,gBAAyC,GAAA,CAAC,EAAE,UAAA,EAAY,UAAe,KAAA;AAChF,EAAA,MAAM,CAAC,aAAe,EAAA,gBAAgB,CAAI,GAAA,QAAA,CAAmB,EAAE,CAAA,CAAA;AAE/D,EAAA,SAAA,CAAU,MAAM;AACZ,IAAM,MAAA,uBAAA,GAA0B,yBAA0B,CAAA,UAAA,EAAY,QAAQ,CAAA,CAAA;AAC9E,IAAA,gBAAA;AAAA,MAAiB,CAAC,OACd,KAAA,cAAA,CAAe,OAAS,EAAA,uBAAuB,IAAI,OAAU,GAAA,uBAAA;AAAA,KACjE,CAAA;AAAA,GACD,EAAA,CAAC,QAAU,EAAA,UAAU,CAAC,CAAA,CAAA;AACzB,EAAA,OAAO,EAAE,aAAc,EAAA,CAAA;AAC3B;;;;"}
@@ -1,5 +1,5 @@
1
1
  import { ReactElement, RefObject } from 'react';
2
- import { SearchInputPropsWithRef } from '@hh.ru/magritte-ui-tree-selector/types';
2
+ import { SearchInputPropsWithRef } from './types';
3
3
  export interface UseRenderInputHook {
4
4
  (inputRef: RefObject<HTMLInputElement>): {
5
5
  contentFilterQuery: string;
package/useRenderInput.js CHANGED
@@ -8,40 +8,52 @@ import { SearchInput } from '@hh.ru/magritte-ui-input';
8
8
  import { useAnimationTimeout } from './useAnimationTimeout.js';
9
9
 
10
10
  const WrapperInput = forwardRef(({ parentRef, ...props }, ref) => {
11
- const multiRef = useMultipleRefs(ref, parentRef);
12
- return jsx(SearchInput, { ...props, ref: multiRef, "data-qa": "tree-selector-search-input" });
11
+ const multiRef = useMultipleRefs(ref, parentRef);
12
+ return /* @__PURE__ */ jsx(SearchInput, { ...props, ref: multiRef, "data-qa": "tree-selector-search-input" });
13
13
  });
14
- WrapperInput.displayName = 'TreeSelectorInput';
14
+ WrapperInput.displayName = "TreeSelectorInput";
15
15
  const useRenderInput = (inputRef) => {
16
- const [contentFilterQuery, setContentFilterQuery] = useState('');
17
- const { animationTimeout } = useAnimationTimeout();
18
- const [value, setValue] = useState('');
19
- const { isMobile } = useBreakpoint();
20
- const inputRefCallback = useCallback((node) => {
21
- if (!node) {
22
- setValue('');
23
- setContentFilterQuery('');
24
- }
25
- }, []);
26
- const ref = useMultipleRefs(inputRef, inputRefCallback);
27
- useEffect(() => {
28
- setValue('');
29
- setContentFilterQuery('');
30
- }, [isMobile]);
31
- const debouncedContentFilterQuery = useDebounce(useCallback((newValue) => {
32
- setContentFilterQuery(newValue);
33
- }, []), animationTimeout);
34
- const handleChangeInput = useCallback((newValue) => {
35
- debouncedContentFilterQuery(newValue);
36
- setValue(newValue);
37
- }, [debouncedContentFilterQuery]);
38
- const setInputValue = useCallback((newValue) => {
39
- setValue(newValue);
40
- }, [setValue]);
41
- const renderInput = useCallback((props) => {
42
- return jsx(WrapperInput, { ...props, value: value, onChange: handleChangeInput, parentRef: ref });
43
- }, [ref, value, handleChangeInput]);
44
- return { contentFilterQuery, setInputValue, handleChangeInput, renderInput };
16
+ const [contentFilterQuery, setContentFilterQuery] = useState("");
17
+ const { animationTimeout } = useAnimationTimeout();
18
+ const [value, setValue] = useState("");
19
+ const { isMobile } = useBreakpoint();
20
+ const inputRefCallback = useCallback((node) => {
21
+ if (!node) {
22
+ setValue("");
23
+ setContentFilterQuery("");
24
+ }
25
+ }, []);
26
+ const ref = useMultipleRefs(inputRef, inputRefCallback);
27
+ useEffect(() => {
28
+ setValue("");
29
+ setContentFilterQuery("");
30
+ }, [isMobile]);
31
+ const debouncedContentFilterQuery = useDebounce(
32
+ useCallback((newValue) => {
33
+ setContentFilterQuery(newValue);
34
+ }, []),
35
+ animationTimeout
36
+ );
37
+ const handleChangeInput = useCallback(
38
+ (newValue) => {
39
+ debouncedContentFilterQuery(newValue);
40
+ setValue(newValue);
41
+ },
42
+ [debouncedContentFilterQuery]
43
+ );
44
+ const setInputValue = useCallback(
45
+ (newValue) => {
46
+ setValue(newValue);
47
+ },
48
+ [setValue]
49
+ );
50
+ const renderInput = useCallback(
51
+ (props) => {
52
+ return /* @__PURE__ */ jsx(WrapperInput, { ...props, value, onChange: handleChangeInput, parentRef: ref });
53
+ },
54
+ [ref, value, handleChangeInput]
55
+ );
56
+ return { contentFilterQuery, setInputValue, handleChangeInput, renderInput };
45
57
  };
46
58
 
47
59
  export { useRenderInput };
@@ -1 +1 @@
1
- {"version":3,"file":"useRenderInput.js","sources":["src/useRenderInput.tsx"],"sourcesContent":["import { ReactElement, RefCallback, useCallback, useEffect, useState, RefObject, forwardRef } from 'react';\n\nimport { useDebounce } from '@hh.ru/magritte-common-func-utils';\nimport { useMultipleRefs } from '@hh.ru/magritte-common-use-multiple-refs';\nimport { useBreakpoint } from '@hh.ru/magritte-ui-breakpoint';\nimport { SearchInput, SearchInputProps } from '@hh.ru/magritte-ui-input';\nimport { SearchInputPropsWithRef } from '@hh.ru/magritte-ui-tree-selector/types';\nimport { useAnimationTimeout } from '@hh.ru/magritte-ui-tree-selector/useAnimationTimeout';\n\ntype WrapperInputProps = SearchInputProps & {\n value: string;\n onChange: (newValue: string) => void;\n parentRef: RefCallback<HTMLInputElement>;\n};\n\nconst WrapperInput = forwardRef<HTMLInputElement, WrapperInputProps>(({ parentRef, ...props }, ref) => {\n const multiRef = useMultipleRefs(ref, parentRef);\n return <SearchInput {...props} ref={multiRef} data-qa=\"tree-selector-search-input\" />;\n});\n\nWrapperInput.displayName = 'TreeSelectorInput';\n\nexport interface UseRenderInputHook {\n (inputRef: RefObject<HTMLInputElement>): {\n contentFilterQuery: string;\n setInputValue: (newValue: string) => void;\n handleChangeInput: (newValue: string) => void;\n renderInput: (props?: SearchInputPropsWithRef) => ReactElement;\n };\n}\n\nexport const useRenderInput: UseRenderInputHook = (inputRef) => {\n const [contentFilterQuery, setContentFilterQuery] = useState('');\n const { animationTimeout } = useAnimationTimeout();\n const [value, setValue] = useState('');\n const { isMobile } = useBreakpoint();\n\n const inputRefCallback: RefCallback<HTMLInputElement> = useCallback((node) => {\n if (!node) {\n setValue('');\n setContentFilterQuery('');\n }\n }, []);\n\n const ref = useMultipleRefs(inputRef, inputRefCallback);\n\n useEffect(() => {\n setValue('');\n setContentFilterQuery('');\n }, [isMobile]);\n\n const debouncedContentFilterQuery = useDebounce(\n useCallback((newValue: string) => {\n setContentFilterQuery(newValue);\n }, []),\n animationTimeout\n );\n\n const handleChangeInput = useCallback(\n (newValue: string) => {\n debouncedContentFilterQuery(newValue);\n setValue(newValue);\n },\n [debouncedContentFilterQuery]\n );\n\n const setInputValue = useCallback(\n (newValue: string) => {\n setValue(newValue);\n },\n [setValue]\n );\n\n const renderInput = useCallback(\n (props?: SearchInputPropsWithRef) => {\n return <WrapperInput {...props} value={value} onChange={handleChangeInput} parentRef={ref} />;\n },\n [ref, value, handleChangeInput]\n );\n\n return { contentFilterQuery, setInputValue, handleChangeInput, renderInput };\n};\n"],"names":["_jsx"],"mappings":";;;;;;;;AAeA,MAAM,YAAY,GAAG,UAAU,CAAsC,CAAC,EAAE,SAAS,EAAE,GAAG,KAAK,EAAE,EAAE,GAAG,KAAI;IAClG,MAAM,QAAQ,GAAG,eAAe,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;IACjD,OAAOA,GAAA,CAAC,WAAW,EAAA,EAAA,GAAK,KAAK,EAAE,GAAG,EAAE,QAAQ,EAAA,SAAA,EAAU,4BAA4B,EAAA,CAAG,CAAC;AAC1F,CAAC,CAAC,CAAC;AAEH,YAAY,CAAC,WAAW,GAAG,mBAAmB,CAAC;AAWlC,MAAA,cAAc,GAAuB,CAAC,QAAQ,KAAI;IAC3D,MAAM,CAAC,kBAAkB,EAAE,qBAAqB,CAAC,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC;AACjE,IAAA,MAAM,EAAE,gBAAgB,EAAE,GAAG,mBAAmB,EAAE,CAAC;IACnD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,CAAC,EAAE,CAAC,CAAC;AACvC,IAAA,MAAM,EAAE,QAAQ,EAAE,GAAG,aAAa,EAAE,CAAC;AAErC,IAAA,MAAM,gBAAgB,GAAkC,WAAW,CAAC,CAAC,IAAI,KAAI;QACzE,IAAI,CAAC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derInput.js","sources":["src/useRenderInput.tsx"],"sourcesContent":["import { ReactElement, RefCallback, useCallback, useEffect, useState, RefObject, forwardRef } from 'react';\n\nimport { useDebounce } from '@hh.ru/magritte-common-func-utils';\nimport { useMultipleRefs } from '@hh.ru/magritte-common-use-multiple-refs';\nimport { useBreakpoint } from '@hh.ru/magritte-ui-breakpoint';\nimport { SearchInput, SearchInputProps } from '@hh.ru/magritte-ui-input';\nimport { SearchInputPropsWithRef } from '@hh.ru/magritte-ui-tree-selector/types';\nimport { useAnimationTimeout } from '@hh.ru/magritte-ui-tree-selector/useAnimationTimeout';\n\ntype WrapperInputProps = SearchInputProps & {\n value: string;\n onChange: (newValue: string) => void;\n parentRef: RefCallback<HTMLInputElement>;\n};\n\nconst WrapperInput = forwardRef<HTMLInputElement, WrapperInputProps>(({ parentRef, ...props }, ref) => {\n const multiRef = useMultipleRefs(ref, parentRef);\n return <SearchInput {...props} ref={multiRef} data-qa=\"tree-selector-search-input\" />;\n});\n\nWrapperInput.displayName = 'TreeSelectorInput';\n\nexport interface UseRenderInputHook {\n (inputRef: RefObject<HTMLInputElement>): {\n contentFilterQuery: string;\n setInputValue: (newValue: string) => void;\n handleChangeInput: (newValue: string) => void;\n renderInput: (props?: SearchInputPropsWithRef) => ReactElement;\n };\n}\n\nexport const useRenderInput: UseRenderInputHook = (inputRef) => {\n const [contentFilterQuery, setContentFilterQuery] = useState('');\n const { animationTimeout } = useAnimationTimeout();\n const [value, setValue] = useState('');\n const { isMobile } = useBreakpoint();\n\n const inputRefCallback: RefCallback<HTMLInputElement> = useCallback((node) => {\n if (!node) {\n setValue('');\n setContentFilterQuery('');\n }\n }, []);\n\n const ref = useMultipleRefs(inputRef, inputRefCallback);\n\n useEffect(() => {\n setValue('');\n setContentFilterQuery('');\n }, [isMobile]);\n\n const debouncedContentFilterQuery = useDebounce(\n useCallback((newValue: string) => {\n setContentFilterQuery(newValue);\n }, []),\n animationTimeout\n );\n\n const handleChangeInput = useCallback(\n (newValue: string) => {\n debouncedContentFilterQuery(newValue);\n setValue(newValue);\n },\n [debouncedContentFilterQuery]\n );\n\n const setInputValue = useCallback(\n (newValue: string) => {\n setValue(newValue);\n },\n [setValue]\n );\n\n const renderInput = useCallback(\n (props?: SearchInputPropsWithRef) => {\n return <WrapperInput {...props} value={value} onChange={handleChangeInput} parentRef={ref} />;\n },\n [ref, value, handleChangeInput]\n );\n\n return { contentFilterQuery, setInputValue, handleChangeInput, renderInput };\n};\n"],"names":[],"mappings":";;;;;;;;AAeA,MAAM,YAAA,GAAe,WAAgD,CAAC,EAAE,WAAW,GAAG,KAAA,IAAS,GAAQ,KAAA;AACnG,EAAM,MAAA,QAAA,GAAW,eAAgB,CAAA,GAAA,EAAK,SAAS,CAAA,CAAA;AAC/C,EAAA,2BAAQ,WAAa,EAAA,EAAA,GAAG,OAAO,GAAK,EAAA,QAAA,EAAU,WAAQ,4BAA6B,EAAA,CAAA,CAAA;AACvF,CAAC,CAAA,CAAA;AAED,YAAA,CAAa,WAAc,GAAA,mBAAA,CAAA;AAWd,MAAA,cAAA,GAAqC,CAAC,QAAa,KAAA;AAC5D,EAAA,MAAM,CAAC,kBAAA,EAAoB,qBAAqB,CAAA,GAAI,SAAS,EAAE,CAAA,CAAA;AAC/D,EAAM,MAAA,EAAE,gBAAiB,EAAA,GAAI,mBAAoB,EAAA,CAAA;AACjD,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAI,SAAS,EAAE,CAAA,CAAA;AACrC,EAAM,MAAA,EAAE,QAAS,EAAA,GAAI,aAAc,EAAA,CAAA;AAEnC,EAAM,MAAA,gBAAA,GAAkD,WAAY,CAAA,CAAC,IAAS,KAAA;AAC1E,IAAA,IAAI,CAAC,IAAM,EAAA;AACP,MAAA,QAAA,CAAS,EAAE,CAAA,CAAA;AACX,MAAA,qBAAA,CAAsB,EAAE,CAAA,CAAA;AAAA,KAC5B;AAAA,GACJ,EAAG,EAAE,CAAA,CAAA;AAEL,EAAM,MAAA,GAAA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} from '@hh.ru/magritte-ui-tree-selector/collection/types';
2
- import ImmutableSelectionStrategy from '@hh.ru/magritte-ui-tree-selector/strategy/immutableSelectionStrategy';
3
- import { TreeSelectorProps } from '@hh.ru/magritte-ui-tree-selector/types';
1
+ import { AdditionalDefault } from './collection/types';
2
+ import ImmutableSelectionStrategy from './strategy/immutableSelectionStrategy';
3
+ import { TreeSelectorProps } from './types';
4
4
  type UseSelectedHookProps<Additional extends AdditionalDefault> = {
5
5
  strategy: ImmutableSelectionStrategy<Additional>;
6
6
  } & Pick<TreeSelectorProps<Additional>, 'value' | 'onChange' | 'collapseToParentId' | 'maxSelected' | 'collection'>;
package/useSelected.js CHANGED
@@ -3,38 +3,43 @@ import { useCallback, useState } from 'react';
3
3
  import { getIdsWithNoParentsInSameList } from './collection/treeCollectionHelper.js';
4
4
  import './collection/treeCollection.js';
5
5
 
6
- const useSelected = ({ value, maxSelected, strategy, collection, collapseToParentId, onChange, }) => {
7
- const getSelectedFromValue = useCallback(() => {
8
- let newValue = value.slice();
9
- if (maxSelected && newValue && newValue.length > maxSelected) {
10
- newValue = newValue.slice(0, maxSelected);
11
- }
12
- return strategy.add([], newValue);
13
- }, [value, maxSelected, strategy]);
14
- const [selected, setSelectedState] = useState(getSelectedFromValue);
15
- // синхронизируем selected из value прямо в рендере, а не в useEffect: ref-колбэки и layout-фаза коммита
16
- // выполняются раньше пассивных эффектов, и отложенная синхронизация давала им устаревший selected
17
- // (например, сброс expanded при маунте дерева в том же коммите, где изменился value)
18
- const [prevGetSelectedFromValue, setPrevGetSelectedFromValue] = useState(() => getSelectedFromValue);
19
- if (prevGetSelectedFromValue !== getSelectedFromValue) {
20
- setPrevGetSelectedFromValue(() => getSelectedFromValue);
21
- setSelectedState(getSelectedFromValue());
6
+ const useSelected = ({
7
+ value,
8
+ maxSelected,
9
+ strategy,
10
+ collection,
11
+ collapseToParentId,
12
+ onChange
13
+ }) => {
14
+ const getSelectedFromValue = useCallback(() => {
15
+ let newValue = value.slice();
16
+ if (maxSelected && newValue && newValue.length > maxSelected) {
17
+ newValue = newValue.slice(0, maxSelected);
22
18
  }
23
- const setSelected = useCallback((id, isSelected) => {
24
- const ids = [id];
25
- const updatedSelected = isSelected ? strategy.add(selected, ids) : strategy.remove(selected, ids);
26
- const filteredUpdatedSelected = collapseToParentId
27
- ? getIdsWithNoParentsInSameList(collection, updatedSelected)
28
- : updatedSelected;
29
- if (maxSelected && filteredUpdatedSelected.length > maxSelected) {
30
- filteredUpdatedSelected.length = maxSelected;
31
- }
32
- onChange(filteredUpdatedSelected, id, isSelected);
33
- }, [strategy, selected, collection, collapseToParentId, onChange, maxSelected]);
34
- return {
35
- selected,
36
- setSelected,
37
- };
19
+ return strategy.add([], newValue);
20
+ }, [value, maxSelected, strategy]);
21
+ const [selected, setSelectedState] = useState(getSelectedFromValue);
22
+ const [prevGetSelectedFromValue, setPrevGetSelectedFromValue] = useState(() => getSelectedFromValue);
23
+ if (prevGetSelectedFromValue !== getSelectedFromValue) {
24
+ setPrevGetSelectedFromValue(() => getSelectedFromValue);
25
+ setSelectedState(getSelectedFromValue());
26
+ }
27
+ const setSelected = useCallback(
28
+ (id, isSelected) => {
29
+ const ids = [id];
30
+ const updatedSelected = isSelected ? strategy.add(selected, ids) : strategy.remove(selected, ids);
31
+ const filteredUpdatedSelected = collapseToParentId ? getIdsWithNoParentsInSameList(collection, updatedSelected) : updatedSelected;
32
+ if (maxSelected && filteredUpdatedSelected.length > maxSelected) {
33
+ filteredUpdatedSelected.length = maxSelected;
34
+ }
35
+ onChange(filteredUpdatedSelected, id, isSelected);
36
+ },
37
+ [strategy, selected, collection, collapseToParentId, onChange, maxSelected]
38
+ );
39
+ return {
40
+ selected,
41
+ setSelected
42
+ };
38
43
  };
39
44
 
40
45
  export { useSelected };