@applica-software-guru/react-admin 1.2.120 → 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.120",
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',
@@ -62,8 +62,12 @@ function reducer(state: IState, action: IAction): IState {
62
62
  return _.includes(getItemsIds(state.items), payload) ? _.extend(newState, { activeItem: payload }) : newState;
63
63
  case ActionType.ADD_ITEM: {
64
64
  const id = payload.id,
65
- items = _.reject(newState.items, (item: IItem) => item.id === id);
66
- items.push(payload);
65
+ items = _.chain(newState.items)
66
+ .clone()
67
+ .reject((item: IItem) => item.id === id)
68
+ .concat([payload])
69
+ .orderBy((item) => item.index)
70
+ .value();
67
71
  _.extend(newState, { items: items });
68
72
  if (newState.activeItem === undefined) {
69
73
  _.extend(newState, { activeItem: payload.id });
@@ -72,7 +76,10 @@ function reducer(state: IState, action: IAction): IState {
72
76
  }
73
77
  case ActionType.REMOVE_ITEM: {
74
78
  const id = _.isString(payload) ? payload : payload.id,
75
- items = _.reject(newState.items, (item: IItem) => item.id === id);
79
+ items = _.chain(newState.items)
80
+ .clone()
81
+ .reject((item: IItem) => item.id === id)
82
+ .value();
76
83
  _.extend(newState, { items: items });
77
84
  if (newState.activeItem === id) {
78
85
  _.extend(newState, { activeItem: _.first(items)?.id ?? undefined });
@@ -138,7 +145,15 @@ function Provider(props: IProviderProps) {
138
145
 
139
146
  useEffect(() => {
140
147
  if (syncWithLocation && formRootPath !== undefined) {
141
- 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
+ }
142
157
  dispatch({
143
158
  type: ActionType.SET_ACTIVE_ITEM,
144
159
  payload: locationItem
@@ -7,12 +7,12 @@ import { IItem } from './types';
7
7
  import { useIsActive } from './hooks';
8
8
  import { getId } from './utils';
9
9
 
10
- type IBaseItemProps = React.PropsWithChildren<Optional<IItem, 'id'>>;
10
+ type IBaseItemProps = React.PropsWithChildren<Optional<IItem, 'id' | 'index'>>;
11
11
  type ITabProps = IBaseItemProps;
12
12
  type IGroupProps = IBaseItemProps;
13
13
 
14
14
  function BaseItem(props: IBaseItemProps) {
15
- const { label, icon, badge } = props,
15
+ const { label, icon, badge, index = 0 } = props,
16
16
  id = getId(props),
17
17
  addItem = useAddItem(),
18
18
  removeItem = useRemoveItem(),
@@ -21,6 +21,7 @@ function BaseItem(props: IBaseItemProps) {
21
21
  useEffect(() => {
22
22
  addItem({
23
23
  id: id,
24
+ index: index,
24
25
  label: label,
25
26
  icon: icon,
26
27
  badge: badge
@@ -28,7 +29,7 @@ function BaseItem(props: IBaseItemProps) {
28
29
  return () => {
29
30
  removeItem(id);
30
31
  };
31
- }, [addItem, removeItem, label, icon, id, badge]);
32
+ }, [addItem, removeItem, label, icon, id, badge, index]);
32
33
 
33
34
  /* All tabs are rendered (not only the one in focus), to allow validation
34
35
  on tabs not in focus. The tabs receive a `hidden` property, which they'll
@@ -57,8 +58,12 @@ function Tab(props: ITabProps) {
57
58
  function useBaseItemChildren(props: React.PropsWithChildren): Array<React.FunctionComponentElement<IBaseItemProps>> {
58
59
  const { children } = props,
59
60
  result = useMemo<Array<React.FunctionComponentElement<IBaseItemProps>>>(
60
- //@ts-ignore
61
- () => _.filter(Children.toArray(children), (Child) => isValidElement(Child) && (Child?.type === Tab || Child?.type === Group)),
61
+ () =>
62
+ //@ts-ignore
63
+ _.chain(Children.toArray(children))
64
+ .filter((Child) => isValidElement(Child) && (Child?.type === Tab || Child?.type === Group))
65
+ .map((Child: React.FunctionComponentElement<IBaseItemProps>, index) => cloneElement(Child, { index: index }))
66
+ .value(),
62
67
  [children]
63
68
  );
64
69
  return result;
@@ -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
  }
@@ -8,6 +8,7 @@ type Disposition = {
8
8
 
9
9
  type IItem = {
10
10
  id: string;
11
+ index: number;
11
12
  label: string;
12
13
  icon?: React.ReactNode;
13
14
  badge?: {
@@ -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 };