@pushwoosh/dumb-components 1.1.224 → 1.1.225
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/Switch/Switch.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type ReactElement } from 'react';
|
|
2
2
|
import type { SwitchProps } from './types';
|
|
3
3
|
/**
|
|
4
|
-
* Toggle switch for a boolean value, with an optional label on either side.
|
|
4
|
+
* Toggle switch for a boolean value, with an optional label on either side; a click anywhere flips it.
|
|
5
5
|
*
|
|
6
6
|
* @example
|
|
7
7
|
* <Switch enabled={on} onChange={() => setOn((v) => !v)}>Notifications</Switch>
|
package/Switch/Switch.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback } from 'react';
|
|
2
3
|
import { Container, Box, Circle, Label } from './styles';
|
|
3
4
|
/**
|
|
4
|
-
* Toggle switch for a boolean value, with an optional label on either side.
|
|
5
|
+
* Toggle switch for a boolean value, with an optional label on either side; a click anywhere flips it.
|
|
5
6
|
*
|
|
6
7
|
* @example
|
|
7
8
|
* <Switch enabled={on} onChange={() => setOn((v) => !v)}>Notifications</Switch>
|
|
@@ -13,12 +14,18 @@ export function Switch({
|
|
|
13
14
|
children,
|
|
14
15
|
onChange
|
|
15
16
|
}) {
|
|
17
|
+
const handleClick = useCallback(() => {
|
|
18
|
+
if (!isDisabled) {
|
|
19
|
+
onChange();
|
|
20
|
+
}
|
|
21
|
+
}, [isDisabled, onChange]);
|
|
16
22
|
return _jsxs(Container, {
|
|
17
23
|
labelPosition: labelPosition,
|
|
24
|
+
isDisabled: isDisabled,
|
|
25
|
+
onClick: handleClick,
|
|
18
26
|
children: [_jsx(Box, {
|
|
19
27
|
enabled: enabled,
|
|
20
28
|
isDisabled: isDisabled,
|
|
21
|
-
onClick: onChange,
|
|
22
29
|
children: _jsx(Circle, {
|
|
23
30
|
enabled: enabled,
|
|
24
31
|
isDisabled: isDisabled
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { fireEvent, render, screen } from '@testing-library/react';
|
|
3
|
+
import { Switch } from '../Switch';
|
|
4
|
+
describe('Switch', () => {
|
|
5
|
+
it('flips on a click on the label, not only on the toggle', () => {
|
|
6
|
+
const onChange = jest.fn();
|
|
7
|
+
render(_jsx(Switch, {
|
|
8
|
+
enabled: false,
|
|
9
|
+
onChange: onChange,
|
|
10
|
+
children: "Save inbox"
|
|
11
|
+
}));
|
|
12
|
+
fireEvent.click(screen.getByText('Save inbox'));
|
|
13
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
14
|
+
});
|
|
15
|
+
it('flips on a click on the toggle', () => {
|
|
16
|
+
const onChange = jest.fn();
|
|
17
|
+
const {
|
|
18
|
+
container
|
|
19
|
+
} = render(_jsx(Switch, {
|
|
20
|
+
enabled: true,
|
|
21
|
+
onChange: onChange,
|
|
22
|
+
children: "Save inbox"
|
|
23
|
+
}));
|
|
24
|
+
const box = container.querySelector('[tabindex="0"]');
|
|
25
|
+
if (!box) {
|
|
26
|
+
throw new Error('toggle not rendered');
|
|
27
|
+
}
|
|
28
|
+
fireEvent.click(box);
|
|
29
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
30
|
+
});
|
|
31
|
+
it('stays silent while disabled, label and toggle alike', () => {
|
|
32
|
+
const onChange = jest.fn();
|
|
33
|
+
const {
|
|
34
|
+
container
|
|
35
|
+
} = render(_jsx(Switch, {
|
|
36
|
+
isDisabled: true,
|
|
37
|
+
onChange: onChange,
|
|
38
|
+
children: "Save inbox"
|
|
39
|
+
}));
|
|
40
|
+
fireEvent.click(screen.getByText('Save inbox'));
|
|
41
|
+
fireEvent.click(container.querySelector('[tabindex="0"]'));
|
|
42
|
+
expect(onChange).not.toHaveBeenCalled();
|
|
43
|
+
});
|
|
44
|
+
});
|
package/Switch/styles.js
CHANGED
|
@@ -37,9 +37,11 @@ const cssActive = css(["&:active{background:", ";border:2px solid ", ";}"], Colo
|
|
|
37
37
|
export const Container = styled.div.withConfig({
|
|
38
38
|
displayName: "Container",
|
|
39
39
|
componentId: "sc-csdrg2-0"
|
|
40
|
-
})(["display:flex;align-items:center;flex-direction:", ";"], ({
|
|
40
|
+
})(["display:flex;align-items:center;flex-direction:", ";cursor:", ";"], ({
|
|
41
41
|
labelPosition
|
|
42
|
-
}) => labelPosition === 'left' ? 'row-reverse' : 'row'
|
|
42
|
+
}) => labelPosition === 'left' ? 'row-reverse' : 'row', ({
|
|
43
|
+
isDisabled
|
|
44
|
+
}) => isDisabled ? 'not-allowed' : 'pointer');
|
|
43
45
|
export const Box = styled.div.attrs({
|
|
44
46
|
tabIndex: '0'
|
|
45
47
|
}).withConfig({
|
package/Switch/types.d.ts
CHANGED
|
@@ -12,9 +12,10 @@ export interface SwitchProps {
|
|
|
12
12
|
/** Fires when the toggle is clicked; flip `enabled` in the handler. */
|
|
13
13
|
onChange: () => void;
|
|
14
14
|
}
|
|
15
|
-
/** Internal style
|
|
15
|
+
/** Internal style props carrying the label side and the disabled state. */
|
|
16
16
|
export interface AlignProps {
|
|
17
17
|
labelPosition?: string;
|
|
18
|
+
isDisabled?: boolean;
|
|
18
19
|
}
|
|
19
20
|
/** Internal style prop carrying the toggle state. */
|
|
20
21
|
export interface ComponentProps {
|