@etsoo/react 1.4.47 → 1.4.51

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/index.d.ts CHANGED
@@ -65,6 +65,7 @@ export * from './mu/SearchField';
65
65
  export * from './mu/SearchOptionGroup';
66
66
  export * from './mu/SelectEx';
67
67
  export * from './mu/Switch';
68
+ export * from './mu/SwitchAnt';
68
69
  export * from './mu/TabBox';
69
70
  export * from './mu/TableEx';
70
71
  export * from './mu/TextFieldEx';
package/lib/index.js CHANGED
@@ -68,6 +68,7 @@ export * from './mu/SearchField';
68
68
  export * from './mu/SearchOptionGroup';
69
69
  export * from './mu/SelectEx';
70
70
  export * from './mu/Switch';
71
+ export * from './mu/SwitchAnt';
71
72
  export * from './mu/TabBox';
72
73
  export * from './mu/TableEx';
73
74
  export * from './mu/TextFieldEx';
@@ -10,16 +10,19 @@ import { SearchField } from './SearchField';
10
10
  */
11
11
  export function ComboBox(props) {
12
12
  // Destruct
13
- const { search = false, idField = 'id', idValue, inputError, inputHelperText, inputMargin, inputOnChange, inputRequired, inputVariant, defaultValue, label, labelField = 'label', loadData, onLoadData, name, inputAutoComplete = 'off', options = [], readOnly = true, onChange, value, getOptionLabel = (option) => String(Reflect.get(option, labelField)), sx = { minWidth: '150px' }, ...rest } = props;
13
+ const { search = false, idField = 'id', idValue, inputError, inputHelperText, inputMargin, inputOnChange, inputRequired, inputVariant, defaultValue, label, labelField = 'label', loadData, onLoadData, name, inputAutoComplete = 'off', options, readOnly = true, onChange, value, getOptionLabel = (option) => String(Reflect.get(option, labelField)), sx = { minWidth: '150px' }, ...rest } = props;
14
14
  // Value input ref
15
15
  const inputRef = React.createRef();
16
16
  // Options state
17
- const [localOptions, setOptions] = React.useState(options);
17
+ const [localOptions, setOptions] = React.useState(options !== null && options !== void 0 ? options : []);
18
18
  const isMounted = React.useRef(true);
19
19
  // When options change
20
+ // [options] will cause infinite loop
21
+ const propertyWay = loadData == null;
20
22
  React.useEffect(() => {
21
- setOptions(options);
22
- }, [options]);
23
+ if (propertyWay && options != null)
24
+ setOptions(options);
25
+ }, [JSON.stringify(options), propertyWay]);
23
26
  // Local default value
24
27
  let localValue = idValue != null
25
28
  ? localOptions.find((o) => Reflect.get(o, idField) === idValue)
@@ -16,9 +16,12 @@ export function SelectEx(props) {
16
16
  const [localOptions, setOptions] = React.useState(options);
17
17
  const isMounted = React.useRef(true);
18
18
  // When options change
19
+ // [options] will cause infinite loop
20
+ const propertyWay = loadData == null;
19
21
  React.useEffect(() => {
20
- setOptions(options);
21
- }, [options]);
22
+ if (propertyWay && options != null)
23
+ setOptions(options);
24
+ }, [JSON.stringify(options), propertyWay]);
22
25
  // Local value
23
26
  const valueSource = (_a = defaultValue !== null && defaultValue !== void 0 ? defaultValue : value) !== null && _a !== void 0 ? _a : '';
24
27
  let localValue;
package/lib/mu/Switch.js CHANGED
@@ -8,13 +8,20 @@ import MuiSwitch from '@mui/material/Switch';
8
8
  * @returns Component
9
9
  */
