@etsoo/materialui 1.1.39 → 1.1.41

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/src/DataSteps.tsx CHANGED
@@ -1,57 +1,57 @@
1
1
  import {
2
- Button,
3
- IconButton,
4
- InputAdornment,
5
- TextField,
6
- TextFieldProps
7
- } from '@mui/material';
8
- import CloseIcon from '@mui/icons-material/Close';
9
- import NavigateNextIcon from '@mui/icons-material/NavigateNext';
10
- import NavigateBeforeIcon from '@mui/icons-material/NavigateBefore';
11
- import CheckIcon from '@mui/icons-material/Check';
12
- import StartIcon from '@mui/icons-material/Start';
13
- import { InputDialogProps } from '@etsoo/react';
14
- import React from 'react';
15
- import { HBox } from './FlexBox';
16
- import { globalApp } from './app/ReactApp';
17
- import { MUGlobal } from './MUGlobal';
2
+ Button,
3
+ IconButton,
4
+ InputAdornment,
5
+ TextField,
6
+ TextFieldProps
7
+ } from "@mui/material";
8
+ import CloseIcon from "@mui/icons-material/Close";
9
+ import NavigateNextIcon from "@mui/icons-material/NavigateNext";
10
+ import NavigateBeforeIcon from "@mui/icons-material/NavigateBefore";
11
+ import CheckIcon from "@mui/icons-material/Check";
12
+ import StartIcon from "@mui/icons-material/Start";
13
+ import { InputDialogProps } from "@etsoo/react";
14
+ import React from "react";
15
+ import { HBox } from "./FlexBox";
16
+ import { globalApp } from "./app/ReactApp";
17
+ import { MUGlobal } from "./MUGlobal";
18
18
 
19
19
  /**
20
20
  * Data step
21
21
  */
