@etsoo/materialui 1.0.34 → 1.0.36
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/__tests__/SwitchAnt.tsx +23 -0
- package/lib/SwitchAnt.d.ts +1 -1
- package/lib/SwitchAnt.js +5 -3
- package/package.json +1 -1
- package/src/SwitchAnt.tsx +7 -3
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { getByText, render } from '@testing-library/react';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { act } from 'react-dom/test-utils';
|
|
4
|
+
import { SwitchAnt } from '../src/SwitchAnt';
|
|
5
|
+
|
|
6
|
+
it('SwitchAnt Tests', () => {
|
|
7
|
+
const onChange = jest.fn((event: React.ChangeEvent<HTMLInputElement>) =>
|
|
8
|
+
expect(event.target.checked).toBeTruthy()
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
// Render component
|
|
12
|
+
const { baseElement } = render(
|
|
13
|
+
<SwitchAnt startLabel="No" endLabel="Yes" onChange={onChange} />
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
const yes = getByText(baseElement, 'Yes');
|
|
17
|
+
|
|
18
|
+
act(() => {
|
|
19
|
+
yes.click();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
expect(onChange).toBeCalled();
|
|
23
|
+
});
|
package/lib/SwitchAnt.d.ts
CHANGED
package/lib/SwitchAnt.js
CHANGED
|
@@ -12,6 +12,8 @@ export function SwitchAnt(props) {
|
|
|
12
12
|
const { activeColor, checked, defaultChecked, defaultValue, endLabel, startLabel, onChange, value = 'true', ...rest } = props;
|
|
13
13
|
// Checked state
|
|
14
14
|
const [controlChecked, setControlChecked] = React.useState((_a = checked !== null && checked !== void 0 ? checked : defaultChecked) !== null && _a !== void 0 ? _a : defaultValue == value);
|
|
15
|
+
// Ref
|
|
16
|
+
const ref = React.useRef(null);
|
|
15
17
|
React.useEffect(() => {
|
|
16
18
|
if (checked)
|
|
17
19
|
setControlChecked(checked);
|
|
@@ -24,14 +26,14 @@ export function SwitchAnt(props) {
|
|
|
24
26
|
};
|
|
25
27
|
// Layout
|
|
26
28
|
return (React.createElement(Stack, { direction: "row", spacing: 1, alignItems: "center" },
|
|
27
|
-
React.createElement(Typography, { onClick: () =>
|
|
29
|
+
React.createElement(Typography, { onClick: () => { var _a; return controlChecked && ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.click()); }, sx: {
|
|
28
30
|
cursor: 'pointer',
|
|
29
31
|
color: controlChecked
|
|
30
32
|
? undefined
|
|
31
33
|
: (theme) => activeColor !== null && activeColor !== void 0 ? activeColor : theme.palette.warning.main
|
|
32
34
|
} }, startLabel),
|
|
33
|
-
React.createElement(Switch, { checked: controlChecked, value: value, onChange: onChangeLocal, ...rest }),
|
|
34
|
-
React.createElement(Typography, { onClick: () =>
|
|
35
|
+
React.createElement(Switch, { checked: controlChecked, inputRef: ref, value: value, onChange: onChangeLocal, ...rest }),
|
|
36
|
+
React.createElement(Typography, { onClick: () => { var _a; return !controlChecked && ((_a = ref.current) === null || _a === void 0 ? void 0 : _a.click()); }, sx: {
|
|
35
37
|
cursor: 'pointer',
|
|
36
38
|
color: controlChecked
|
|
37
39
|
? (theme) => activeColor !== null && activeColor !== void 0 ? activeColor : theme.palette.warning.main
|
package/package.json
CHANGED
package/src/SwitchAnt.tsx
CHANGED
|
@@ -7,7 +7,7 @@ import React from 'react';
|
|
|
7
7
|
*/
|
|
8
8
|
export interface SwitchAntProps extends SwitchProps {
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* Active color
|
|
11
11
|
*/
|
|
12
12
|
activeColor?: string;
|
|
13
13
|
|
|
@@ -46,6 +46,9 @@ export function SwitchAnt(props: SwitchAntProps) {
|
|
|
46
46
|
checked ?? defaultChecked ?? defaultValue == value
|
|
47
47
|
);
|
|
48
48
|
|
|
49
|
+
// Ref
|
|
50
|
+
const ref = React.useRef<HTMLButtonElement>(null);
|
|
51
|
+
|
|
49
52
|
React.useEffect(() => {
|
|
50
53
|
if (checked) setControlChecked(checked);
|
|
51
54
|
}, [checked]);
|
|
@@ -63,7 +66,7 @@ export function SwitchAnt(props: SwitchAntProps) {
|
|
|
63
66
|
return (
|
|
64
67
|
<Stack direction="row" spacing={1} alignItems="center">
|
|
65
68
|
<Typography
|
|
66
|
-
onClick={() =>
|
|
69
|
+
onClick={() => controlChecked && ref.current?.click()}
|
|
67
70
|
sx={{
|
|
68
71
|
cursor: 'pointer',
|
|
69
72
|
color: controlChecked
|
|
@@ -75,12 +78,13 @@ export function SwitchAnt(props: SwitchAntProps) {
|
|
|
75
78
|
</Typography>
|
|
76
79
|
<Switch
|
|
77
80
|
checked={controlChecked}
|
|
81
|
+
inputRef={ref}
|
|
78
82
|
value={value}
|
|
79
83
|
onChange={onChangeLocal}
|
|
80
84
|
{...rest}
|
|
81
85
|
/>
|
|
82
86
|
<Typography
|
|
83
|
-
onClick={() =>
|
|
87
|
+
onClick={() => !controlChecked && ref.current?.click()}
|
|
84
88
|
sx={{
|
|
85
89
|
cursor: 'pointer',
|
|
86
90
|
color: controlChecked
|