@etsoo/materialui 1.1.2 → 1.1.4
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/lib/ItemList.d.ts +5 -5
- package/lib/ItemList.js +12 -12
- package/package.json +13 -13
- package/src/ItemList.tsx +203 -210
package/lib/ItemList.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import React from
|
|
2
|
-
import { DataTypes, IdDefaultType, LabelDefaultType, ListType } from
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { DataTypes, IdDefaultType, LabelDefaultType, ListType } from "@etsoo/shared";
|
|
3
3
|
/**
|
|
4
4
|
* Item list properties
|
|
5
5
|
*/
|
|
@@ -35,7 +35,7 @@ export interface ItemListProps<T extends object, D extends DataTypes.Keys<T>, L
|
|
|
35
35
|
/**
|
|
36
36
|
* Button color
|
|
37
37
|
*/
|
|
38
|
-
color?:
|
|
38
|
+
color?: "inherit" | "primary" | "secondary";
|
|
39
39
|
/**
|
|
40
40
|
* Close event
|
|
41
41
|
*/
|
|
@@ -47,7 +47,7 @@ export interface ItemListProps<T extends object, D extends DataTypes.Keys<T>, L
|
|
|
47
47
|
/**
|
|
48
48
|
* Button size
|
|
49
49
|
*/
|
|
50
|
-
size?:
|
|
50
|
+
size?: "small" | "medium" | "large";
|
|
51
51
|
/**
|
|
52
52
|
* Title
|
|
53
53
|
*/
|
|
@@ -59,7 +59,7 @@ export interface ItemListProps<T extends object, D extends DataTypes.Keys<T>, L
|
|
|
59
59
|
/**
|
|
60
60
|
* Button variant
|
|
61
61
|
*/
|
|
62
|
-
variant?:
|
|
62
|
+
variant?: "text" | "outlined" | "contained";
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
65
65
|
* Item list component
|
package/lib/ItemList.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import React from
|
|
2
|
-
import { Dialog, DialogTitle, List, ListItemText, DialogContent, Button, ListItemButton } from
|
|
3
|
-
import { DataTypes } from
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Dialog, DialogTitle, List, ListItemText, DialogContent, Button, ListItemButton, } from "@mui/material";
|
|
3
|
+
import { DataTypes, } from "@etsoo/shared";
|
|
4
4
|
/**
|
|
5
5
|
* Item list component
|
|
6
6
|
* @param props Properties
|
|
7
7
|
*/
|
|
8
8
|
export function ItemList(props) {
|
|
9
9
|
// properties destructure
|
|
10
|
-
const { buttonLabel, className, color =
|
|
10
|
+
const { buttonLabel, className, color = "primary", keepClick = false, items, idField = "id", labelField = "label", minWidth, icon, onClose, selectedValue, size = "medium", title, variant = "outlined", } = props;
|
|
11
11
|
// Get label
|
|
12
12
|
const getLabel = (item) => {
|
|
13
13
|
var _a;
|
|
14
|
-
if (typeof labelField ===
|
|
14
|
+
if (typeof labelField === "function") {
|
|
15
15
|
return labelField(item);
|
|
16
16
|
}
|
|
17
17
|
else {
|
|
18
|
-
return (_a = DataTypes.convert(item[labelField],
|
|
18
|
+
return (_a = DataTypes.convert(item[labelField], "string")) !== null && _a !== void 0 ? _a : "";
|
|
19
19
|
}
|
|
20
20
|
};
|
|
21
21
|
// Dialog open or not state
|
|
@@ -46,8 +46,10 @@ export function ItemList(props) {
|
|
|
46
46
|
};
|
|
47
47
|
// Close item handler
|
|
48
48
|
const closeItemHandler = (item) => {
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
if (!keepClick) {
|
|
50
|
+
// Update the current item
|
|
51
|
+
setCurrentItem(item);
|
|
52
|
+
}
|
|
51
53
|
// Close the dialog
|
|
52
54
|
setOpen(false);
|
|
53
55
|
// Emit close event
|
|
@@ -62,10 +64,8 @@ export function ItemList(props) {
|
|
|
62
64
|
React.createElement(DialogContent, { sx: { minWidth } },
|
|
63
65
|
React.createElement(List, null, items.map((item) => {
|
|
64
66
|
const id = item[idField];
|
|
65
|
-
return (React.createElement(ListItemButton, { key: id, disabled: id ===
|
|
66
|
-
(
|
|
67
|
-
? currentItem[idField]
|
|
68
|
-
: undefined) && !keepClick, onClick: () => closeItemHandler(item) },
|
|
67
|
+
return (React.createElement(ListItemButton, { key: id, disabled: id === (currentItem ? currentItem[idField] : undefined) &&
|
|
68
|
+
!keepClick, onClick: () => closeItemHandler(item) },
|
|
69
69
|
React.createElement(ListItemText, null, getLabel(item))));
|
|
70
70
|
}))))));
|
|
71
71
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/materialui",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "TypeScript Material-UI Implementation",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -45,17 +45,17 @@
|
|
|
45
45
|
},
|
|
46
46
|
"homepage": "https://github.com/ETSOO/ReactMU#readme",
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@dnd-kit/core": "^6.0.
|
|
49
|
-
"@dnd-kit/sortable": "^7.0.
|
|
48
|
+
"@dnd-kit/core": "^6.0.7",
|
|
49
|
+
"@dnd-kit/sortable": "^7.0.2",
|
|
50
50
|
"@emotion/css": "^11.10.5",
|
|
51
51
|
"@emotion/react": "^11.10.5",
|
|
52
52
|
"@emotion/styled": "^11.10.5",
|
|
53
|
-
"@etsoo/appscript": "^1.3.
|
|
54
|
-
"@etsoo/notificationbase": "^1.1.
|
|
55
|
-
"@etsoo/react": "^1.6.
|
|
56
|
-
"@etsoo/shared": "^1.1.
|
|
53
|
+
"@etsoo/appscript": "^1.3.56",
|
|
54
|
+
"@etsoo/notificationbase": "^1.1.21",
|
|
55
|
+
"@etsoo/react": "^1.6.39",
|
|
56
|
+
"@etsoo/shared": "^1.1.87",
|
|
57
57
|
"@mui/icons-material": "^5.11.0",
|
|
58
|
-
"@mui/material": "^5.11.
|
|
58
|
+
"@mui/material": "^5.11.4",
|
|
59
59
|
"@types/pica": "^9.0.1",
|
|
60
60
|
"@types/pulltorefreshjs": "^0.1.5",
|
|
61
61
|
"@types/react": "^18.0.26",
|
|
@@ -70,12 +70,12 @@
|
|
|
70
70
|
"react-dom": "^18.2.0",
|
|
71
71
|
"react-draggable": "^4.4.5",
|
|
72
72
|
"react-imask": "^6.4.3",
|
|
73
|
-
"react-router-dom": "^6.6.
|
|
73
|
+
"react-router-dom": "^6.6.2",
|
|
74
74
|
"react-window": "^1.8.8"
|
|
75
75
|
},
|
|
76
76
|
"devDependencies": {
|
|
77
77
|
"@babel/cli": "^7.20.7",
|
|
78
|
-
"@babel/core": "^7.20.
|
|
78
|
+
"@babel/core": "^7.20.12",
|
|
79
79
|
"@babel/plugin-transform-runtime": "^7.19.6",
|
|
80
80
|
"@babel/preset-env": "^7.20.2",
|
|
81
81
|
"@babel/preset-react": "^7.18.6",
|
|
@@ -83,9 +83,9 @@
|
|
|
83
83
|
"@babel/runtime-corejs3": "^7.20.7",
|
|
84
84
|
"@testing-library/jest-dom": "^5.16.5",
|
|
85
85
|
"@testing-library/react": "^13.4.0",
|
|
86
|
-
"@types/jest": "^29.2.
|
|
87
|
-
"@typescript-eslint/eslint-plugin": "^5.
|
|
88
|
-
"@typescript-eslint/parser": "^5.
|
|
86
|
+
"@types/jest": "^29.2.5",
|
|
87
|
+
"@typescript-eslint/eslint-plugin": "^5.48.1",
|
|
88
|
+
"@typescript-eslint/parser": "^5.48.1",
|
|
89
89
|
"jest": "^29.3.1",
|
|
90
90
|
"jest-environment-jsdom": "^29.3.1",
|
|
91
91
|
"typescript": "^4.9.4"
|
package/src/ItemList.tsx
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
import React from
|
|
1
|
+
import React from "react";
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from
|
|
3
|
+
Dialog,
|
|
4
|
+
DialogTitle,
|
|
5
|
+
List,
|
|
6
|
+
ListItemText,
|
|
7
|
+
DialogContent,
|
|
8
|
+
Button,
|
|
9
|
+
ListItemButton,
|
|
10
|
+
} from "@mui/material";
|
|
11
11
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} from
|
|
12
|
+
DataTypes,
|
|
13
|
+
IdDefaultType,
|
|
14
|
+
LabelDefaultType,
|
|
15
|
+
ListType,
|
|
16
|
+
} from "@etsoo/shared";
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Item list properties
|
|
20
20
|
*/
|
|
21
21
|
export interface ItemListProps<
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
T extends object,
|
|
23
|
+
D extends DataTypes.Keys<T>,
|
|
24
|
+
L extends DataTypes.Keys<T, string>
|
|
25
25
|
> {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Button label
|
|
28
|
+
*/
|
|
29
|
+
buttonLabel?: React.ReactNode;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Style class name
|
|
33
|
+
*/
|
|
34
|
+
className?: string;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Keep click for all items
|
|
38
|
+
*/
|
|
39
|
+
keepClick?: boolean;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Id field name
|
|
43
|
+
*/
|
|
44
|
+
idField?: D;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Label field name or callback
|
|
48
|
+
*/
|
|
49
|
+
labelField?: L | ((item: T) => string);
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Minimum width
|
|
53
|
+
*/
|
|
54
|
+
minWidth?: number | string;
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Button icon
|
|
58
|
+
*/
|
|
59
|
+
icon?: React.ReactNode;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Button color
|
|
63
|
+
*/
|
|
64
|
+
color?: "inherit" | "primary" | "secondary";
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Close event
|
|
68
|
+
*/
|
|
69
|
+
onClose?(item: T | undefined, changed: boolean): void;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Current selected language
|
|
73
|
+
*/
|
|
74
|
+
selectedValue?: T[D];
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Button size
|
|
78
|
+
*/
|
|
79
|
+
size?: "small" | "medium" | "large";
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Title
|
|
83
|
+
*/
|
|
84
|
+
title?: string;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Items
|
|
88
|
+
*/
|
|
89
|
+
items: T[];
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Button variant
|
|
93
|
+
*/
|
|
94
|
+
variant?: "text" | "outlined" | "contained";
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
/**
|
|
@@ -99,129 +99,122 @@ export interface ItemListProps<
|
|
|
99
99
|
* @param props Properties
|
|
100
100
|
*/
|
|
101
101
|
export function ItemList<
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
102
|
+
T extends object = ListType,
|
|
103
|
+
D extends DataTypes.Keys<T> = IdDefaultType<T>,
|
|
104
|
+
L extends DataTypes.Keys<T, string> = LabelDefaultType<T>
|
|
105
105
|
>(props: ItemListProps<T, D, L>) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
);
|
|
221
|
-
})}
|
|
222
|
-
</List>
|
|
223
|
-
</DialogContent>
|
|
224
|
-
</Dialog>
|
|
225
|
-
</>
|
|
226
|
-
);
|
|
106
|
+
// properties destructure
|
|
107
|
+
const {
|
|
108
|
+
buttonLabel,
|
|
109
|
+
className,
|
|
110
|
+
color = "primary",
|
|
111
|
+
keepClick = false,
|
|
112
|
+
items,
|
|
113
|
+
idField = "id" as D,
|
|
114
|
+
labelField = "label" as L,
|
|
115
|
+
minWidth,
|
|
116
|
+
icon,
|
|
117
|
+
onClose,
|
|
118
|
+
selectedValue,
|
|
119
|
+
size = "medium",
|
|
120
|
+
title,
|
|
121
|
+
variant = "outlined",
|
|
122
|
+
} = props;
|
|
123
|
+
|
|
124
|
+
// Get label
|
|
125
|
+
const getLabel = (item: T): string => {
|
|
126
|
+
if (typeof labelField === "function") {
|
|
127
|
+
return labelField(item);
|
|
128
|
+
} else {
|
|
129
|
+
return DataTypes.convert(item[labelField], "string") ?? "";
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// Dialog open or not state
|
|
134
|
+
const [open, setOpen] = React.useState(false);
|
|
135
|
+
|
|
136
|
+
// Default state
|
|
137
|
+
const defaultItem: T | undefined = items.find(
|
|
138
|
+
(item) => item[idField] === selectedValue
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
// Current item
|
|
142
|
+
const [currentItem, setCurrentItem] = React.useState(defaultItem);
|
|
143
|
+
|
|
144
|
+
// Click handler
|
|
145
|
+
const clickHandler = () => {
|
|
146
|
+
// More than one language
|
|
147
|
+
if (items.length < 2) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Open the dialog
|
|
152
|
+
setOpen(true);
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// Close handler
|
|
156
|
+
const closeHandler = () => {
|
|
157
|
+
if (!open) return;
|
|
158
|
+
|
|
159
|
+
// Close the dialog
|
|
160
|
+
setOpen(false);
|
|
161
|
+
|
|
162
|
+
// Emit close event
|
|
163
|
+
if (onClose) {
|
|
164
|
+
onClose(currentItem, false);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// Close item handler
|
|
169
|
+
const closeItemHandler = (item: T) => {
|
|
170
|
+
if (!keepClick) {
|
|
171
|
+
// Update the current item
|
|
172
|
+
setCurrentItem(item);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Close the dialog
|
|
176
|
+
setOpen(false);
|
|
177
|
+
|
|
178
|
+
// Emit close event
|
|
179
|
+
if (onClose) {
|
|
180
|
+
onClose(item, true);
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
return (
|
|
185
|
+
<>
|
|
186
|
+
<Button
|
|
187
|
+
className={className}
|
|
188
|
+
variant={variant}
|
|
189
|
+
startIcon={icon}
|
|
190
|
+
color={color}
|
|
191
|
+
size={size}
|
|
192
|
+
onClick={clickHandler}
|
|
193
|
+
>
|
|
194
|
+
{buttonLabel ?? (currentItem ? getLabel(currentItem) : undefined)}
|
|
195
|
+
</Button>
|
|
196
|
+
<Dialog aria-labelledby="dialog-title" open={open} onClose={closeHandler}>
|
|
197
|
+
{title && <DialogTitle id="dialog-title">{title}</DialogTitle>}
|
|
198
|
+
<DialogContent sx={{ minWidth }}>
|
|
199
|
+
<List>
|
|
200
|
+
{items.map((item) => {
|
|
201
|
+
const id = item[idField];
|
|
202
|
+
return (
|
|
203
|
+
<ListItemButton
|
|
204
|
+
key={id as unknown as React.Key}
|
|
205
|
+
disabled={
|
|
206
|
+
id === (currentItem ? currentItem[idField] : undefined) &&
|
|
207
|
+
!keepClick
|
|
208
|
+
}
|
|
209
|
+
onClick={() => closeItemHandler(item)}
|
|
210
|
+
>
|
|
211
|
+
<ListItemText>{getLabel(item)}</ListItemText>
|
|
212
|
+
</ListItemButton>
|
|
213
|
+
);
|
|
214
|
+
})}
|
|
215
|
+
</List>
|
|
216
|
+
</DialogContent>
|
|
217
|
+
</Dialog>
|
|
218
|
+
</>
|
|
219
|
+
);
|
|
227
220
|
}
|