22
- export type DataStep = Omit<InputDialogProps, 'callback'> & {
23
- /**
24
- * Callback
25
- */
26
- callback: (form: HTMLFormElement) => boolean | void;
22
+ export type DataStep = Omit<InputDialogProps, "callback"> & {
23
+ /**
24
+ * Callback
25
+ */
26
+ callback: (form: HTMLFormElement) => boolean | void;
27
27
  };
28
28
 
29
29
  /**
30
30
  * Data collecting steps component props
31
31
  */
32
32
  export type DataStepsProps<T extends object> = Omit<
33
- TextFieldProps,
34
- 'InputProps' | 'onClick'
33
+ TextFieldProps,
34
+ "InputProps" | "onClick"
35
35
  > & {
36
- /**
37
- * JSON value
38
- */
39
- jsonValue: T;
40
-
41
- /**
42
- * Value formatter
43
- */
44
- valueFormatter?: (data: T) => string;
45
-
46
- /**
47
- * Steps
48
- */
49
- steps: (index: number, data: T) => [DataStep, boolean];
50
-
51
- /**
52
- * On value change handler
53
- */
54
- onValueChange?: (value: T) => void;
36
+ /**
37
+ * JSON value
38
+ */
39
+ jsonValue: T;
40
+
41
+ /**
42
+ * Value formatter
43
+ */
44
+ valueFormatter?: (data: T) => string;
45
+
46
+ /**
47
+ * Steps
48
+ */
49
+ steps: (index: number, data: T) => [DataStep, boolean];
50
+
51
+ /**
52
+ * On value change handler
53
+ */
54
+ onValueChange?: (value: T) => void;
55
55
  };
56
56
 
57
57
  /**
@@ -60,147 +60,150 @@ export type DataStepsProps<T extends object> = Omit<
60
60
  * @returns Component
61
61
  */
62
62
  export function DataSteps<T extends object>(props: DataStepsProps<T>) {
63
- // App
64
- const app = globalApp;
65
-
66
- // Labels
67
- const labels = app.getLabels('close', 'nextStep', 'previousStep', 'submit');
68
-
69
- // Destruct
70
- const {
71
- InputLabelProps = {},
72
- jsonValue,
73
- valueFormatter = (_data) => '...',
74
- onValueChange,
75
- steps,
76
- value = '',
77
- ...rest
78
- } = props;
79
-
80
- // Shrink
81
- InputLabelProps.shrink ??= MUGlobal.searchFieldShrink;
82
-
83
- // Current index
84
- const indexRef = React.useRef<number>(-1);
85
-
86
- // Current Json data
87
- const jsonRef = React.useRef<T>(jsonValue);
88
-
89
- // Ignore empty value case
90
- if (jsonValue !== jsonRef.current && valueFormatter(jsonValue))
91
- jsonRef.current = jsonValue;
92
-
93
- // Current value
94
- const [localValue, setLocalValue] = React.useState(value);
95
-
96
- // Get step
97
- const showStep = (index: number) => {
98
- indexRef.current = index;
99
- const [{ callback, ...rest }, more] = steps(index, jsonRef.current);
100
-
101
- app.showInputDialog({
102
- ...rest,
103
- buttons: (n, callback) => (
104
- <HBox
105
- paddingLeft={2}
106
- paddingRight={2}
107
- paddingBottom={2}
108
- gap={2}
109
- justifyContent="space-between"
110
- >
111
- {index === 0 ? (
112
- <Button
113
- variant="outlined"
114
- startIcon={<CloseIcon />}
115
- onClick={() => n.dismiss()}
116
- >
117
- {labels.close}
118
- </Button>
119
- ) : (
120
- <Button
121
- variant="outlined"
122
- startIcon={<NavigateBeforeIcon />}
123
- onClick={() => {
124
- n.dismiss();
125
- showStep(indexRef.current - 1);
126
- }}
127
- >
128
- {labels.previousStep}
129
- </Button>
130
- )}
131
-
132
- {more ? (
133
- <Button
134
- variant="contained"
135
- startIcon={<NavigateNextIcon />}
136
- onClick={async (event) => {
137
- const result = await callback(event);
138
- if (!result) return;
139
- showStep(indexRef.current + 1);
140
- }}
141
- >
142
- {labels.nextStep}
143
- </Button>
144
- ) : (
145
- <Button
146
- variant="contained"
147
- startIcon={<CheckIcon />}
148
- onClick={async (event) => {
149
- const result = await callback(event);
150
- if (!result) return;
151
-
152
- const value = jsonRef.current;
153
- setLocalValue(valueFormatter(value));
154
-
155
- if (onValueChange) onValueChange(value);
156
- }}
157
- >
158
- {labels.submit}
159
- </Button>
160
- )}
161
- </HBox>
162
- ),
163
- callback: (form) => {
164
- if (form == null) return;
165
- const result = callback(form);
166
- if (result !== true) {
167
- return false;
168
- }
169
- }
170
- });
171
- };
172
-
173
- const cancelInput = (event: React.SyntheticEvent) => {
174
- event.stopPropagation();
175
- event.preventDefault();
176
- };
177
-
178
- React.useEffect(() => {
179
- setLocalValue(valueFormatter(jsonRef.current));
180
- }, [valueFormatter]);
181
-
182
- return (
183
- <TextField
184
- autoComplete="new-password"
185
- InputLabelProps={InputLabelProps}
186
- inputProps={{ style: { cursor: 'pointer' } }}
187
- InputProps={{
188
- onKeyDown: (event) => {
189
- if (event.key === 'Tab') return;
190
- cancelInput(event);
191
- },
192
- onPaste: cancelInput,
193
- endAdornment: (
194
- <InputAdornment position="end">
195
- <IconButton edge="end" size="small">
196
- <StartIcon />
197
- </IconButton>
198
- </InputAdornment>
199
- )
200
- }}
201
- onClick={() => showStep(0)}
202
- value={localValue}
203
- {...rest}
204
- />
205
- );
63
+ // App
64
+ const app = globalApp;
65
+ if (app == null) {
66
+ throw new Error("No globalApp");
67
+ }
68
+
69
+ // Labels
70
+ const labels = app.getLabels("close", "nextStep", "previousStep", "submit");
71
+
72
+ // Destruct
73
+ const {
74
+ InputLabelProps = {},
75
+ jsonValue,
76
+ valueFormatter = (_data) => "...",
77
+ onValueChange,
78
+ steps,
79
+ value = "",
80
+ ...rest
81
+ } = props;
82
+
83
+ // Shrink
84
+ InputLabelProps.shrink ??= MUGlobal.searchFieldShrink;
85
+
86
+ // Current index
87
+ const indexRef = React.useRef<number>(-1);
88
+
89
+ // Current Json data
90
+ const jsonRef = React.useRef<T>(jsonValue);
91
+
92
+ // Ignore empty value case
93
+ if (jsonValue !== jsonRef.current && valueFormatter(jsonValue))
94
+ jsonRef.current = jsonValue;
95
+
96
+ // Current value
97
+ const [localValue, setLocalValue] = React.useState(value);
98
+
99
+ // Get step
100
+ const showStep = (index: number) => {
101
+ indexRef.current = index;
102
+ const [{ callback, ...rest }, more] = steps(index, jsonRef.current);
103
+
104
+ app.showInputDialog({
105
+ ...rest,
106
+ buttons: (n, callback) => (
107
+ <HBox
108
+ paddingLeft={2}
109
+ paddingRight={2}
110
+ paddingBottom={2}
111
+ gap={2}
112
+ justifyContent="space-between"
113
+ >
114
+ {index === 0 ? (
115
+ <Button
116
+ variant="outlined"
117
+ startIcon={<CloseIcon />}
118
+ onClick={() => n.dismiss()}
119
+ >
120
+ {labels.close}
121
+ </Button>
122
+ ) : (
123
+ <Button
124
+ variant="outlined"
125
+ startIcon={<NavigateBeforeIcon />}
126
+ onClick={() => {
127
+ n.dismiss();
128
+ showStep(indexRef.current - 1);
129
+ }}
130
+ >
131
+ {labels.previousStep}
132
+ </Button>
133
+ )}
134
+
135
+ {more ? (
136
+ <Button
137
+ variant="contained"
138
+ startIcon={<NavigateNextIcon />}
139
+ onClick={async (event) => {
140
+ const result = await callback(event);
141
+ if (!result) return;
142
+ showStep(indexRef.current + 1);
143
+ }}
144
+ >
145
+ {labels.nextStep}
146
+ </Button>
147
+ ) : (
148
+ <Button
149
+ variant="contained"
150
+ startIcon={<CheckIcon />}
151
+ onClick={async (event) => {
152
+ const result = await callback(event);
153
+ if (!result) return;
154
+
155
+ const value = jsonRef.current;
156
+ setLocalValue(valueFormatter(value));
157
+
158
+ if (onValueChange) onValueChange(value);
159
+ }}
160
+ >
161
+ {labels.submit}
162
+ </Button>
163
+ )}
164
+ </HBox>
165
+ ),
166
+ callback: (form) => {
167
+ if (form == null) return;
168
+ const result = callback(form);
169
+ if (result !== true) {
170
+ return false;
171
+ }
172
+ }
173
+ });
174
+ };
175
+
176
+ const cancelInput = (event: React.SyntheticEvent) => {
177
+ event.stopPropagation();
178
+ event.preventDefault();
179
+ };
180
+
181
+ React.useEffect(() => {
182
+ setLocalValue(valueFormatter(jsonRef.current));
183
+ }, [valueFormatter]);
184
+
185
+ return (
186
+ <TextField
187
+ autoComplete="new-password"
188
+ InputLabelProps={InputLabelProps}
189
+ inputProps={{ style: { cursor: "pointer" } }}
190
+ InputProps={{
191
+ onKeyDown: (event) => {
192
+ if (event.key === "Tab") return;
193
+ cancelInput(event);
194
+ },
195
+ onPaste: cancelInput,
196
+ endAdornment: (
197
+ <InputAdornment position="end">
198
+ <IconButton edge="end" size="small">
199
+ <StartIcon />
200
+ </IconButton>
201
+ </InputAdornment>
202
+ )
203
+ }}
204
+ onClick={() => showStep(0)}
205
+ value={localValue}
206
+ {...rest}
207
+ />
208
+ );
206
209
  }