@applica-software-guru/react-admin 1.2.121 → 1.2.123

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applica-software-guru/react-admin",
3
- "version": "1.2.121",
3
+ "version": "1.2.123",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",
@@ -3,7 +3,7 @@ import { matchPath, useLocation } from 'react-router';
3
3
 
4
4
  import { IItem } from './types';
5
5
  import _ from 'lodash';
6
- import { getItemsIds } from './utils';
6
+ import { getChildren, getItemsIds, getLevel } from './utils';
7
7
 
8
8
  enum ActionType {
9
9
  SET_FORM_ROOT_PATH = 'setFormRootPath',
@@ -145,7 +145,15 @@ function Provider(props: IProviderProps) {
145
145
 
146
146
  useEffect(() => {
147
147
  if (syncWithLocation && formRootPath !== undefined) {
148
- const locationItem = pathname.replace(`${formRootPath}/`, '');
148
+ let locationItem = pathname.replace(formRootPath, '').replace(new RegExp(/^\/?/), '');
149
+ if (_.isEmpty(locationItem)) {
150
+ locationItem =
151
+ _.chain(items)
152
+ .filter((item) => getChildren(item.id, items).length === 0)
153
+ .orderBy([(item) => getLevel(item.id), (item) => item.index])
154
+ .first()
155
+ .value()?.id ?? undefined;
156
+ }
149
157
  dispatch({
150
158
  type: ActionType.SET_ACTIVE_ITEM,
151
159
  payload: locationItem
@@ -1,8 +1,7 @@
1
- import _ from 'lodash';
2
1
  import { useCallback, useMemo } from 'react';
3
2
  import { useActiveItem, useFormRootPath, useItems } from './Provider';
4
3
  import { IItem } from './types';
5
- import { getLevel, isChild } from './utils';
4
+ import { getChildren, isChild } from './utils';
6
5
  import { useNavigate } from 'react-router';
7
6
 
8
7
  function useIsActive(id: string): boolean {
@@ -12,13 +11,7 @@ function useIsActive(id: string): boolean {
12
11
 
13
12
  function useChildren(id?: string): Array<IItem> {
14
13
  const items = useItems(),
15
- level = id !== undefined ? getLevel(id) : 0,
16
- children = useMemo(() => {
17
- return _.chain(items)
18
- .filter((item) => (id !== undefined ? isChild(id, item.id) : true))
19
- .filter((item) => getLevel(item.id) === level + 1)
20
- .value();
21
- }, [items, level, id]);
14
+ children = useMemo(() => getChildren(id, items), [items, id]);
22
15
 
23
16
  return children;
24
17
  }
@@ -19,4 +19,14 @@ function getLevel(id: string): number {
19
19
  return match === null ? 0 : match.length;
20
20
  }
21
21
 
22
- export { getId, isChild, getItemsIds, getLevel };
22
+ function getChildren(id: string | undefined, items: Array<IItem>) {
23
+ const level = id !== undefined ? getLevel(id) : 0,
24
+ children = _.chain(items)
25
+ .filter((item) => (id !== undefined ? isChild(id, item.id) : true))
26
+ .filter((item) => getLevel(item.id) === level + 1)
27
+ .value();
28
+
29
+ return children;
30
+ }
31
+
32
+ export { getId, isChild, getItemsIds, getLevel, getChildren };