@etsoo/react 1.3.99 → 1.4.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.
@@ -6,7 +6,7 @@ export interface DnDListProps<D extends {}, E extends React.ElementType> {
6
6
  /**
7
7
  * Item renderer
8
8
  */
9
- children: (item: D, index: number, deleteItem: (index: number) => void) => React.ReactNode;
9
+ children: (item: D, index: number, deleteItem: (index: number) => void, editItem: (newItem: D, indexOrLabel: number | string) => void) => React.ReactNode;
10
10
  /**
11
11
  * List container component
12
12
  * https://javascript.plainenglish.io/building-a-polymorphic-component-in-react-and-typescript-d9f236950af4
package/lib/mu/DnDList.js CHANGED
@@ -39,6 +39,21 @@ export function DnDList(props) {
39
39
  setItems(newItems);
40
40
  return true;
41
41
  };
42
+ // Edit item
43
+ const editItem = (newItem, indexOrLabel) => {
44
+ const index = typeof indexOrLabel === 'number'
45
+ ? indexOrLabel
46
+ : items.findIndex((item) => DataTypes.convert(item[labelField], 'string') ==
47
+ indexOrLabel);
48
+ if (index === -1)
49
+ return;
50
+ // Clone
51
+ const newItems = [...items];
52
+ // Remove the item
53
+ newItems.splice(index, 1, newItem);
54
+ // Update the state
55
+ setItems(newItems);
56
+ };
42
57
  // Add items
43
58
  const addItems = (inputItems) => {
44
59
  // Clone
@@ -83,7 +98,7 @@ export function DnDList(props) {
83
98
  ...provided
84
99
  .draggableProps
85
100
  .style
86
- } }, children(item, index, deleteItem)))));
101
+ } }, children(item, index, deleteItem, editItem)))));
87
102
  }),
88
103
  provided.placeholder))))),
89
104
  sideRenderer && sideRenderer(false, addItem, addItems)));
@@ -3,7 +3,7 @@ import React from 'react';
3
3
  import { Labels } from '../../app/Labels';
4
4
  import { MUGlobal } from '../MUGlobal';
5
5
  import { CommonPage, CommonPageScrollContainer } from './CommonPage';
6
- import EditIcon from '@mui/icons-material/Edit';
6
+ import SaveIcon from '@mui/icons-material/Save';
7
7
  /**
8
8
  * Edit page
9
9
  * @param props Props
@@ -21,5 +21,5 @@ export function EditPage(props) {
21
21
  bottom: (theme) => MUGlobal.updateWithTheme(paddings, theme.spacing),
22
22
  paddingTop: paddings
23
23
  } },
24
- React.createElement(Button, { variant: "contained", type: "submit", fullWidth: true, startIcon: React.createElement(EditIcon, null) }, labels.save)))));
24
+ React.createElement(Button, { variant: "contained", type: "submit", fullWidth: true, startIcon: React.createElement(SaveIcon, null) }, labels.save)))));
25
25
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.3.99",
3
+ "version": "1.4.0",
4
4
  "description": "TypeScript ReactJs framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -17,7 +17,8 @@ export interface DnDListProps<D extends {}, E extends React.ElementType> {
17
17
  children: (
18
18
  item: D,
19
19
  index: number,
20
- deleteItem: (index: number) => void
20
+ deleteItem: (index: number) => void,
21
+ editItem: (newItem: D, indexOrLabel: number | string) => void
21
22
  ) => React.ReactNode;
22
23
 
23
24
  /**
@@ -128,6 +129,28 @@ export function DnDList<
128
129
  return true;
129
130
  };
130
131
 
132
+ // Edit item
133
+ const editItem = (newItem: D, indexOrLabel: number | string) => {
134
+ const index =
135
+ typeof indexOrLabel === 'number'
136
+ ? indexOrLabel
137
+ : items.findIndex(
138
+ (item) =>
139
+ DataTypes.convert(item[labelField], 'string') ==
140
+ indexOrLabel
141
+ );
142
+ if (index === -1) return;
143
+
144
+ // Clone
145
+ const newItems = [...items];
146
+
147
+ // Remove the item
148
+ newItems.splice(index, 1, newItem);
149
+
150
+ // Update the state
151
+ setItems(newItems);
152
+ };
153
+
131
154
  // Add items
132
155
  const addItems = (inputItems: D[]) => {
133
156
  // Clone
@@ -212,7 +235,8 @@ export function DnDList<
212
235
  {children(
213
236
  item,
214
237
  index,
215
- deleteItem
238
+ deleteItem,
239
+ editItem
216
240
  )}
217
241
  </div>
218
242
  )}
@@ -6,7 +6,7 @@ import { Labels } from '../../app/Labels';
6
6
  import { MUGlobal } from '../MUGlobal';
7
7
  import { CommonPage, CommonPageScrollContainer } from './CommonPage';
8
8
  import { CommonPageProps } from './CommonPageProps';
9
- import EditIcon from '@mui/icons-material/Edit';
9
+ import SaveIcon from '@mui/icons-material/Save';
10
10
 
11
11
  /**
12
12
  * Edit page router props, include 'id' (/:id) query parameter
@@ -70,7 +70,7 @@ export function EditPage(props: EditPageProps) {
70
70
  variant="contained"
71
71
  type="submit"
72
72
  fullWidth
73
- startIcon={<EditIcon />}
73
+ startIcon={<SaveIcon />}
74
74
  >
75
75
  {labels.save}
76
76
  </Button>