@etsoo/react 1.5.72 → 1.5.75

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.
@@ -6,6 +6,17 @@ const updateFunc = (value: number) => `${value * 8}px`;
6
6
  // Arrange
7
7
  const paddings = { sx: 2, sm: 3, key: 'a' };
8
8
 
9
+ test('getMenuItem tests', () => {
10
+ // Assert
11
+ expect(
12
+ MUGlobal.getMenuItem('/user/add', '/user/all').selected
13
+ ).toBeTruthy();
14
+ expect(MUGlobal.getMenuItem('/user/add', '/user/*').selected).toBeTruthy();
15
+ expect(
16
+ MUGlobal.getMenuItem('/user/add', '/user/edit').selected
17
+ ).toBeFalsy();
18
+ });
19
+
9
20
  test('half tests', () => {
10
21
  // Act
11
22
  const result = MUGlobal.half(paddings);
@@ -1,8 +1,21 @@
1
1
  import { UniqueIdentifier } from '@dnd-kit/core';
2
2
  import { DataTypes } from '@etsoo/shared';
3
+ import { Theme } from '@mui/material';
3
4
  import React, { CSSProperties } from 'react';
4
5
  /**
5
- * Scroller list forward ref
6
+ * DnD item default style
7
+ * @param index Item index
8
+ * @param isDragging Is dragging
9
+ * @param theme Theme
10
+ * @returns Style
11
+ */
12
+ export declare const DnDItemStyle: (index: number, isDragging: boolean, theme: Theme) => {
13
+ padding: string;
14
+ zIndex: string | number;
15
+ background: string;
16
+ };
17
+ /**
18
+ * DnD list forward ref
6
19
  */
7
20
  export interface DnDListRef<D extends {}> {
8
21
  /**
package/lib/mu/DnDList.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { DndContext } from '@dnd-kit/core';
2
2
  import { SortableContext, useSortable, verticalListSortingStrategy } from '@dnd-kit/sortable';
3
3
  import { CSS } from '@dnd-kit/utilities';
4
+ import { useTheme } from '@mui/material';
4
5
  import React from 'react';
5
6
  function SortableItem(props) {
6
7
  // Destruct
@@ -23,6 +24,22 @@ function SortableItem(props) {
23
24
  };
24
25
  return itemRenderer(nodeRef, actionNodeRef);
25
26
  }
27
+ /**
28
+ * DnD item default style
29
+ * @param index Item index
30
+ * @param isDragging Is dragging
31
+ * @param theme Theme
32
+ * @returns Style
33
+ */
34
+ export const DnDItemStyle = (index, isDragging, theme) => ({
35
+ padding: theme.spacing(1),
36
+ zIndex: isDragging ? 1 : 'auto',
37
+ background: isDragging
38
+ ? theme.palette.primary.light
39
+ : index % 2 === 0
40
+ ? theme.palette.grey[100]
41
+ : theme.palette.grey[50]
42
+ });
26
43
  /**
27
44
  * DnD (Drag and Drop) sortable list
28
45
  * @param props Props
@@ -30,7 +47,13 @@ function SortableItem(props) {
30
47
  */
31
48
  export function DnDList(props) {
32
49
  // Destruct
33
- const { getItemStyle, keyField, itemRenderer, labelField, mRef, onChange, onDragEnd } = props;
50
+ const { keyField, itemRenderer, labelField, mRef, onChange, onDragEnd } = props;
51
+ let getItemStyle = props.getItemStyle;
52
+ if (getItemStyle == null) {
53
+ // Theme
54
+ const theme = useTheme();
55
+ getItemStyle = (index, isDragging) => DnDItemStyle(index, isDragging, theme);
56
+ }
34
57
  // States
35
58
  const [items, setItems] = React.useState([]);
36
59
  const [activeId, setActiveId] = React.useState();
@@ -124,10 +147,7 @@ export function DnDList(props) {
124
147
  }, [props.items]);
125
148
  return (React.createElement(DndContext, { onDragStart: handleDragStart, onDragEnd: handleDragEnd },
126
149
  React.createElement(SortableContext, { items: items, strategy: verticalListSortingStrategy }, items.map((item, index) => {
127
- var id = item[keyField];
128
- var itemStyle = getItemStyle == null
129
- ? undefined
130
- : getItemStyle(index, id === activeId);
131
- return (React.createElement(SortableItem, { id: id, key: id, style: itemStyle, itemRenderer: (nodeRef, actionNodeRef) => itemRenderer(item, index, nodeRef, actionNodeRef) }));
150
+ const id = item[keyField];
151
+ return (React.createElement(SortableItem, { id: id, key: id, style: getItemStyle(index, id === activeId), itemRenderer: (nodeRef, actionNodeRef) => itemRenderer(item, index, nodeRef, actionNodeRef) }));
132
152
  }))));
133
153
  }
@@ -1,5 +1,5 @@
1
1
  /// <reference types="react" />
2
- import { Theme } from '@mui/material';
2
+ import { ListItemButtonProps, Theme } from '@mui/material';
3
3
  /**
4
4
  * Mouse event handler with data
5
5
  */
@@ -43,6 +43,13 @@ export declare class MUGlobal {
43
43
  xs: number;
44
44
  sm: number;
45
45
  };
46
+ /**
47
+ * Get menu item props
48
+ * @param path Current path
49
+ * @param href Item's href
50
+ * @returns Props
51
+ */
52
+ static getMenuItem(path: string, href: string): ListItemButtonProps<"div", {}>;
46
53
  /**
47
54
  * Update object number properties with half of it
48
55
  * @param input Input object
@@ -1,8 +1,41 @@
1
1
  import { NumberUtils } from '@etsoo/shared';
2
+ import { RLink } from './RLink';
2
3
  /**
3
4
  * MUGlobal for global configurations
4
5
  */
5
6
  export class MUGlobal {
7
+ /**
8
+ * Get menu item props
9
+ * @param path Current path
10
+ * @param href Item's href
11
+ * @returns Props
12
+ */
13
+ static getMenuItem(path, href) {
14
+ let selected = false;
15
+ if (path === href) {
16
+ // Exact match, most common case
17
+ selected = true;
18
+ }
19
+ else if (href.endsWith('*')) {
20
+ href = href.slice(0, -1);
21
+ selected = path.startsWith(href);
22
+ }
23
+ else if (href.endsWith('/all')) {
24
+ selected = path.startsWith(href.slice(0, -3));
25
+ }
26
+ return {
27
+ component: RLink,
28
+ selected,
29
+ href,
30
+ sx: {
31
+ ...(selected && {
32
+ '.MuiListItemIcon-root': {
33
+ color: (theme) => theme.palette.primary.main
34
+ }
35
+ })
36
+ }
37
+ };
38
+ }
6
39
  /**
7
40
  * Update object number properties with half of it
8
41
  * @param input Input object
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.5.72",
3
+ "version": "1.5.75",
4
4
  "description": "TypeScript ReactJs framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -51,7 +51,7 @@
51
51
  "@emotion/css": "^11.10.0",
52
52
  "@emotion/react": "^11.10.0",
53
53
  "@emotion/styled": "^11.10.0",
54
- "@etsoo/appscript": "^1.2.77",
54
+ "@etsoo/appscript": "^1.2.78",
55
55
  "@etsoo/notificationbase": "^1.1.6",
56
56
  "@etsoo/shared": "^1.1.45",
57
57
  "@mui/icons-material": "^5.8.4",
@@ -75,14 +75,14 @@
75
75
  },
76
76
  "devDependencies": {
77
77
  "@babel/cli": "^7.18.10",
78
- "@babel/core": "^7.18.10",
78
+ "@babel/core": "^7.18.13",
79
79
  "@babel/plugin-transform-runtime": "^7.18.10",
80
80
  "@babel/preset-env": "^7.18.10",
81
81
  "@babel/runtime-corejs3": "^7.18.9",
82
82
  "@types/jest": "^28.1.7",
83
83
  "@types/react-test-renderer": "^18.0.0",
84
- "@typescript-eslint/eslint-plugin": "^5.33.1",
85
- "@typescript-eslint/parser": "^5.33.1",
84
+ "@typescript-eslint/eslint-plugin": "^5.34.0",
85
+ "@typescript-eslint/parser": "^5.34.0",
86
86
  "eslint": "^8.22.0",
87
87
  "eslint-config-airbnb-base": "^15.0.0",
88
88
  "eslint-plugin-import": "^2.26.0",
@@ -11,6 +11,7 @@ import {
11
11
  } from '@dnd-kit/sortable';
12
12
  import { CSS } from '@dnd-kit/utilities';
13
13
  import { DataTypes } from '@etsoo/shared';
14
+ import { Theme, useTheme } from '@mui/material';
14
15
  import React, { CSSProperties } from 'react';
15
16
 
16
17
  function SortableItem(props: {
@@ -55,7 +56,28 @@ function SortableItem(props: {
55
56
  }
56
57
 
57
58
  /**
58
- * Scroller list forward ref
59
+ * DnD item default style
60
+ * @param index Item index
61
+ * @param isDragging Is dragging
62
+ * @param theme Theme
63
+ * @returns Style
64
+ */
65
+ export const DnDItemStyle = (
66
+ index: number,
67
+ isDragging: boolean,
68
+ theme: Theme
69
+ ) => ({
70
+ padding: theme.spacing(1),
71
+ zIndex: isDragging ? 1 : 'auto',
72
+ background: isDragging
73
+ ? theme.palette.primary.light
74
+ : index % 2 === 0
75
+ ? theme.palette.grey[100]
76
+ : theme.palette.grey[50]
77
+ });
78
+
79
+ /**
80
+ * DnD list forward ref
59
81
  */
60
82
  export interface DnDListRef<D extends {}> {
61
83
  /**
@@ -147,15 +169,16 @@ export function DnDList<
147
169
  >
148
170
  >(props: DnDListPros<D, K>) {
149
171
  // Destruct
150
- const {
151
- getItemStyle,
152
- keyField,
153
- itemRenderer,
154
- labelField,
155
- mRef,
156
- onChange,
157
- onDragEnd
158
- } = props;
172
+ const { keyField, itemRenderer, labelField, mRef, onChange, onDragEnd } =
173
+ props;
174
+
175
+ let getItemStyle = props.getItemStyle;
176
+ if (getItemStyle == null) {
177
+ // Theme
178
+ const theme = useTheme();
179
+ getItemStyle = (index, isDragging) =>
180
+ DnDItemStyle(index, isDragging, theme);
181
+ }
159
182
 
160
183
  // States
161
184
  const [items, setItems] = React.useState<D[]>([]);
@@ -298,16 +321,12 @@ export function DnDList<
298
321
  strategy={verticalListSortingStrategy}
299
322
  >
300
323
  {items.map((item, index) => {
301
- var id = item[keyField] as unknown as UniqueIdentifier;
302
- var itemStyle =
303
- getItemStyle == null
304
- ? undefined
305
- : getItemStyle(index, id === activeId);
324
+ const id = item[keyField] as unknown as UniqueIdentifier;
306
325
  return (
307
326
  <SortableItem
308
327
  id={id}
309
328
  key={id}
310
- style={itemStyle}
329
+ style={getItemStyle!(index, id === activeId)}
311
330
  itemRenderer={(nodeRef, actionNodeRef) =>
312
331
  itemRenderer(
313
332
  item,
@@ -1,5 +1,6 @@
1
1
  import { NumberUtils } from '@etsoo/shared';
2
- import { Breakpoint, Theme } from '@mui/material';
2
+ import { Breakpoint, ListItemButtonProps, Theme } from '@mui/material';
3
+ import { RLink } from './RLink';
3
4
 
4
5
  /**
5
6
  * Mouse event handler with data
@@ -53,6 +54,39 @@ export class MUGlobal {
53
54
  */
54
55
  static pagePaddings = { xs: 2, sm: 3 };
55
56
 
57
+ /**
58
+ * Get menu item props
59
+ * @param path Current path
60
+ * @param href Item's href
61
+ * @returns Props
62
+ */
63
+ static getMenuItem(path: string, href: string) {
64
+ let selected = false;
65
+
66
+ if (path === href) {
67
+ // Exact match, most common case
68
+ selected = true;
69
+ } else if (href.endsWith('*')) {
70
+ href = href.slice(0, -1);
71
+ selected = path.startsWith(href);
72
+ } else if (href.endsWith('/all')) {
73
+ selected = path.startsWith(href.slice(0, -3));
74
+ }
75
+
76
+ return {
77
+ component: RLink,
78
+ selected,
79
+ href,
80
+ sx: {
81
+ ...(selected && {
82
+ '.MuiListItemIcon-root': {
83
+ color: (theme) => theme.palette.primary.main
84
+ }
85
+ })
86
+ }
87
+ } as ListItemButtonProps;
88
+ }
89
+
56
90
  /**
57
91
  * Update object number properties with half of it
58
92
  * @param input Input object