10
10
  export function Switch(props) {
11
+ var _a;
11
12
  // Destruct
12
- const { defaultChecked, defaultValue, readOnly, size, checkbox = false, value = 'true', ...rest } = props;
13
+ const { checked, defaultChecked, defaultValue, onChange, readOnly, size, checkbox = false, value = 'true', ...rest } = props;
13
14
  // Checked state
14
- const [controlChecked, setControlChecked] = React.useState(defaultChecked !== null && defaultChecked !== void 0 ? defaultChecked : defaultValue == value);
15
+ const [controlChecked, setControlChecked] = React.useState((_a = checked !== null && checked !== void 0 ? checked : defaultChecked) !== null && _a !== void 0 ? _a : defaultValue == value);
16
+ React.useEffect(() => {
17
+ if (checked)
18
+ setControlChecked(checked);
19
+ }, [checked]);
15
20
  // Handle change
16
- const handleChange = (event) => {
17
- setControlChecked(event.target.checked);
21
+ const handleChange = (event, checked) => {
22
+ if (onChange)
23
+ onChange(event, checked);
24
+ setControlChecked(checked);
18
25
  };
19
26
  // Control
20
27
  const control = checkbox ? (React.createElement(MuiCheckbox, { readOnly: readOnly, checked: controlChecked, onChange: handleChange, size: size, value: value })) : (React.createElement(MuiSwitch, { readOnly: readOnly, checked: controlChecked, onChange: handleChange, size: size, value: value }));
@@ -0,0 +1,25 @@
1
+ /// <reference types="react" />
2
+ import { SwitchProps } from '@mui/material/Switch';
3
+ /**
4
+ * Ant style switch props
5
+ */
6
+ export interface SwitchAntProps extends SwitchProps {
7
+ /**
8
+ *
9
+ */
10
+ activeColor?: string;
11
+ /**
12
+ * Start label
13
+ */
14
+ startLabel: string;
15
+ /**
16
+ * End label
17
+ */
18
+ endLabel: string;
19
+ }
20
+ /**
21
+ * Ant style switch
22
+ * @param props Props
23
+ * @returns Component
24
+ */
25
+ export declare function SwitchAnt(props: SwitchAntProps): JSX.Element;
@@ -0,0 +1,40 @@
1
+ import { Stack, Typography } from '@mui/material';
2
+ import Switch from '@mui/material/Switch';
3
+ import React from 'react';
4
+ /**
5
+ * Ant style switch
6
+ * @param props Props
7
+ * @returns Component
8
+ */
9
+ export function SwitchAnt(props) {
10
+ var _a;
11
+ // Destruct
12
+ const { activeColor, checked, defaultChecked, defaultValue, endLabel, startLabel, onChange, value = 'true', ...rest } = props;
13
+ // Checked state
14
+ const [controlChecked, setControlChecked] = React.useState((_a = checked !== null && checked !== void 0 ? checked : defaultChecked) !== null && _a !== void 0 ? _a : defaultValue == value);
15
+ React.useEffect(() => {
16
+ if (checked)
17
+ setControlChecked(checked);
18
+ }, [checked]);
19
+ // On change
20
+ const onChangeLocal = (event, checked) => {
21
+ if (onChange)
22
+ onChange(event, checked);
23
+ setControlChecked(checked);
24
+ };
25
+ // Layout
26
+ return (React.createElement(Stack, { direction: "row", spacing: 1, alignItems: "center" },
27
+ React.createElement(Typography, { onClick: () => setControlChecked(false), sx: {
28
+ cursor: 'pointer',
29
+ color: controlChecked
30
+ ? undefined
31
+ : (theme) => activeColor !== null && activeColor !== void 0 ? activeColor : theme.palette.warning.main
32
+ } }, startLabel),
33
+ React.createElement(Switch, { checked: controlChecked, value: value, onChange: onChangeLocal, ...rest }),
34
+ React.createElement(Typography, { onClick: () => setControlChecked(true), sx: {
35
+ cursor: 'pointer',
36
+ color: controlChecked
37
+ ? (theme) => activeColor !== null && activeColor !== void 0 ? activeColor : theme.palette.warning.main
38
+ : undefined
39
+ } }, endLabel)));
40
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/react",
3
- "version": "1.4.47",
3
+ "version": "1.4.51",
4
4
  "description": "TypeScript ReactJs framework",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -50,11 +50,11 @@
50
50
  "@emotion/react": "^11.7.1",
51
51
  "@emotion/style": "^0.8.0",
52
52
  "@emotion/styled": "^11.6.0",
53
- "@etsoo/appscript": "^1.2.24",
53
+ "@etsoo/appscript": "^1.2.25",
54
54
  "@etsoo/notificationbase": "^1.1.0",
55
- "@etsoo/shared": "^1.1.3",
56
- "@mui/icons-material": "^5.2.5",
57
- "@mui/material": "^5.2.8",
55
+ "@etsoo/shared": "^1.1.5",
56
+ "@mui/icons-material": "^5.3.0",
57
+ "@mui/material": "^5.3.0",
58
58
  "@reach/router": "^1.3.4",
59
59
  "@types/pica": "^5.1.3",
60
60
  "@types/pulltorefreshjs": "^0.1.5",
@@ -83,8 +83,8 @@
83
83
  "@babel/runtime-corejs3": "^7.16.8",
84
84
  "@types/jest": "^27.4.0",
85
85
  "@types/react-test-renderer": "^17.0.1",
86
- "@typescript-eslint/eslint-plugin": "^5.9.1",
87
- "@typescript-eslint/parser": "^5.9.1",
86
+ "@typescript-eslint/eslint-plugin": "^5.10.0",
87
+ "@typescript-eslint/parser": "^5.10.0",
88
88
  "eslint": "^8.7.0",
89
89
  "eslint-config-airbnb-base": "^15.0.0",
90
90
  "eslint-plugin-import": "^2.25.4",
package/src/index.ts CHANGED
@@ -72,6 +72,7 @@ export * from './mu/SearchField';
72
72
  export * from './mu/SearchOptionGroup';
73
73
  export * from './mu/SelectEx';
74
74
  export * from './mu/Switch';
75
+ export * from './mu/SwitchAnt';
75
76
  export * from './mu/TabBox';
76
77
  export * from './mu/TableEx';
77
78
  export * from './mu/TextFieldEx';
@@ -56,7 +56,7 @@ export function ComboBox<T extends {} = IdLabelDto>(props: ComboBoxProps<T>) {
56
56
  onLoadData,
57
57
  name,
58
58
  inputAutoComplete = 'off',
59
- options = [],
59
+ options,
60
60
  readOnly = true,
61
61
  onChange,
62
62
  value,
@@ -69,13 +69,15 @@ export function ComboBox<T extends {} = IdLabelDto>(props: ComboBoxProps<T>) {
69
69
  const inputRef = React.createRef<HTMLInputElement>();
70
70
 
71
71
  // Options state
72
- const [localOptions, setOptions] = React.useState(options);
72
+ const [localOptions, setOptions] = React.useState(options ?? []);
73
73
  const isMounted = React.useRef(true);
74
74
 
75
75
  // When options change
76
+ // [options] will cause infinite loop
77
+ const propertyWay = loadData == null;
76
78
  React.useEffect(() => {
77
- setOptions(options);
78
- }, [options]);
79
+ if (propertyWay && options != null) setOptions(options);
80
+ }, [JSON.stringify(options), propertyWay]);
79
81
 
80
82
  // Local default value
81
83
  let localValue =
@@ -91,9 +91,11 @@ export function SelectEx<T extends {} = IdLabelDto>(props: SelectExProps<T>) {
91
91
  const isMounted = React.useRef(true);
92
92
 
93
93
  // When options change
94
+ // [options] will cause infinite loop
95
+ const propertyWay = loadData == null;
94
96
  React.useEffect(() => {
95
- setOptions(options);
96
- }, [options]);
97
+ if (propertyWay && options != null) setOptions(options);
98
+ }, [JSON.stringify(options), propertyWay]);
97
99
 
98
100
  // Local value
99
101
  const valueSource = defaultValue ?? value ?? '';
package/src/mu/Switch.tsx CHANGED
@@ -36,8 +36,10 @@ export interface SwitchProps extends Omit<FormControlLabelProps, 'control'> {
36
36
  export function Switch(props: SwitchProps) {
37
37
  // Destruct
38
38
  const {
39
+ checked,
39
40
  defaultChecked,
40
41
  defaultValue,
42
+ onChange,
41
43
  readOnly,
42
44
  size,
43
45
  checkbox = false,
@@ -47,12 +49,20 @@ export function Switch(props: SwitchProps) {
47
49
 
48
50
  // Checked state
49
51
  const [controlChecked, setControlChecked] = React.useState(
50
- defaultChecked ?? defaultValue == value
52
+ checked ?? defaultChecked ?? defaultValue == value
51
53
  );
52
54
 
55
+ React.useEffect(() => {
56
+ if (checked) setControlChecked(checked);
57
+ }, [checked]);
58
+
53
59
  // Handle change
54
- const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
55
- setControlChecked(event.target.checked);
60
+ const handleChange = (
61
+ event: React.ChangeEvent<HTMLInputElement>,
62
+ checked: boolean
63
+ ) => {
64
+ if (onChange) onChange(event, checked);
65
+ setControlChecked(checked);
56
66
  };
57
67
 
58
68
  // Control
@@ -0,0 +1,95 @@
1
+ import { Stack, Typography } from '@mui/material';
2
+ import Switch, { SwitchProps } from '@mui/material/Switch';
3
+ import React from 'react';
4
+
5
+ /**
6
+ * Ant style switch props
7
+ */
8
+ export interface SwitchAntProps extends SwitchProps {
9
+ /**
10
+ *
11
+ */
12
+ activeColor?: string;
13
+
14
+ /**
15
+ * Start label
16
+ */
17
+ startLabel: string;
18
+
19
+ /**
20
+ * End label
21
+ */
22
+ endLabel: string;
23
+ }
24
+
25
+ /**
26
+ * Ant style switch
27
+ * @param props Props
28
+ * @returns Component
29
+ */
30
+ export function SwitchAnt(props: SwitchAntProps) {
31
+ // Destruct
32
+ const {
33
+ activeColor,
34
+ checked,
35
+ defaultChecked,
36
+ defaultValue,
37
+ endLabel,
38
+ startLabel,
39
+ onChange,
40
+ value = 'true',
41
+ ...rest
42
+ } = props;
43
+
44
+ // Checked state
45
+ const [controlChecked, setControlChecked] = React.useState(
46
+ checked ?? defaultChecked ?? defaultValue == value
47
+ );
48
+
49
+ React.useEffect(() => {
50
+ if (checked) setControlChecked(checked);
51
+ }, [checked]);
52
+
53
+ // On change
54
+ const onChangeLocal = (
55
+ event: React.ChangeEvent<HTMLInputElement>,
56
+ checked: boolean
57
+ ) => {
58
+ if (onChange) onChange(event, checked);
59
+ setControlChecked(checked);
60
+ };
61
+
62
+ // Layout
63
+ return (
64
+ <Stack direction="row" spacing={1} alignItems="center">
65
+ <Typography
66
+ onClick={() => setControlChecked(false)}
67
+ sx={{
68
+ cursor: 'pointer',
69
+ color: controlChecked
70
+ ? undefined
71
+ : (theme) => activeColor ?? theme.palette.warning.main
72
+ }}
73
+ >
74
+ {startLabel}
75
+ </Typography>
76
+ <Switch
77
+ checked={controlChecked}
78
+ value={value}
79
+ onChange={onChangeLocal}
80
+ {...rest}
81
+ />
82
+ <Typography
83
+ onClick={() => setControlChecked(true)}
84
+ sx={{
85
+ cursor: 'pointer',
86
+ color: controlChecked
87
+ ? (theme) => activeColor ?? theme.palette.warning.main
88
+ : undefined
89
+ }}
90
+ >
91
+ {endLabel}
92
+ </Typography>
93
+ </Stack>
94
+ );
95
+ }