@etsoo/materialui 1.1.1 → 1.1.3
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 +6 -6
- package/lib/ItemList.js +13 -12
- package/package.json +89 -93
- package/src/ItemList.tsx +203 -206
- package/.eslintignore +0 -3
- package/.eslintrc.json +0 -38
- package/.prettierignore +0 -5
- package/.prettierrc +0 -6
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,11 +35,11 @@ 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
|
*/
|
|
42
|
-
onClose?(item: T, changed: boolean): void;
|
|
42
|
+
onClose?(item: T | undefined, changed: boolean): void;
|
|
43
43
|
/**
|
|
44
44
|
* Current selected language
|
|
45
45
|
*/
|
|
@@ -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,28 +1,27 @@
|
|
|
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
|
-
var _a;
|
|
10
9
|
// properties destructure
|
|
11
|
-
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;
|
|
12
11
|
// Get label
|
|
13
12
|
const getLabel = (item) => {
|
|
14
13
|
var _a;
|
|
15
|
-
if (typeof labelField ===
|
|
14
|
+
if (typeof labelField === "function") {
|
|
16
15
|
return labelField(item);
|
|
17
16
|
}
|
|
18
17
|
else {
|
|
19
|
-
return (_a = DataTypes.convert(item[labelField],
|
|
18
|
+
return (_a = DataTypes.convert(item[labelField], "string")) !== null && _a !== void 0 ? _a : "";
|
|
20
19
|
}
|
|
21
20
|
};
|
|
22
21
|
// Dialog open or not state
|
|
23
22
|
const [open, setOpen] = React.useState(false);
|
|
24
23
|
// Default state
|
|
25
|
-
const defaultItem =
|
|
24
|
+
const defaultItem = items.find((item) => item[idField] === selectedValue);
|
|
26
25
|
// Current item
|
|
27
26
|
const [currentItem, setCurrentItem] = React.useState(defaultItem);
|
|
28
27
|
// Click handler
|
|
@@ -47,8 +46,10 @@ export function ItemList(props) {
|
|
|
47
46
|
};
|
|
48
47
|
// Close item handler
|
|
49
48
|
const closeItemHandler = (item) => {
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
if (!keepClick) {
|
|
50
|
+
// Update the current item
|
|
51
|
+
setCurrentItem(item);
|
|
52
|
+
}
|
|
52
53
|
// Close the dialog
|
|
53
54
|
setOpen(false);
|
|
54
55
|
// Emit close event
|
|
@@ -57,13 +58,13 @@ export function ItemList(props) {
|
|
|
57
58
|
}
|
|
58
59
|
};
|
|
59
60
|
return (React.createElement(React.Fragment, null,
|
|
60
|
-
React.createElement(Button, { className: className, variant: variant, startIcon: icon, color: color, size: size, onClick: clickHandler }, buttonLabel !== null && buttonLabel !== void 0 ? buttonLabel : getLabel(currentItem)),
|
|
61
|
+
React.createElement(Button, { className: className, variant: variant, startIcon: icon, color: color, size: size, onClick: clickHandler }, buttonLabel !== null && buttonLabel !== void 0 ? buttonLabel : (currentItem ? getLabel(currentItem) : undefined)),
|
|
61
62
|
React.createElement(Dialog, { "aria-labelledby": "dialog-title", open: open, onClose: closeHandler },
|
|
62
63
|
title && React.createElement(DialogTitle, { id: "dialog-title" }, title),
|
|
63
64
|
React.createElement(DialogContent, { sx: { minWidth } },
|
|
64
65
|
React.createElement(List, null, items.map((item) => {
|
|
65
66
|
const id = item[idField];
|
|
66
|
-
return (React.createElement(ListItemButton, { key: id, disabled: id === currentItem[idField] &&
|
|
67
|
+
return (React.createElement(ListItemButton, { key: id, disabled: id === (currentItem ? currentItem[idField] : undefined) &&
|
|
67
68
|
!keepClick, onClick: () => closeItemHandler(item) },
|
|
68
69
|
React.createElement(ListItemText, null, getLabel(item))));
|
|
69
70
|
}))))));
|
package/package.json
CHANGED
|
@@ -1,97 +1,93 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
],
|
|
19
|
-
"testEnvironment": "jsdom",
|
|
20
|
-
"transform": {
|
|
21
|
-
"^.+\\.[t|j]sx?$": "babel-jest"
|
|
22
|
-
},
|
|
23
|
-
"transformIgnorePatterns": [
|
|
24
|
-
"/node_modules/(?!(@etsoo)/)"
|
|
25
|
-
]
|
|
26
|
-
},
|
|
27
|
-
"repository": {
|
|
28
|
-
"type": "git",
|
|
29
|
-
"url": "git+https://github.com/ETSOO/ReactMU.git"
|
|
30
|
-
},
|
|
31
|
-
"keywords": [
|
|
32
|
-
"React",
|
|
33
|
-
"MaterialUI",
|
|
34
|
-
"TypeScript",
|
|
35
|
-
"ETSOO",
|
|
36
|
-
"SmartERP",
|
|
37
|
-
"司友云平台",
|
|
38
|
-
"青岛亿速思维",
|
|
39
|
-
"上海亿商"
|
|
2
|
+
"name": "@etsoo/materialui",
|
|
3
|
+
"version": "1.1.3",
|
|
4
|
+
"description": "TypeScript Material-UI Implementation",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"format": "prettier --write src/**/*.{ts,tsx}",
|
|
10
|
+
"lint": "eslint --ext .ts,.tsx src/",
|
|
11
|
+
"test": "jest",
|
|
12
|
+
"test:debug": "node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand"
|
|
13
|
+
},
|
|
14
|
+
"jest": {
|
|
15
|
+
"automock": false,
|
|
16
|
+
"testMatch": [
|
|
17
|
+
"<rootDir>/__tests__/**/*.{ts,tsx}"
|
|
40
18
|
],
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
"url": "https://github.com/ETSOO/ReactMU/issues"
|
|
45
|
-
},
|
|
46
|
-
"homepage": "https://github.com/ETSOO/ReactMU#readme",
|
|
47
|
-
"dependencies": {
|
|
48
|
-
"@dnd-kit/core": "^6.0.6",
|
|
49
|
-
"@dnd-kit/sortable": "^7.0.1",
|
|
50
|
-
"@emotion/css": "^11.10.5",
|
|
51
|
-
"@emotion/react": "^11.10.5",
|
|
52
|
-
"@emotion/styled": "^11.10.5",
|
|
53
|
-
"@etsoo/appscript": "^1.3.50",
|
|
54
|
-
"@etsoo/notificationbase": "^1.1.20",
|
|
55
|
-
"@etsoo/react": "^1.6.37",
|
|
56
|
-
"@etsoo/shared": "^1.1.84",
|
|
57
|
-
"@mui/icons-material": "^5.11.0",
|
|
58
|
-
"@mui/material": "^5.11.2",
|
|
59
|
-
"@types/pica": "^9.0.1",
|
|
60
|
-
"@types/pulltorefreshjs": "^0.1.5",
|
|
61
|
-
"@types/react": "^18.0.26",
|
|
62
|
-
"@types/react-avatar-editor": "^13.0.0",
|
|
63
|
-
"@types/react-dom": "^18.0.10",
|
|
64
|
-
"@types/react-input-mask": "^3.0.2",
|
|
65
|
-
"@types/react-window": "^1.8.5",
|
|
66
|
-
"pica": "^9.0.1",
|
|
67
|
-
"pulltorefreshjs": "^0.1.22",
|
|
68
|
-
"react": "^18.2.0",
|
|
69
|
-
"react-avatar-editor": "^13.0.0",
|
|
70
|
-
"react-dom": "^18.2.0",
|
|
71
|
-
"react-draggable": "^4.4.5",
|
|
72
|
-
"react-imask": "^6.4.3",
|
|
73
|
-
"react-router-dom": "^6.6.1",
|
|
74
|
-
"react-window": "^1.8.8"
|
|
19
|
+
"testEnvironment": "jsdom",
|
|
20
|
+
"transform": {
|
|
21
|
+
"^.+\\.[t|j]sx?$": "babel-jest"
|
|
75
22
|
},
|
|
76
|
-
"
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
23
|
+
"transformIgnorePatterns": [
|
|
24
|
+
"/node_modules/(?!(@etsoo)/)"
|
|
25
|
+
]
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "git+https://github.com/ETSOO/ReactMU.git"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"React",
|
|
33
|
+
"MaterialUI",
|
|
34
|
+
"TypeScript",
|
|
35
|
+
"ETSOO",
|
|
36
|
+
"SmartERP",
|
|
37
|
+
"司友云平台",
|
|
38
|
+
"青岛亿速思维",
|
|
39
|
+
"上海亿商"
|
|
40
|
+
],
|
|
41
|
+
"author": "ETSOO",
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/ETSOO/ReactMU/issues"
|
|
45
|
+
},
|
|
46
|
+
"homepage": "https://github.com/ETSOO/ReactMU#readme",
|
|
47
|
+
"dependencies": {
|
|
48
|
+
"@dnd-kit/core": "^6.0.6",
|
|
49
|
+
"@dnd-kit/sortable": "^7.0.1",
|
|
50
|
+
"@emotion/css": "^11.10.5",
|
|
51
|
+
"@emotion/react": "^11.10.5",
|
|
52
|
+
"@emotion/styled": "^11.10.5",
|
|
53
|
+
"@etsoo/appscript": "^1.3.50",
|
|
54
|
+
"@etsoo/notificationbase": "^1.1.20",
|
|
55
|
+
"@etsoo/react": "^1.6.37",
|
|
56
|
+
"@etsoo/shared": "^1.1.84",
|
|
57
|
+
"@mui/icons-material": "^5.11.0",
|
|
58
|
+
"@mui/material": "^5.11.2",
|
|
59
|
+
"@types/pica": "^9.0.1",
|
|
60
|
+
"@types/pulltorefreshjs": "^0.1.5",
|
|
61
|
+
"@types/react": "^18.0.26",
|
|
62
|
+
"@types/react-avatar-editor": "^13.0.0",
|
|
63
|
+
"@types/react-dom": "^18.0.10",
|
|
64
|
+
"@types/react-input-mask": "^3.0.2",
|
|
65
|
+
"@types/react-window": "^1.8.5",
|
|
66
|
+
"pica": "^9.0.1",
|
|
67
|
+
"pulltorefreshjs": "^0.1.22",
|
|
68
|
+
"react": "^18.2.0",
|
|
69
|
+
"react-avatar-editor": "^13.0.0",
|
|
70
|
+
"react-dom": "^18.2.0",
|
|
71
|
+
"react-draggable": "^4.4.5",
|
|
72
|
+
"react-imask": "^6.4.3",
|
|
73
|
+
"react-router-dom": "^6.6.1",
|
|
74
|
+
"react-window": "^1.8.8"
|
|
75
|
+
},
|
|
76
|
+
"devDependencies": {
|
|
77
|
+
"@babel/cli": "^7.20.7",
|
|
78
|
+
"@babel/core": "^7.20.7",
|
|
79
|
+
"@babel/plugin-transform-runtime": "^7.19.6",
|
|
80
|
+
"@babel/preset-env": "^7.20.2",
|
|
81
|
+
"@babel/preset-react": "^7.18.6",
|
|
82
|
+
"@babel/preset-typescript": "^7.18.6",
|
|
83
|
+
"@babel/runtime-corejs3": "^7.20.7",
|
|
84
|
+
"@testing-library/jest-dom": "^5.16.5",
|
|
85
|
+
"@testing-library/react": "^13.4.0",
|
|
86
|
+
"@types/jest": "^29.2.4",
|
|
87
|
+
"@typescript-eslint/eslint-plugin": "^5.47.1",
|
|
88
|
+
"@typescript-eslint/parser": "^5.47.1",
|
|
89
|
+
"jest": "^29.3.1",
|
|
90
|
+
"jest-environment-jsdom": "^29.3.1",
|
|
91
|
+
"typescript": "^4.9.4"
|
|
92
|
+
}
|
|
97
93
|
}
|
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,125 +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
|
-
</Dialog>
|
|
221
|
-
</>
|
|
222
|
-
);
|
|
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
|
+
);
|
|
223
220
|
}
|
package/.eslintignore
DELETED
package/.eslintrc.json
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"env": {
|
|
3
|
-
"browser": true,
|
|
4
|
-
"node": true,
|
|
5
|
-
"es6": true,
|
|
6
|
-
"jest": true
|
|
7
|
-
},
|
|
8
|
-
"extends": ["plugin:react/recommended", "airbnb-base", "prettier"],
|
|
9
|
-
"parser": "@typescript-eslint/parser",
|
|
10
|
-
"parserOptions": {
|
|
11
|
-
"ecmaFeatures": {
|
|
12
|
-
"jsx": true
|
|
13
|
-
},
|
|
14
|
-
"ecmaVersion": 6,
|
|
15
|
-
"sourceType": "module"
|
|
16
|
-
},
|
|
17
|
-
"plugins": ["@typescript-eslint"],
|
|
18
|
-
"rules": {
|
|
19
|
-
"class-methods-use-this": "off",
|
|
20
|
-
"import/extensions": ["error", "never"],
|
|
21
|
-
"import/prefer-default-export": "off",
|
|
22
|
-
"@typescript-eslint/no-unused-vars": ["error"]
|
|
23
|
-
},
|
|
24
|
-
"settings": {
|
|
25
|
-
"import/resolver": {
|
|
26
|
-
"node": {
|
|
27
|
-
"paths": ["src"],
|
|
28
|
-
"extensions": [".ts", ".tsx"]
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
"react": {
|
|
32
|
-
"createClass": "createReactClass",
|
|
33
|
-
"pragma": "React",
|
|
34
|
-
"version": "detect",
|
|
35
|
-
"flowVersion": "0.53"
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
package/.prettierignore
DELETED