@true-engineering/true-react-common-ui-kit 3.40.0 → 3.41.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.
@@ -1,184 +1,184 @@
1
- import { FC, useState } from 'react';
2
- import clsx from 'clsx';
3
- import {
4
- addDataTestId,
5
- applyAction,
6
- stopPropagation,
7
- addDataAttributes,
8
- } from '@true-engineering/true-react-platform-helpers';
9
- import {
10
- autoUpdate,
11
- useFloating,
12
- useClick,
13
- useDismiss,
14
- useHover,
15
- useInteractions,
16
- Placement,
17
- Middleware,
18
- FloatingPortal,
19
- offset,
20
- flip,
21
- useTransitionStatus,
22
- OffsetOptions,
23
- UseHoverProps,
24
- safePolygon,
25
- useFocus,
26
- } from '@floating-ui/react';
27
- import { ICommonProps, IRenderNode } from '../../types';
28
- import { DEFAULT_OFFSET } from './constants';
29
- import {
30
- IPopupEventType,
31
- IReferenceProps,
32
- IWithPopupChildrenProps,
33
- IWithPopupToggleEvent,
34
- IWithPopupTriggerProps,
35
- } from './types';
36
- import { useStyles, IWithPopupStyles } from './WithPopup.styles';
37
-
38
- export interface IWithPopupProps extends ICommonProps<IWithPopupStyles> {
39
- trigger: IRenderNode<IWithPopupTriggerProps>;
40
- children: IRenderNode<IWithPopupChildrenProps>;
41
- middlewares?: Middleware[];
42
- /** @default eventType === 'click' ? 'bottom-end' : 'top' */
43
- placement?: Placement;
44
- /** @default 'click' */
45
- eventType?: IPopupEventType;
46
- /** @default 0 */
47
- hoverDelay?: UseHoverProps['delay'];
48
- /** @default 6 */
49
- popupOffset?: OffsetOptions;
50
- /** @default true */
51
- shouldStopPropagation?: boolean;
52
- /** @default false */
53
- shouldHideOnScroll?: boolean;
54
- /** @default true */
55
- shouldRenderInBody?: boolean;
56
- /** Должен ли WithPopup рендерить свой div-wrapper в качестве триггера
57
- * @default true, if eventType === hover */
58
- isTriggerWrapped?: boolean;
59
- /** @default true */
60
- canBeFlipped?: boolean;
61
- /** @default false */
62
- isDisabled?: boolean;
63
- onToggle?: (isActive: boolean, event?: IWithPopupToggleEvent) => void;
64
- }
65
-
66
- export const WithPopup: FC<IWithPopupProps> = ({
67
- trigger,
68
- children,
69
- middlewares = [],
70
- eventType = 'click',
71
- placement = eventType === 'click' ? 'bottom-end' : 'top',
72
- hoverDelay = 0,
73
- popupOffset = DEFAULT_OFFSET,
74
- shouldStopPropagation = true,
75
- shouldHideOnScroll = false,
76
- shouldRenderInBody = true,
77
- canBeFlipped = true,
78
- isTriggerWrapped = eventType === 'hover',
79
- isDisabled = false,
80
- tweakStyles,
81
- data,
82
- testId,
83
- onToggle,
84
- }) => {
85
- const classes = useStyles({ theme: tweakStyles });
86
-
87
- const [isOpen, setIsOpen] = useState(false);
88
-
89
- const handleToggle = (isActive: boolean, event?: IWithPopupToggleEvent) => {
90
- event?.stopPropagation();
91
- if (!isDisabled) {
92
- onToggle?.(isActive, event);
93
- setIsOpen(isActive);
94
- }
95
- };
96
-
97
- const handleClose = (event?: IWithPopupToggleEvent) => {
98
- handleToggle(false, event);
99
- };
100
-
101
- const { refs, floatingStyles, context } = useFloating({
102
- open: isOpen,
103
- middleware: [
104
- offset(popupOffset),
105
- canBeFlipped && flip({ fallbackAxisSideDirection: 'start' }),
106
- ...middlewares,
107
- ],
108
- whileElementsMounted: autoUpdate,
109
- placement,
110
- onOpenChange: handleToggle,
111
- });
112
-
113
- const hover = useHover(context, {
114
- enabled: eventType === 'hover',
115
- delay: typeof hoverDelay === 'number' ? { open: hoverDelay, close: 0 } : hoverDelay,
116
- handleClose: safePolygon(),
117
- });
118
-
119
- const focus = useFocus(context, { enabled: eventType === 'hover' });
120
-
121
- const click = useClick(context, { enabled: eventType === 'click' });
122
-
123
- const dismiss = useDismiss(context, {
124
- enabled: eventType === 'click',
125
- ancestorScroll: shouldHideOnScroll,
126
- });
127
-
128
- const { getFloatingProps, getReferenceProps } = useInteractions([hover, click, focus, dismiss]);
129
-
130
- const { isMounted, status } = useTransitionStatus(context, { duration: { close: 500 } });
131
-
132
- const referenceProps: IReferenceProps = getReferenceProps({
133
- ref: refs.setReference,
134
- ...(shouldStopPropagation && {
135
- onClick: stopPropagation,
136
- }),
137
- });
138
-
139
- const triggerElement = applyAction(trigger, {
140
- referenceProps: !isTriggerWrapped ? referenceProps : undefined,
141
- triggerProps: {
142
- isActive: isOpen,
143
- isDisabled,
144
- ...(!isTriggerWrapped && { data, testId, ...referenceProps }),
145
- },
146
- });
147
-
148
- return (
149
- <>
150
- {isTriggerWrapped ? (
151
- <div
152
- className={clsx(classes.trigger, {
153
- [classes.clickable]: eventType === 'click',
154
- [classes.disabled]: isDisabled,
155
- [classes.active]: isOpen,
156
- })}
157
- {...referenceProps}
158
- {...addDataTestId(testId)}
159
- {...addDataAttributes(data)}
160
- >
161
- {triggerElement}
162
- </div>
163
- ) : (
164
- triggerElement
165
- )}
166
- {isMounted && (
167
- <FloatingPortal
168
- root={!shouldRenderInBody ? (refs.reference.current as HTMLDivElement) : undefined}
169
- >
170
- <div
171
- style={floatingStyles}
172
- className={classes.popup}
173
- ref={refs.setFloating}
174
- {...getFloatingProps()}
175
- >
176
- <div className={classes[`dropdown-${status}`]}>
177
- {applyAction(children, { onClose: handleClose })}
178
- </div>
179
- </div>
180
- </FloatingPortal>
181
- )}
182
- </>
183
- );
184
- };
1
+ import { FC, useState } from 'react';
2
+ import clsx from 'clsx';
3
+ import {
4
+ addDataTestId,
5
+ applyAction,
6
+ stopPropagation,
7
+ addDataAttributes,
8
+ } from '@true-engineering/true-react-platform-helpers';
9
+ import {
10
+ autoUpdate,
11
+ useFloating,
12
+ useClick,
13
+ useDismiss,
14
+ useHover,
15
+ useInteractions,
16
+ Placement,
17
+ Middleware,
18
+ FloatingPortal,
19
+ offset,
20
+ flip,
21
+ useTransitionStatus,
22
+ OffsetOptions,
23
+ UseHoverProps,
24
+ safePolygon,
25
+ useFocus,
26
+ } from '@floating-ui/react';
27
+ import { ICommonProps, IRenderNode } from '../../types';
28
+ import { DEFAULT_OFFSET } from './constants';
29
+ import {
30
+ IPopupEventType,
31
+ IReferenceProps,
32
+ IWithPopupChildrenProps,
33
+ IWithPopupToggleEvent,
34
+ IWithPopupTriggerProps,
35
+ } from './types';
36
+ import { useStyles, IWithPopupStyles } from './WithPopup.styles';
37
+
38
+ export interface IWithPopupProps extends ICommonProps<IWithPopupStyles> {
39
+ trigger: IRenderNode<IWithPopupTriggerProps>;
40
+ children: IRenderNode<IWithPopupChildrenProps>;
41
+ middlewares?: Middleware[];
42
+ /** @default eventType === 'click' ? 'bottom-end' : 'top' */
43
+ placement?: Placement;
44
+ /** @default 'click' */
45
+ eventType?: IPopupEventType;
46
+ /** @default 0 */
47
+ hoverDelay?: UseHoverProps['delay'];
48
+ /** @default 6 */
49
+ popupOffset?: OffsetOptions;
50
+ /** @default true */
51
+ shouldStopPropagation?: boolean;
52
+ /** @default false */
53
+ shouldHideOnScroll?: boolean;
54
+ /** @default true */
55
+ shouldRenderInBody?: boolean;
56
+ /** Должен ли WithPopup рендерить свой div-wrapper в качестве триггера
57
+ * @default true, if eventType === hover */
58
+ isTriggerWrapped?: boolean;
59
+ /** @default true */
60
+ canBeFlipped?: boolean;
61
+ /** @default false */
62
+ isDisabled?: boolean;
63
+ onToggle?: (isActive: boolean, event?: IWithPopupToggleEvent) => void;
64
+ }
65
+
66
+ export const WithPopup: FC<IWithPopupProps> = ({
67
+ trigger,
68
+ children,
69
+ middlewares = [],
70
+ eventType = 'click',
71
+ placement = eventType === 'click' ? 'bottom-end' : 'top',
72
+ hoverDelay = 0,
73
+ popupOffset = DEFAULT_OFFSET,
74
+ shouldStopPropagation = true,
75
+ shouldHideOnScroll = false,
76
+ shouldRenderInBody = true,
77
+ canBeFlipped = true,
78
+ isTriggerWrapped = eventType === 'hover',
79
+ isDisabled = false,
80
+ tweakStyles,
81
+ data,
82
+ testId,
83
+ onToggle,
84
+ }) => {
85
+ const classes = useStyles({ theme: tweakStyles });
86
+
87
+ const [isOpen, setIsOpen] = useState(false);
88
+
89
+ const handleToggle = (isActive: boolean, event?: IWithPopupToggleEvent) => {
90
+ event?.stopPropagation();
91
+ if (!isDisabled) {
92
+ onToggle?.(isActive, event);
93
+ setIsOpen(isActive);
94
+ }
95
+ };
96
+
97
+ const handleClose = (event?: IWithPopupToggleEvent) => {
98
+ handleToggle(false, event);
99
+ };
100
+
101
+ const { refs, floatingStyles, context } = useFloating({
102
+ open: isOpen,
103
+ middleware: [
104
+ offset(popupOffset),
105
+ canBeFlipped && flip({ fallbackAxisSideDirection: 'start' }),
106
+ ...middlewares,
107
+ ],
108
+ whileElementsMounted: autoUpdate,
109
+ placement,
110
+ onOpenChange: handleToggle,
111
+ });
112
+
113
+ const hover = useHover(context, {
114
+ enabled: eventType === 'hover',
115
+ delay: typeof hoverDelay === 'number' ? { open: hoverDelay, close: 0 } : hoverDelay,
116
+ handleClose: safePolygon(),
117
+ });
118
+
119
+ const focus = useFocus(context, { enabled: eventType === 'hover' });
120
+
121
+ const click = useClick(context, { enabled: eventType === 'click' });
122
+
123
+ const dismiss = useDismiss(context, {
124
+ enabled: eventType === 'click',
125
+ ancestorScroll: shouldHideOnScroll,
126
+ });
127
+
128
+ const { getFloatingProps, getReferenceProps } = useInteractions([hover, click, focus, dismiss]);
129
+
130
+ const { isMounted, status } = useTransitionStatus(context, { duration: { close: 500 } });
131
+
132
+ const referenceProps: IReferenceProps = getReferenceProps({
133
+ ref: refs.setReference,
134
+ ...(shouldStopPropagation && {
135
+ onClick: stopPropagation,
136
+ }),
137
+ });
138
+
139
+ const triggerElement = applyAction(trigger, {
140
+ referenceProps: !isTriggerWrapped ? referenceProps : undefined,
141
+ triggerProps: {
142
+ isActive: isOpen,
143
+ isDisabled,
144
+ ...(!isTriggerWrapped && { data, testId, ...referenceProps }),
145
+ },
146
+ });
147
+
148
+ return (
149
+ <>
150
+ {isTriggerWrapped ? (
151
+ <div
152
+ className={clsx(classes.trigger, {
153
+ [classes.clickable]: eventType === 'click',
154
+ [classes.disabled]: isDisabled,
155
+ [classes.active]: isOpen,
156
+ })}
157
+ {...referenceProps}
158
+ {...addDataTestId(testId)}
159
+ {...addDataAttributes(data)}
160
+ >
161
+ {triggerElement}
162
+ </div>
163
+ ) : (
164
+ triggerElement
165
+ )}
166
+ {isMounted && (
167
+ <FloatingPortal
168
+ root={!shouldRenderInBody ? (refs.reference.current as HTMLDivElement) : undefined}
169
+ >
170
+ <div
171
+ style={floatingStyles}
172
+ className={classes.popup}
173
+ ref={refs.setFloating}
174
+ {...getFloatingProps()}
175
+ >
176
+ <div className={classes[`dropdown-${status}`]}>
177
+ {applyAction(children, { onClose: handleClose })}
178
+ </div>
179
+ </div>
180
+ </FloatingPortal>
181
+ )}
182
+ </>
183
+ );
184
+ };
@@ -1,56 +1,56 @@
1
- import { Meta, StoryObj } from '@storybook/react';
2
- import { WithTooltip } from './WithTooltip';
3
-
4
- const positions = [
5
- 'top',
6
- 'bottom',
7
- 'right',
8
- 'left',
9
- 'top-start',
10
- 'top-end',
11
- 'bottom-start',
12
- 'bottom-end',
13
- 'right-start',
14
- 'right-end',
15
- 'left-start',
16
- 'left-end',
17
- ];
18
-
19
- const meta: Meta<typeof WithTooltip> = {
20
- title: 'Data Display/WithTooltip',
21
- component: WithTooltip,
22
- argTypes: {
23
- placement: { control: 'select', options: positions },
24
- tooltipView: { control: 'inline-radio', options: ['tooltip', 'hint'] },
25
- tooltipType: { control: 'inline-radio', options: ['info', 'error'] },
26
- eventType: { control: 'inline-radio', options: ['hover', 'click'] },
27
- },
28
- };
29
- export default meta;
30
- type IStory = StoryObj<typeof WithTooltip>;
31
-
32
- export const Default: IStory = {
33
- args: {
34
- children: 'Наведи на меня - появится тултип',
35
- tooltipText: 'Текст тултипа',
36
- placement: 'top',
37
- tooltipView: 'tooltip',
38
- tooltipType: 'info',
39
- eventType: 'hover',
40
- isDisabled: false,
41
- shouldRenderInBody: true,
42
- },
43
- render: (args) => (
44
- <div
45
- style={{
46
- display: 'flex',
47
- alignItems: 'center',
48
- justifyContent: 'center',
49
- width: 600,
50
- height: 400,
51
- }}
52
- >
53
- <WithTooltip {...args} />
54
- </div>
55
- ),
56
- };
1
+ import { Meta, StoryObj } from '@storybook/react';
2
+ import { WithTooltip } from './WithTooltip';
3
+
4
+ const positions = [
5
+ 'top',
6
+ 'bottom',
7
+ 'right',
8
+ 'left',
9
+ 'top-start',
10
+ 'top-end',
11
+ 'bottom-start',
12
+ 'bottom-end',
13
+ 'right-start',
14
+ 'right-end',
15
+ 'left-start',
16
+ 'left-end',
17
+ ];
18
+
19
+ const meta: Meta<typeof WithTooltip> = {
20
+ title: 'Data Display/WithTooltip',
21
+ component: WithTooltip,
22
+ argTypes: {
23
+ placement: { control: 'select', options: positions },
24
+ tooltipView: { control: 'inline-radio', options: ['tooltip', 'hint'] },
25
+ tooltipType: { control: 'inline-radio', options: ['info', 'error'] },
26
+ eventType: { control: 'inline-radio', options: ['hover', 'click'] },
27
+ },
28
+ };
29
+ export default meta;
30
+ type IStory = StoryObj<typeof WithTooltip>;
31
+
32
+ export const Default: IStory = {
33
+ args: {
34
+ children: 'Наведи на меня - появится тултип',
35
+ tooltipText: 'Текст тултипа',
36
+ placement: 'top',
37
+ tooltipView: 'tooltip',
38
+ tooltipType: 'info',
39
+ eventType: 'hover',
40
+ isDisabled: false,
41
+ shouldRenderInBody: true,
42
+ },
43
+ render: (args) => (
44
+ <div
45
+ style={{
46
+ display: 'flex',
47
+ alignItems: 'center',
48
+ justifyContent: 'center',
49
+ width: 600,
50
+ height: 400,
51
+ }}
52
+ >
53
+ <WithTooltip {...args} />
54
+ </div>
55
+ ),
56
+ };
@@ -1,7 +1,7 @@
1
- import { ITooltipStyles } from '../Tooltip';
2
- import { IWithPopupStyles } from '../WithPopup';
3
-
4
- export interface IWithTooltipStyles {
5
- tweakWithPopup?: IWithPopupStyles;
6
- tweakTooltip?: ITooltipStyles;
7
- }
1
+ import { ITooltipStyles } from '../Tooltip';
2
+ import { IWithPopupStyles } from '../WithPopup';
3
+
4
+ export interface IWithTooltipStyles {
5
+ tweakWithPopup?: IWithPopupStyles;
6
+ tweakTooltip?: ITooltipStyles;
7
+ }
@@ -1,67 +1,67 @@
1
- import { FC } from 'react';
2
- import { getTestId, isReactNodeNotEmpty } from '@true-engineering/true-react-platform-helpers';
3
- import { useTweakStyles } from '../../hooks';
4
- import { ICommonProps } from '../../types';
5
- import { ITooltipProps, Tooltip } from '../Tooltip';
6
- import { IWithPopupProps, WithPopup } from '../WithPopup';
7
- import { IWithTooltipStyles } from './WithTooltip.styles';
8
-
9
- export interface IWithTooltipProps
10
- extends Omit<
11
- IWithPopupProps,
12
- 'trigger' | 'children' | 'placement' | 'eventType' | 'isTriggerWrapped' | 'tweakStyles'
13
- >,
14
- ICommonProps<IWithTooltipStyles> {
15
- children: IWithPopupProps['trigger'];
16
- /** @default 'hover' */
17
- eventType?: IWithPopupProps['eventType'];
18
- /** @default 'top' */
19
- placement?: IWithPopupProps['placement'];
20
- tooltipText?: ITooltipProps['text'];
21
- /** @default 'tooltip' */
22
- tooltipView?: ITooltipProps['view'];
23
- /** @default 'info' */
24
- tooltipType?: ITooltipProps['type'];
25
- }
26
-
27
- export const WithTooltip: FC<IWithTooltipProps> = ({
28
- children,
29
- eventType = 'hover',
30
- placement = 'top',
31
- tooltipText,
32
- tooltipView = 'tooltip',
33
- tooltipType = 'info',
34
- isDisabled = false,
35
- tweakStyles,
36
- ...restProps
37
- }) => {
38
- const tweakWithPopupStyles = useTweakStyles({
39
- tweakStyles,
40
- className: 'tweakWithPopup',
41
- });
42
-
43
- const tweakTooltipStyles = useTweakStyles({
44
- tweakStyles,
45
- className: 'tweakTooltip',
46
- });
47
-
48
- return (
49
- <WithPopup
50
- trigger={children}
51
- placement={placement}
52
- eventType={eventType}
53
- isTriggerWrapped
54
- isDisabled={isDisabled || !isReactNodeNotEmpty(tooltipText)}
55
- tweakStyles={tweakWithPopupStyles}
56
- {...restProps}
57
- >
58
- <Tooltip
59
- view={tooltipView}
60
- type={tooltipType}
61
- text={tooltipText}
62
- tweakStyles={tweakTooltipStyles}
63
- testId={getTestId(restProps.testId, 'tooltip')}
64
- />
65
- </WithPopup>
66
- );
67
- };
1
+ import { FC } from 'react';
2
+ import { getTestId, isReactNodeNotEmpty } from '@true-engineering/true-react-platform-helpers';
3
+ import { useTweakStyles } from '../../hooks';
4
+ import { ICommonProps } from '../../types';
5
+ import { ITooltipProps, Tooltip } from '../Tooltip';
6
+ import { IWithPopupProps, WithPopup } from '../WithPopup';
7
+ import { IWithTooltipStyles } from './WithTooltip.styles';
8
+
9
+ export interface IWithTooltipProps
10
+ extends Omit<
11
+ IWithPopupProps,
12
+ 'trigger' | 'children' | 'placement' | 'eventType' | 'isTriggerWrapped' | 'tweakStyles'
13
+ >,
14
+ ICommonProps<IWithTooltipStyles> {
15
+ children: IWithPopupProps['trigger'];
16
+ /** @default 'hover' */
17
+ eventType?: IWithPopupProps['eventType'];
18
+ /** @default 'top' */
19
+ placement?: IWithPopupProps['placement'];
20
+ tooltipText?: ITooltipProps['text'];
21
+ /** @default 'tooltip' */
22
+ tooltipView?: ITooltipProps['view'];
23
+ /** @default 'info' */
24
+ tooltipType?: ITooltipProps['type'];
25
+ }
26
+
27
+ export const WithTooltip: FC<IWithTooltipProps> = ({
28
+ children,
29
+ eventType = 'hover',
30
+ placement = 'top',
31
+ tooltipText,
32
+ tooltipView = 'tooltip',
33
+ tooltipType = 'info',
34
+ isDisabled = false,
35
+ tweakStyles,
36
+ ...restProps
37
+ }) => {
38
+ const tweakWithPopupStyles = useTweakStyles({
39
+ tweakStyles,
40
+ className: 'tweakWithPopup',
41
+ });
42
+
43
+ const tweakTooltipStyles = useTweakStyles({
44
+ tweakStyles,
45
+ className: 'tweakTooltip',
46
+ });
47
+
48
+ return (
49
+ <WithPopup
50
+ trigger={children}
51
+ placement={placement}
52
+ eventType={eventType}
53
+ isTriggerWrapped
54
+ isDisabled={isDisabled || !isReactNodeNotEmpty(tooltipText)}
55
+ tweakStyles={tweakWithPopupStyles}
56
+ {...restProps}
57
+ >
58
+ <Tooltip
59
+ view={tooltipView}
60
+ type={tooltipType}
61
+ text={tooltipText}
62
+ tweakStyles={tweakTooltipStyles}
63
+ testId={getTestId(restProps.testId, 'tooltip')}
64
+ />
65
+ </WithPopup>
66
+ );
67
+ };
@@ -1,2 +1,2 @@
1
- export * from './WithTooltip';
2
- export type { IWithTooltipStyles } from './WithTooltip.styles';
1
+ export * from './WithTooltip';
2
+ export type { IWithTooltipStyles } from './WithTooltip.styles';