@snack-uikit/toggles 0.8.1 → 0.9.0
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/CHANGELOG.md +11 -0
- package/README.md +15 -15
- package/dist/components/Checkbox/Checkbox.d.ts +0 -4
- package/dist/components/Checkbox/Checkbox.js +2 -3
- package/dist/components/Favorite/Favorite.d.ts +1 -5
- package/dist/components/Favorite/Favorite.js +6 -7
- package/dist/components/Radio/Radio.d.ts +0 -4
- package/dist/components/Radio/Radio.js +2 -3
- package/dist/components/Switch/Switch.d.ts +0 -4
- package/dist/components/Switch/Switch.js +2 -3
- package/dist/components/ToggleGroup/ToggleGroup.d.ts +3 -6
- package/dist/components/ToggleGroup/ToggleGroup.js +3 -4
- package/dist/components/ToggleGroup/hooks.js +2 -2
- package/dist/components/TogglePrivate/TogglePrivate.d.ts +2 -3
- package/dist/components/TogglePrivate/TogglePrivate.js +2 -2
- package/dist/constants.d.ts +16 -16
- package/dist/constants.js +16 -20
- package/dist/context.d.ts +1 -1
- package/dist/context.js +2 -2
- package/dist/types.d.ts +6 -2
- package/dist/utils.d.ts +1 -2
- package/dist/utils.js +3 -3
- package/package.json +3 -3
- package/src/components/Checkbox/Checkbox.tsx +2 -4
- package/src/components/Favorite/Favorite.tsx +8 -10
- package/src/components/Radio/Radio.tsx +2 -4
- package/src/components/Switch/Switch.tsx +2 -4
- package/src/components/ToggleGroup/ToggleGroup.tsx +5 -7
- package/src/components/ToggleGroup/hooks.ts +2 -2
- package/src/components/TogglePrivate/TogglePrivate.tsx +3 -3
- package/src/constants.ts +16 -16
- package/src/context.ts +3 -2
- package/src/types.ts +7 -2
- package/src/utils.ts +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# 0.9.0 (2023-12-14)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### BREAKING CHANGES
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
* **FF-3729:** replace enum with unions ([910db4a](https://github.com/cloud-ru-tech/snack-uikit/commit/910db4aa8231ccbc58e538e5c5c1f461b1dec275))
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
## 0.8.1 (2023-12-06)
|
|
7
18
|
|
|
8
19
|
### Only dependencies have been changed
|
package/README.md
CHANGED
|
@@ -42,14 +42,17 @@
|
|
|
42
42
|
|
|
43
43
|
// ITEMS -some array with ToggleCardProps
|
|
44
44
|
|
|
45
|
-
...
|
|
45
|
+
// ...
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
{
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
47
|
+
return (
|
|
48
|
+
<ToggleGroup selectionMode='single' value={value} onChange={setValue}>
|
|
49
|
+
{ITEMS.map(props => (
|
|
50
|
+
<ToggleCard key={props.id} {...props} />
|
|
51
|
+
))}
|
|
52
|
+
</ToggleGroup>
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// ...
|
|
53
56
|
|
|
54
57
|
return (
|
|
55
58
|
<>
|
|
@@ -66,9 +69,6 @@ return (
|
|
|
66
69
|
```
|
|
67
70
|
|
|
68
71
|
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
|
|
72
72
|
[//]: DOCUMENTATION_SECTION_START
|
|
73
73
|
[//]: THIS_SECTION_IS_AUTOGENERATED_PLEASE_DONT_EDIT_IT
|
|
74
74
|
## Checkbox
|
|
@@ -88,7 +88,7 @@ return (
|
|
|
88
88
|
| onBlur | `FocusEventHandler<HTMLInputElement>` | - | Колбек потери фокуса |
|
|
89
89
|
| onFocus | `FocusEventHandler<HTMLInputElement>` | - | Колбек приобретения фокуса |
|
|
90
90
|
| className | `string` | - | CSS-класс |
|
|
91
|
-
| size | enum Size: `"s"`, `"m"` |
|
|
91
|
+
| size | enum Size: `"s"`, `"m"` | m | Размер |
|
|
92
92
|
| inputRef | `RefObject<HTMLInputElement>` | - | |
|
|
93
93
|
| indeterminate | `boolean` | - | Состояние частичного выбора |
|
|
94
94
|
| indeterminateDefault | `boolean` | - | Состояние частичного выбора по-умолчанию |
|
|
@@ -109,7 +109,7 @@ return (
|
|
|
109
109
|
| onBlur | `FocusEventHandler<HTMLInputElement>` | - | Колбек потери фокуса |
|
|
110
110
|
| onFocus | `FocusEventHandler<HTMLInputElement>` | - | Колбек приобретения фокуса |
|
|
111
111
|
| className | `string` | - | CSS-класс |
|
|
112
|
-
| size | enum Size: `"s"`, `"m"` |
|
|
112
|
+
| size | enum Size: `"s"`, `"m"` | m | Размер |
|
|
113
113
|
| inputRef | `RefObject<HTMLInputElement>` | - | |
|
|
114
114
|
| showIcon | `boolean` | - | Показывать ли иконку в переключателе |
|
|
115
115
|
## Radio
|
|
@@ -147,9 +147,9 @@ return (
|
|
|
147
147
|
| onBlur | `FocusEventHandler<HTMLInputElement>` | - | Колбек потери фокуса |
|
|
148
148
|
| onFocus | `FocusEventHandler<HTMLInputElement>` | - | Колбек приобретения фокуса |
|
|
149
149
|
| className | `string` | - | CSS-класс |
|
|
150
|
-
| size | enum Size: `"s"`, `"m"` |
|
|
150
|
+
| size | enum Size: `"s"`, `"m"` | m | Размер |
|
|
151
151
|
| inputRef | `RefObject<HTMLInputElement>` | - | |
|
|
152
|
-
| icon | enum FavoriteIcon: `"star"`, `"heart"` |
|
|
152
|
+
| icon | enum FavoriteIcon: `"star"`, `"heart"` | heart | Иконка |
|
|
153
153
|
## ToggleGroup
|
|
154
154
|
### Props
|
|
155
155
|
| name | type | default value | description |
|
|
@@ -158,7 +158,7 @@ return (
|
|
|
158
158
|
| defaultValue | `string \| string[]` | - | Начальное состояние |
|
|
159
159
|
| value | `string \| string[]` | - | Controlled состояние |
|
|
160
160
|
| onChange | `((value: string) => void) \| ((value: string[]) => void)` | - | Controlled обработчик измения состояния |
|
|
161
|
-
| selectionMode |
|
|
161
|
+
| selectionMode | "single" \| "multiple" | single | Режим выбора |
|
|
162
162
|
## useToggleGroup
|
|
163
163
|
### Props
|
|
164
164
|
| name | type | default value | description |
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Size } from '../../constants';
|
|
2
1
|
import { ToggleProps } from '../../types';
|
|
3
2
|
export type CheckboxProps = ToggleProps & {
|
|
4
3
|
/** Состояние частичного выбора */
|
|
@@ -7,6 +6,3 @@ export type CheckboxProps = ToggleProps & {
|
|
|
7
6
|
indeterminateDefault?: boolean;
|
|
8
7
|
};
|
|
9
8
|
export declare function Checkbox({ inputRef, indeterminate: indeterminateProp, indeterminateDefault, onChange: onChangeProp, 'data-test-id': dataTestId, size, ...restProps }: CheckboxProps): import("react/jsx-runtime").JSX.Element;
|
|
10
|
-
export declare namespace Checkbox {
|
|
11
|
-
var sizes: typeof Size;
|
|
12
|
-
}
|
|
@@ -14,12 +14,12 @@ import mergeRefs from 'merge-refs';
|
|
|
14
14
|
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
15
15
|
import { useUncontrolledProp } from 'uncontrollable';
|
|
16
16
|
import { CheckSVG, MinusSVG } from '@snack-uikit/icons';
|
|
17
|
-
import {
|
|
17
|
+
import { SIZE } from '../../constants';
|
|
18
18
|
import { getIconSize, getVisualStateAttributes } from '../../utils';
|
|
19
19
|
import { TogglePrivate } from '../TogglePrivate';
|
|
20
20
|
import styles from './styles.module.css';
|
|
21
21
|
export function Checkbox(_a) {
|
|
22
|
-
var { inputRef, indeterminate: indeterminateProp, indeterminateDefault, onChange: onChangeProp, 'data-test-id': dataTestId, size =
|
|
22
|
+
var { inputRef, indeterminate: indeterminateProp, indeterminateDefault, onChange: onChangeProp, 'data-test-id': dataTestId, size = SIZE.M } = _a, restProps = __rest(_a, ["inputRef", "indeterminate", "indeterminateDefault", "onChange", 'data-test-id', "size"]);
|
|
23
23
|
const localRef = useRef(null);
|
|
24
24
|
const ref = mergeRefs(localRef, inputRef);
|
|
25
25
|
const [indeterminate, setIndeterminate] = useUncontrolledProp(indeterminateProp, indeterminateDefault);
|
|
@@ -38,4 +38,3 @@ export function Checkbox(_a) {
|
|
|
38
38
|
return (_jsxs("div", Object.assign({ className: styles.container }, data, { children: [_jsx("div", Object.assign({ className: styles.box }, data)), _jsx("div", Object.assign({ className: styles.icon }, data, { children: indeterminate ? _jsx(MinusSVG, { size: iconSize }) : _jsx(CheckSVG, { size: iconSize }) }))] })));
|
|
39
39
|
} })));
|
|
40
40
|
}
|
|
41
|
-
Checkbox.sizes = Size;
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import { FavoriteIcon,
|
|
2
|
-
import { ToggleProps } from '../../types';
|
|
1
|
+
import { FavoriteIcon, ToggleProps } from '../../types';
|
|
3
2
|
export type FavoriteProps = Omit<ToggleProps, 'disabled'> & {
|
|
4
3
|
/** Иконка */
|
|
5
4
|
icon?: FavoriteIcon;
|
|
6
5
|
};
|
|
7
6
|
export declare function Favorite({ inputRef, 'data-test-id': dataTestId, size, icon, ...restProps }: FavoriteProps): import("react/jsx-runtime").JSX.Element;
|
|
8
|
-
export declare namespace Favorite {
|
|
9
|
-
var sizes: typeof Size;
|
|
10
|
-
}
|
|
@@ -12,20 +12,20 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
12
12
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
13
|
import { useMemo } from 'react';
|
|
14
14
|
import { HeartFilledSVG, HeartSVG, StarFilledSVG, StarSVG } from '@snack-uikit/icons';
|
|
15
|
-
import {
|
|
15
|
+
import { FAVORITE_ICON, SIZE } from '../../constants';
|
|
16
16
|
import { getIconSize, getVisualStateAttributes } from '../../utils';
|
|
17
17
|
import { TogglePrivate } from '../TogglePrivate';
|
|
18
18
|
import styles from './styles.module.css';
|
|
19
19
|
const CHECKED_ICONS = {
|
|
20
|
-
[
|
|
21
|
-
[
|
|
20
|
+
[FAVORITE_ICON.Star]: StarFilledSVG,
|
|
21
|
+
[FAVORITE_ICON.Heart]: HeartFilledSVG,
|
|
22
22
|
};
|
|
23
23
|
const UNCHECKED_ICONS = {
|
|
24
|
-
[
|
|
25
|
-
[
|
|
24
|
+
[FAVORITE_ICON.Star]: StarSVG,
|
|
25
|
+
[FAVORITE_ICON.Heart]: HeartSVG,
|
|
26
26
|
};
|
|
27
27
|
export function Favorite(_a) {
|
|
28
|
-
var { inputRef, 'data-test-id': dataTestId, size =
|
|
28
|
+
var { inputRef, 'data-test-id': dataTestId, size = SIZE.M, icon = FAVORITE_ICON.Heart } = _a, restProps = __rest(_a, ["inputRef", 'data-test-id', "size", "icon"]);
|
|
29
29
|
const iconSize = useMemo(() => getIconSize(size), [size]);
|
|
30
30
|
return (_jsx(TogglePrivate, Object.assign({}, restProps, { "data-test-id": dataTestId, ref: inputRef, size: size, render: function Favorite(visualState) {
|
|
31
31
|
const Icon = visualState.checked ? CHECKED_ICONS[icon] : UNCHECKED_ICONS[icon];
|
|
@@ -33,4 +33,3 @@ export function Favorite(_a) {
|
|
|
33
33
|
return (_jsx("div", Object.assign({ className: styles.container }, data, { children: _jsx(Icon, Object.assign({ className: styles.icon }, data, { size: iconSize })) })));
|
|
34
34
|
} })));
|
|
35
35
|
}
|
|
36
|
-
Favorite.sizes = Size;
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import { Size } from '../../constants';
|
|
2
1
|
import { ToggleProps } from '../../types';
|
|
3
2
|
export type RadioProps = ToggleProps;
|
|
4
3
|
export declare function Radio({ inputRef, 'data-test-id': dataTestId, ...restProps }: RadioProps): import("react/jsx-runtime").JSX.Element;
|
|
5
|
-
export declare namespace Radio {
|
|
6
|
-
var sizes: typeof Size;
|
|
7
|
-
}
|
|
@@ -10,15 +10,14 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
10
10
|
return t;
|
|
11
11
|
};
|
|
12
12
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
-
import {
|
|
13
|
+
import { MODE } from '../../constants';
|
|
14
14
|
import { getVisualStateAttributes } from '../../utils';
|
|
15
15
|
import { TogglePrivate } from '../TogglePrivate';
|
|
16
16
|
import styles from './styles.module.css';
|
|
17
17
|
export function Radio(_a) {
|
|
18
18
|
var { inputRef, 'data-test-id': dataTestId } = _a, restProps = __rest(_a, ["inputRef", 'data-test-id']);
|
|
19
|
-
return (_jsx(TogglePrivate, Object.assign({}, restProps, { "data-test-id": dataTestId, mode:
|
|
19
|
+
return (_jsx(TogglePrivate, Object.assign({}, restProps, { "data-test-id": dataTestId, mode: MODE.Radio, ref: inputRef, render: function Radio(visualState) {
|
|
20
20
|
const data = getVisualStateAttributes(visualState);
|
|
21
21
|
return (_jsx("div", Object.assign({ className: styles.container }, data, { children: _jsx("div", Object.assign({ className: styles.box }, data, { children: _jsx("div", Object.assign({ className: styles.flag }, data)) })) })));
|
|
22
22
|
} })));
|
|
23
23
|
}
|
|
24
|
-
Radio.sizes = Size;
|
|
@@ -1,10 +1,6 @@
|
|
|
1
|
-
import { Size } from '../../constants';
|
|
2
1
|
import { ToggleProps } from '../../types';
|
|
3
2
|
export type SwitchProps = ToggleProps & {
|
|
4
3
|
/** Показывать ли иконку в переключателе */
|
|
5
4
|
showIcon?: boolean;
|
|
6
5
|
};
|
|
7
6
|
export declare function Switch({ inputRef, 'data-test-id': dataTestId, showIcon, size, ...restProps }: SwitchProps): import("react/jsx-runtime").JSX.Element;
|
|
8
|
-
export declare namespace Switch {
|
|
9
|
-
var sizes: typeof Size;
|
|
10
|
-
}
|
|
@@ -12,16 +12,15 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
12
12
|
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
|
|
13
13
|
import { useMemo } from 'react';
|
|
14
14
|
import { CheckSVG, CrossSVG } from '@snack-uikit/icons';
|
|
15
|
-
import {
|
|
15
|
+
import { SIZE } from '../../constants';
|
|
16
16
|
import { getIconSize, getVisualStateAttributes } from '../../utils';
|
|
17
17
|
import { TogglePrivate } from '../TogglePrivate';
|
|
18
18
|
import styles from './styles.module.css';
|
|
19
19
|
export function Switch(_a) {
|
|
20
|
-
var { inputRef, 'data-test-id': dataTestId, showIcon, size =
|
|
20
|
+
var { inputRef, 'data-test-id': dataTestId, showIcon, size = SIZE.M } = _a, restProps = __rest(_a, ["inputRef", 'data-test-id', "showIcon", "size"]);
|
|
21
21
|
const iconSize = useMemo(() => getIconSize(size), [size]);
|
|
22
22
|
return (_jsx(TogglePrivate, Object.assign({}, restProps, { size: size, "data-test-id": dataTestId, ref: inputRef, render: function Switch(visualState) {
|
|
23
23
|
const data = getVisualStateAttributes(visualState);
|
|
24
24
|
return (_jsxs("div", Object.assign({ className: styles.container }, data, { children: [_jsx("div", Object.assign({ className: styles.box }, data)), _jsxs("div", Object.assign({ className: styles.containerFlag }, data, { children: [_jsx("div", Object.assign({ className: styles.flag }, data)), showIcon && (_jsxs(_Fragment, { children: [_jsx("div", Object.assign({ className: styles.flag_icon_off }, data, { children: _jsx(CrossSVG, { size: iconSize }) })), _jsx("div", Object.assign({ className: styles.flag_icon_on }, data, { children: _jsx(CheckSVG, { size: iconSize }) }))] }))] }))] })));
|
|
25
25
|
} })));
|
|
26
26
|
}
|
|
27
|
-
Switch.sizes = Size;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ReactNode } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { SELECTION_MODE } from '../../constants';
|
|
3
3
|
export type ToggleGroupProps = {
|
|
4
4
|
/** Вложенный контент */
|
|
5
5
|
children: ReactNode;
|
|
@@ -11,7 +11,7 @@ export type ToggleGroupProps = {
|
|
|
11
11
|
/** Controlled обработчик измения состояния */
|
|
12
12
|
onChange?(value: (string | undefined) | string): void;
|
|
13
13
|
/** Режим выбора */
|
|
14
|
-
selectionMode?:
|
|
14
|
+
selectionMode?: typeof SELECTION_MODE.Single;
|
|
15
15
|
} | {
|
|
16
16
|
/** Начальное состояние */
|
|
17
17
|
defaultValue?: string[];
|
|
@@ -20,9 +20,6 @@ export type ToggleGroupProps = {
|
|
|
20
20
|
/** Controlled обработчик измения состояния */
|
|
21
21
|
onChange?(value: (string[] | undefined) | string[]): void;
|
|
22
22
|
/** Режим выбора */
|
|
23
|
-
selectionMode?:
|
|
23
|
+
selectionMode?: typeof SELECTION_MODE.Multiple;
|
|
24
24
|
});
|
|
25
25
|
export declare function ToggleGroup({ children, value: valueProp, onChange: onChangeProp, selectionMode, defaultValue, }: ToggleGroupProps): import("react/jsx-runtime").JSX.Element;
|
|
26
|
-
export declare namespace ToggleGroup {
|
|
27
|
-
var selectionModes: typeof SelectionMode;
|
|
28
|
-
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useCallback } from 'react';
|
|
3
3
|
import { useUncontrolledProp } from 'uncontrollable';
|
|
4
|
-
import {
|
|
4
|
+
import { SELECTION_MODE } from '../../constants';
|
|
5
5
|
import { ToggleGroupContext } from '../../context';
|
|
6
|
-
export function ToggleGroup({ children, value: valueProp, onChange: onChangeProp, selectionMode =
|
|
6
|
+
export function ToggleGroup({ children, value: valueProp, onChange: onChangeProp, selectionMode = SELECTION_MODE.Single, defaultValue, }) {
|
|
7
7
|
const [value, setValue] = useUncontrolledProp(valueProp, defaultValue, cb => {
|
|
8
8
|
onChangeProp === null || onChangeProp === void 0 ? void 0 : onChangeProp(cb(value));
|
|
9
9
|
});
|
|
10
10
|
const onChange = useCallback((newValue) => {
|
|
11
|
-
if (selectionMode ===
|
|
11
|
+
if (selectionMode === SELECTION_MODE.Single) {
|
|
12
12
|
return setValue((oldValue) => {
|
|
13
13
|
if (newValue !== oldValue) {
|
|
14
14
|
return newValue;
|
|
@@ -28,4 +28,3 @@ export function ToggleGroup({ children, value: valueProp, onChange: onChangeProp
|
|
|
28
28
|
}, [selectionMode, setValue]);
|
|
29
29
|
return (_jsx(ToggleGroupContext.Provider, Object.assign({ value: { value, onChange, selectionMode } }, { children: children })));
|
|
30
30
|
}
|
|
31
|
-
ToggleGroup.selectionModes = SelectionMode;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useCallback, useMemo } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { SELECTION_MODE } from '../../constants';
|
|
3
3
|
import { useToggleGroupContext } from '../../context';
|
|
4
4
|
export function useToggleGroup({ value: itemValue }) {
|
|
5
5
|
const { value, onChange, selectionMode } = useToggleGroupContext();
|
|
@@ -18,6 +18,6 @@ export function useToggleGroup({ value: itemValue }) {
|
|
|
18
18
|
return {
|
|
19
19
|
isChecked,
|
|
20
20
|
handleClick,
|
|
21
|
-
multipleSelection: selectionMode ===
|
|
21
|
+
multipleSelection: selectionMode === SELECTION_MODE.Multiple,
|
|
22
22
|
};
|
|
23
23
|
}
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import { Mode, Size } from '../../constants';
|
|
3
2
|
export declare const TogglePrivate: import("react").ForwardRefExoticComponent<{
|
|
4
3
|
'data-test-id'?: string | undefined;
|
|
5
4
|
} & import("react").AriaAttributes & {
|
|
6
5
|
className?: string | undefined;
|
|
7
|
-
size?: Size | undefined;
|
|
6
|
+
size?: import("../../types").Size | undefined;
|
|
8
7
|
render: (visualState: import("../../types").InputVisualState) => import("react").ReactNode;
|
|
9
|
-
mode?: Mode | undefined;
|
|
8
|
+
mode?: import("../../types").Mode | undefined;
|
|
10
9
|
id?: string | undefined;
|
|
11
10
|
value?: string | undefined;
|
|
12
11
|
name?: string | undefined;
|
|
@@ -13,11 +13,11 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
|
13
13
|
import cn from 'classnames';
|
|
14
14
|
import { forwardRef, useState } from 'react';
|
|
15
15
|
import { useUncontrolledProp } from 'uncontrollable';
|
|
16
|
-
import {
|
|
16
|
+
import { MODE, SIZE } from '../../constants';
|
|
17
17
|
import { getVisualStateAttributes } from '../../utils';
|
|
18
18
|
import styles from './styles.module.css';
|
|
19
19
|
export const TogglePrivate = forwardRef(function TogglePrivate(_a, ref) {
|
|
20
|
-
var { render, checked: checkedProp, defaultChecked, onChange, onBlur, onFocus, disabled, className, size =
|
|
20
|
+
var { render, checked: checkedProp, defaultChecked, onChange, onBlur, onFocus, disabled, className, size = SIZE.M, mode = MODE.Checkbox, 'data-test-id': testId } = _a, otherProps = __rest(_a, ["render", "checked", "defaultChecked", "onChange", "onBlur", "onFocus", "disabled", "className", "size", "mode", 'data-test-id']);
|
|
21
21
|
const [checked, setChecked] = useUncontrolledProp(checkedProp, Boolean(defaultChecked), onChange);
|
|
22
22
|
const [focusVisible, setFocusVisible] = useState(false);
|
|
23
23
|
const [hover, setHover] = useState(false);
|
package/dist/constants.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
export declare
|
|
2
|
-
Checkbox
|
|
3
|
-
Radio
|
|
4
|
-
}
|
|
5
|
-
export declare
|
|
6
|
-
S
|
|
7
|
-
M
|
|
8
|
-
}
|
|
9
|
-
export declare
|
|
10
|
-
Star
|
|
11
|
-
Heart
|
|
12
|
-
}
|
|
13
|
-
export declare
|
|
14
|
-
Single
|
|
15
|
-
Multiple
|
|
16
|
-
}
|
|
1
|
+
export declare const MODE: {
|
|
2
|
+
readonly Checkbox: "checkbox";
|
|
3
|
+
readonly Radio: "radio";
|
|
4
|
+
};
|
|
5
|
+
export declare const SIZE: {
|
|
6
|
+
readonly S: "s";
|
|
7
|
+
readonly M: "m";
|
|
8
|
+
};
|
|
9
|
+
export declare const FAVORITE_ICON: {
|
|
10
|
+
readonly Star: "star";
|
|
11
|
+
readonly Heart: "heart";
|
|
12
|
+
};
|
|
13
|
+
export declare const SELECTION_MODE: {
|
|
14
|
+
readonly Single: "single";
|
|
15
|
+
readonly Multiple: "multiple";
|
|
16
|
+
};
|
package/dist/constants.js
CHANGED
|
@@ -1,20 +1,16 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
(function (SelectionMode) {
|
|
18
|
-
SelectionMode["Single"] = "single";
|
|
19
|
-
SelectionMode["Multiple"] = "multiple";
|
|
20
|
-
})(SelectionMode || (SelectionMode = {}));
|
|
1
|
+
export const MODE = {
|
|
2
|
+
Checkbox: 'checkbox',
|
|
3
|
+
Radio: 'radio',
|
|
4
|
+
};
|
|
5
|
+
export const SIZE = {
|
|
6
|
+
S: 's',
|
|
7
|
+
M: 'm',
|
|
8
|
+
};
|
|
9
|
+
export const FAVORITE_ICON = {
|
|
10
|
+
Star: 'star',
|
|
11
|
+
Heart: 'heart',
|
|
12
|
+
};
|
|
13
|
+
export const SELECTION_MODE = {
|
|
14
|
+
Single: 'single',
|
|
15
|
+
Multiple: 'multiple',
|
|
16
|
+
};
|
package/dist/context.d.ts
CHANGED
package/dist/context.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { createContext, useContext } from 'react';
|
|
2
|
-
import {
|
|
2
|
+
import { SELECTION_MODE } from './constants';
|
|
3
3
|
export const ToggleGroupContext = createContext({
|
|
4
4
|
value: undefined,
|
|
5
5
|
onChange: undefined,
|
|
6
|
-
selectionMode:
|
|
6
|
+
selectionMode: SELECTION_MODE.Single,
|
|
7
7
|
});
|
|
8
8
|
export const useToggleGroupContext = () => useContext(ToggleGroupContext);
|
package/dist/types.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { FocusEventHandler, MouseEventHandler, ReactNode, RefObject } from 'react';
|
|
2
|
-
import { WithSupportProps } from '@snack-uikit/utils';
|
|
3
|
-
import {
|
|
2
|
+
import { ValueOf, WithSupportProps } from '@snack-uikit/utils';
|
|
3
|
+
import { FAVORITE_ICON, MODE, SELECTION_MODE, SIZE } from './constants';
|
|
4
|
+
export type Size = ValueOf<typeof SIZE>;
|
|
5
|
+
export type Mode = ValueOf<typeof MODE>;
|
|
6
|
+
export type SelectionMode = ValueOf<typeof SELECTION_MODE>;
|
|
7
|
+
export type FavoriteIcon = ValueOf<typeof FAVORITE_ICON>;
|
|
4
8
|
export type InputVisualState = {
|
|
5
9
|
focusVisible: boolean;
|
|
6
10
|
disabled: boolean;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Size } from './
|
|
2
|
-
import { DataAttributes } from './types';
|
|
1
|
+
import { DataAttributes, Size } from './types';
|
|
3
2
|
export declare function getVisualStateAttributes(state: Record<string, string | boolean>): DataAttributes;
|
|
4
3
|
export declare function getIconSize(size?: Size): 16 | 24;
|
package/dist/utils.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SIZE } from './constants';
|
|
2
2
|
export function getVisualStateAttributes(state) {
|
|
3
3
|
return Object.entries(state).reduce(function (result, [name, value]) {
|
|
4
4
|
result[`data-${name.toLowerCase()}`] = value;
|
|
@@ -7,10 +7,10 @@ export function getVisualStateAttributes(state) {
|
|
|
7
7
|
}
|
|
8
8
|
export function getIconSize(size) {
|
|
9
9
|
switch (size) {
|
|
10
|
-
case
|
|
10
|
+
case SIZE.S: {
|
|
11
11
|
return 16;
|
|
12
12
|
}
|
|
13
|
-
case
|
|
13
|
+
case SIZE.M:
|
|
14
14
|
default: {
|
|
15
15
|
return 24;
|
|
16
16
|
}
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
6
|
"title": "Toggles",
|
|
7
|
-
"version": "0.
|
|
7
|
+
"version": "0.9.0",
|
|
8
8
|
"sideEffects": [
|
|
9
9
|
"*.css",
|
|
10
10
|
"*.woff",
|
|
@@ -33,10 +33,10 @@
|
|
|
33
33
|
"scripts": {},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@snack-uikit/icons": "0.19.0",
|
|
36
|
-
"@snack-uikit/utils": "3.
|
|
36
|
+
"@snack-uikit/utils": "3.2.0",
|
|
37
37
|
"classnames": "2.3.2",
|
|
38
38
|
"merge-refs": "1.2.1",
|
|
39
39
|
"uncontrollable": "8.0.0"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "bd39c5e674f3b91b0e2487782a04b15034cf3d8b"
|
|
42
42
|
}
|
|
@@ -4,7 +4,7 @@ import { useUncontrolledProp } from 'uncontrollable';
|
|
|
4
4
|
|
|
5
5
|
import { CheckSVG, MinusSVG } from '@snack-uikit/icons';
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import { SIZE } from '../../constants';
|
|
8
8
|
import { ToggleProps } from '../../types';
|
|
9
9
|
import { getIconSize, getVisualStateAttributes } from '../../utils';
|
|
10
10
|
import { TogglePrivate } from '../TogglePrivate';
|
|
@@ -23,7 +23,7 @@ export function Checkbox({
|
|
|
23
23
|
indeterminateDefault,
|
|
24
24
|
onChange: onChangeProp,
|
|
25
25
|
'data-test-id': dataTestId,
|
|
26
|
-
size =
|
|
26
|
+
size = SIZE.M,
|
|
27
27
|
...restProps
|
|
28
28
|
}: CheckboxProps) {
|
|
29
29
|
const localRef = useRef<HTMLInputElement>(null);
|
|
@@ -68,5 +68,3 @@ export function Checkbox({
|
|
|
68
68
|
/>
|
|
69
69
|
);
|
|
70
70
|
}
|
|
71
|
-
|
|
72
|
-
Checkbox.sizes = Size;
|
|
@@ -2,20 +2,20 @@ import { useMemo } from 'react';
|
|
|
2
2
|
|
|
3
3
|
import { HeartFilledSVG, HeartSVG, StarFilledSVG, StarSVG } from '@snack-uikit/icons';
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import { ToggleProps } from '../../types';
|
|
5
|
+
import { FAVORITE_ICON, SIZE } from '../../constants';
|
|
6
|
+
import { FavoriteIcon, ToggleProps } from '../../types';
|
|
7
7
|
import { getIconSize, getVisualStateAttributes } from '../../utils';
|
|
8
8
|
import { TogglePrivate } from '../TogglePrivate';
|
|
9
9
|
import styles from './styles.module.scss';
|
|
10
10
|
|
|
11
11
|
const CHECKED_ICONS = {
|
|
12
|
-
[
|
|
13
|
-
[
|
|
12
|
+
[FAVORITE_ICON.Star]: StarFilledSVG,
|
|
13
|
+
[FAVORITE_ICON.Heart]: HeartFilledSVG,
|
|
14
14
|
};
|
|
15
15
|
|
|
16
16
|
const UNCHECKED_ICONS = {
|
|
17
|
-
[
|
|
18
|
-
[
|
|
17
|
+
[FAVORITE_ICON.Star]: StarSVG,
|
|
18
|
+
[FAVORITE_ICON.Heart]: HeartSVG,
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
export type FavoriteProps = Omit<ToggleProps, 'disabled'> & {
|
|
@@ -26,8 +26,8 @@ export type FavoriteProps = Omit<ToggleProps, 'disabled'> & {
|
|
|
26
26
|
export function Favorite({
|
|
27
27
|
inputRef,
|
|
28
28
|
'data-test-id': dataTestId,
|
|
29
|
-
size =
|
|
30
|
-
icon =
|
|
29
|
+
size = SIZE.M,
|
|
30
|
+
icon = FAVORITE_ICON.Heart,
|
|
31
31
|
...restProps
|
|
32
32
|
}: FavoriteProps) {
|
|
33
33
|
const iconSize = useMemo(() => getIconSize(size), [size]);
|
|
@@ -50,5 +50,3 @@ export function Favorite({
|
|
|
50
50
|
/>
|
|
51
51
|
);
|
|
52
52
|
}
|
|
53
|
-
|
|
54
|
-
Favorite.sizes = Size;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MODE } from '../../constants';
|
|
2
2
|
import { ToggleProps } from '../../types';
|
|
3
3
|
import { getVisualStateAttributes } from '../../utils';
|
|
4
4
|
import { TogglePrivate } from '../TogglePrivate';
|
|
@@ -11,7 +11,7 @@ export function Radio({ inputRef, 'data-test-id': dataTestId, ...restProps }: Ra
|
|
|
11
11
|
<TogglePrivate
|
|
12
12
|
{...restProps}
|
|
13
13
|
data-test-id={dataTestId}
|
|
14
|
-
mode={
|
|
14
|
+
mode={MODE.Radio}
|
|
15
15
|
ref={inputRef}
|
|
16
16
|
render={function Radio(visualState) {
|
|
17
17
|
const data = getVisualStateAttributes(visualState);
|
|
@@ -26,5 +26,3 @@ export function Radio({ inputRef, 'data-test-id': dataTestId, ...restProps }: Ra
|
|
|
26
26
|
/>
|
|
27
27
|
);
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
Radio.sizes = Size;
|
|
@@ -2,7 +2,7 @@ import { useMemo } from 'react';
|
|
|
2
2
|
|
|
3
3
|
import { CheckSVG, CrossSVG } from '@snack-uikit/icons';
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { SIZE } from '../../constants';
|
|
6
6
|
import { ToggleProps } from '../../types';
|
|
7
7
|
import { getIconSize, getVisualStateAttributes } from '../../utils';
|
|
8
8
|
import { TogglePrivate } from '../TogglePrivate';
|
|
@@ -13,7 +13,7 @@ export type SwitchProps = ToggleProps & {
|
|
|
13
13
|
showIcon?: boolean;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
export function Switch({ inputRef, 'data-test-id': dataTestId, showIcon, size =
|
|
16
|
+
export function Switch({ inputRef, 'data-test-id': dataTestId, showIcon, size = SIZE.M, ...restProps }: SwitchProps) {
|
|
17
17
|
const iconSize = useMemo(() => getIconSize(size), [size]);
|
|
18
18
|
|
|
19
19
|
return (
|
|
@@ -46,5 +46,3 @@ export function Switch({ inputRef, 'data-test-id': dataTestId, showIcon, size =
|
|
|
46
46
|
/>
|
|
47
47
|
);
|
|
48
48
|
}
|
|
49
|
-
|
|
50
|
-
Switch.sizes = Size;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ReactNode, useCallback } from 'react';
|
|
2
2
|
import { useUncontrolledProp } from 'uncontrollable';
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { SELECTION_MODE } from '../../constants';
|
|
5
5
|
import { ToggleGroupContext } from '../../context';
|
|
6
6
|
|
|
7
7
|
export type ToggleGroupProps = {
|
|
@@ -16,7 +16,7 @@ export type ToggleGroupProps = {
|
|
|
16
16
|
/** Controlled обработчик измения состояния */
|
|
17
17
|
onChange?(value: (string | undefined) | string): void;
|
|
18
18
|
/** Режим выбора */
|
|
19
|
-
selectionMode?:
|
|
19
|
+
selectionMode?: typeof SELECTION_MODE.Single;
|
|
20
20
|
}
|
|
21
21
|
| {
|
|
22
22
|
/** Начальное состояние */
|
|
@@ -26,7 +26,7 @@ export type ToggleGroupProps = {
|
|
|
26
26
|
/** Controlled обработчик измения состояния */
|
|
27
27
|
onChange?(value: (string[] | undefined) | string[]): void;
|
|
28
28
|
/** Режим выбора */
|
|
29
|
-
selectionMode?:
|
|
29
|
+
selectionMode?: typeof SELECTION_MODE.Multiple;
|
|
30
30
|
}
|
|
31
31
|
);
|
|
32
32
|
|
|
@@ -34,7 +34,7 @@ export function ToggleGroup({
|
|
|
34
34
|
children,
|
|
35
35
|
value: valueProp,
|
|
36
36
|
onChange: onChangeProp,
|
|
37
|
-
selectionMode =
|
|
37
|
+
selectionMode = SELECTION_MODE.Single,
|
|
38
38
|
defaultValue,
|
|
39
39
|
}: ToggleGroupProps) {
|
|
40
40
|
const [value, setValue] = useUncontrolledProp<string | string[] | undefined>(valueProp, defaultValue, cb => {
|
|
@@ -43,7 +43,7 @@ export function ToggleGroup({
|
|
|
43
43
|
|
|
44
44
|
const onChange = useCallback(
|
|
45
45
|
(newValue: string) => {
|
|
46
|
-
if (selectionMode ===
|
|
46
|
+
if (selectionMode === SELECTION_MODE.Single) {
|
|
47
47
|
return setValue((oldValue: string | undefined) => {
|
|
48
48
|
if (newValue !== oldValue) {
|
|
49
49
|
return newValue;
|
|
@@ -72,5 +72,3 @@ export function ToggleGroup({
|
|
|
72
72
|
<ToggleGroupContext.Provider value={{ value, onChange, selectionMode }}>{children}</ToggleGroupContext.Provider>
|
|
73
73
|
);
|
|
74
74
|
}
|
|
75
|
-
|
|
76
|
-
ToggleGroup.selectionModes = SelectionMode;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useCallback, useMemo } from 'react';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { SELECTION_MODE } from '../../constants';
|
|
4
4
|
import { useToggleGroupContext } from '../../context';
|
|
5
5
|
|
|
6
6
|
type UseToggleGroupProps = {
|
|
@@ -29,6 +29,6 @@ export function useToggleGroup({ value: itemValue }: UseToggleGroupProps) {
|
|
|
29
29
|
return {
|
|
30
30
|
isChecked,
|
|
31
31
|
handleClick,
|
|
32
|
-
multipleSelection: selectionMode ===
|
|
32
|
+
multipleSelection: selectionMode === SELECTION_MODE.Multiple,
|
|
33
33
|
};
|
|
34
34
|
}
|
|
@@ -2,7 +2,7 @@ import cn from 'classnames';
|
|
|
2
2
|
import { forwardRef, useState } from 'react';
|
|
3
3
|
import { useUncontrolledProp } from 'uncontrollable';
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { MODE, SIZE } from '../../constants';
|
|
6
6
|
import { TogglePrivateProps } from '../../types';
|
|
7
7
|
import { getVisualStateAttributes } from '../../utils';
|
|
8
8
|
import styles from './styles.module.scss';
|
|
@@ -17,8 +17,8 @@ export const TogglePrivate = forwardRef<HTMLInputElement, TogglePrivateProps>(fu
|
|
|
17
17
|
onFocus,
|
|
18
18
|
disabled,
|
|
19
19
|
className,
|
|
20
|
-
size =
|
|
21
|
-
mode =
|
|
20
|
+
size = SIZE.M,
|
|
21
|
+
mode = MODE.Checkbox,
|
|
22
22
|
'data-test-id': testId,
|
|
23
23
|
...otherProps
|
|
24
24
|
},
|
package/src/constants.ts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
export
|
|
2
|
-
Checkbox
|
|
3
|
-
Radio
|
|
4
|
-
}
|
|
1
|
+
export const MODE = {
|
|
2
|
+
Checkbox: 'checkbox',
|
|
3
|
+
Radio: 'radio',
|
|
4
|
+
} as const;
|
|
5
5
|
|
|
6
|
-
export
|
|
7
|
-
S
|
|
8
|
-
M
|
|
9
|
-
}
|
|
6
|
+
export const SIZE = {
|
|
7
|
+
S: 's',
|
|
8
|
+
M: 'm',
|
|
9
|
+
} as const;
|
|
10
10
|
|
|
11
|
-
export
|
|
12
|
-
Star
|
|
13
|
-
Heart
|
|
14
|
-
}
|
|
11
|
+
export const FAVORITE_ICON = {
|
|
12
|
+
Star: 'star',
|
|
13
|
+
Heart: 'heart',
|
|
14
|
+
} as const;
|
|
15
15
|
|
|
16
|
-
export
|
|
17
|
-
Single
|
|
18
|
-
Multiple
|
|
19
|
-
}
|
|
16
|
+
export const SELECTION_MODE = {
|
|
17
|
+
Single: 'single',
|
|
18
|
+
Multiple: 'multiple',
|
|
19
|
+
} as const;
|
package/src/context.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createContext, useContext } from 'react';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { SELECTION_MODE } from './constants';
|
|
4
|
+
import { SelectionMode } from './types';
|
|
4
5
|
|
|
5
6
|
type ToggleGroupContextType = {
|
|
6
7
|
value: string | undefined | string[];
|
|
@@ -11,7 +12,7 @@ type ToggleGroupContextType = {
|
|
|
11
12
|
export const ToggleGroupContext = createContext<ToggleGroupContextType>({
|
|
12
13
|
value: undefined,
|
|
13
14
|
onChange: undefined,
|
|
14
|
-
selectionMode:
|
|
15
|
+
selectionMode: SELECTION_MODE.Single,
|
|
15
16
|
});
|
|
16
17
|
|
|
17
18
|
export const useToggleGroupContext = () => useContext(ToggleGroupContext);
|
package/src/types.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { FocusEventHandler, MouseEventHandler, ReactNode, RefObject } from 'react';
|
|
2
2
|
|
|
3
|
-
import { WithSupportProps } from '@snack-uikit/utils';
|
|
3
|
+
import { ValueOf, WithSupportProps } from '@snack-uikit/utils';
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import { FAVORITE_ICON, MODE, SELECTION_MODE, SIZE } from './constants';
|
|
6
|
+
|
|
7
|
+
export type Size = ValueOf<typeof SIZE>;
|
|
8
|
+
export type Mode = ValueOf<typeof MODE>;
|
|
9
|
+
export type SelectionMode = ValueOf<typeof SELECTION_MODE>;
|
|
10
|
+
export type FavoriteIcon = ValueOf<typeof FAVORITE_ICON>;
|
|
6
11
|
|
|
7
12
|
export type InputVisualState = {
|
|
8
13
|
focusVisible: boolean;
|
package/src/utils.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { DataAttributes } from './types';
|
|
1
|
+
import { SIZE } from './constants';
|
|
2
|
+
import { DataAttributes, Size } from './types';
|
|
3
3
|
|
|
4
4
|
export function getVisualStateAttributes(state: Record<string, string | boolean>) {
|
|
5
5
|
return Object.entries(state).reduce<DataAttributes>(function (result, [name, value]) {
|
|
@@ -10,10 +10,10 @@ export function getVisualStateAttributes(state: Record<string, string | boolean>
|
|
|
10
10
|
|
|
11
11
|
export function getIconSize(size?: Size) {
|
|
12
12
|
switch (size) {
|
|
13
|
-
case
|
|
13
|
+
case SIZE.S: {
|
|
14
14
|
return 16;
|
|
15
15
|
}
|
|
16
|
-
case
|
|
16
|
+
case SIZE.M:
|
|
17
17
|
default: {
|
|
18
18
|
return 24;
|
|
19
19
|
}
|