@true-engineering/true-react-common-ui-kit 3.8.1 → 3.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.
Files changed (122) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +10 -0
  3. package/dist/components/Checkbox/Checkbox.d.ts +2 -2
  4. package/dist/components/NewMoreMenu/NewMoreMenu.styles.d.ts +3 -1
  5. package/dist/components/ScrollIntoViewIfNeeded/ScrollIntoViewIfNeeded.d.ts +1 -1
  6. package/dist/components/Select/Select.d.ts +4 -4
  7. package/dist/components/Select/components/SelectList/SelectList.d.ts +5 -6
  8. package/dist/components/Select/components/SelectListItem/SelectListItem.d.ts +2 -2
  9. package/dist/components/WithPopup/WithPopup.styles.d.ts +1 -1
  10. package/dist/true-react-common-ui-kit.js +111 -86
  11. package/dist/true-react-common-ui-kit.js.map +1 -1
  12. package/dist/true-react-common-ui-kit.umd.cjs +111 -86
  13. package/dist/true-react-common-ui-kit.umd.cjs.map +1 -1
  14. package/package.json +1 -1
  15. package/src/components/AccountInfo/AccountInfo.stories.tsx +32 -32
  16. package/src/components/AccountInfo/AccountInfo.tsx +80 -80
  17. package/src/components/AddButton/AddButton.stories.tsx +21 -21
  18. package/src/components/AddButton/AddButton.tsx +52 -52
  19. package/src/components/Button/Button.stories.tsx +56 -56
  20. package/src/components/Button/Button.tsx +129 -129
  21. package/src/components/Checkbox/Checkbox.stories.tsx +28 -28
  22. package/src/components/Checkbox/Checkbox.tsx +7 -4
  23. package/src/components/CloseButton/CloseButton.tsx +34 -34
  24. package/src/components/Colors/Colors.stories.tsx +7 -7
  25. package/src/components/DateInput/DateInput.tsx +90 -90
  26. package/src/components/DateInput/constants.ts +2 -2
  27. package/src/components/DatePicker/DatePicker.tsx +308 -308
  28. package/src/components/Description/Description.stories.tsx +27 -27
  29. package/src/components/Description/Description.tsx +61 -61
  30. package/src/components/FiltersPane/FiltersPane.tsx +158 -158
  31. package/src/components/FiltersPane/components/Filter/Filter.tsx +203 -203
  32. package/src/components/FiltersPane/components/FilterValueView/FilterValueView.tsx +166 -166
  33. package/src/components/FiltersPane/components/FilterWithDates/FilterWithDates.tsx +210 -210
  34. package/src/components/FiltersPane/components/FilterWithPeriod/FilterWithPeriod.tsx +177 -177
  35. package/src/components/FiltersPane/components/FilterWrapper/FilterWrapper.tsx +167 -167
  36. package/src/components/Flag/Flag.stories.tsx +29 -29
  37. package/src/components/Flag/Flag.tsx +26 -26
  38. package/src/components/Flag/augment.d.ts +1 -1
  39. package/src/components/FlexibleTable/FlexibleTable.stories.tsx +267 -267
  40. package/src/components/FlexibleTable/FlexibleTable.styles.ts +110 -110
  41. package/src/components/FlexibleTable/FlexibleTable.tsx +271 -271
  42. package/src/components/FlexibleTable/components/FlexibleTableCell/FlexibleTableCell.styles.ts +38 -38
  43. package/src/components/FlexibleTable/components/FlexibleTableCell/FlexibleTableCell.tsx +83 -83
  44. package/src/components/FlexibleTable/components/FlexibleTableRow/FlexibleTableRow.styles.ts +25 -25
  45. package/src/components/FlexibleTable/components/FlexibleTableRow/FlexibleTableRow.tsx +196 -196
  46. package/src/components/FlexibleTable/helpers.ts +13 -13
  47. package/src/components/FlexibleTable/types.ts +52 -52
  48. package/src/components/Icon/Icon.stories.tsx +86 -86
  49. package/src/components/Icon/complexIcons/augment.d.ts +1 -1
  50. package/src/components/Icon/complexIcons/avatarGreen.svg +57 -57
  51. package/src/components/Icon/complexIcons/index.ts +1 -1
  52. package/src/components/IncrementInput/IncrementInput.tsx +105 -105
  53. package/src/components/Input/Input.tsx +297 -297
  54. package/src/components/Input/types.ts +32 -32
  55. package/src/components/List/List.stories.tsx +70 -70
  56. package/src/components/List/List.tsx +33 -33
  57. package/src/components/List/components/ListItem/ListItem.tsx +57 -57
  58. package/src/components/Modal/Modal.stories.tsx +105 -105
  59. package/src/components/Modal/Modal.tsx +196 -196
  60. package/src/components/MoreMenu/MoreMenu.styles.ts +68 -68
  61. package/src/components/MultiSelect/MultiSelect.stories.tsx +46 -46
  62. package/src/components/MultiSelect/MultiSelect.tsx +106 -106
  63. package/src/components/MultiSelect/components/MultiSelectInput/MultiSelectInput.tsx +53 -53
  64. package/src/components/MultiSelectList/MultiSelectList.tsx +461 -461
  65. package/src/components/NewMoreMenu/NewMoreMenu.styles.ts +5 -5
  66. package/src/components/NewMoreMenu/NewMoreMenu.tsx +15 -1
  67. package/src/components/Notification/Notification.stories.tsx +46 -46
  68. package/src/components/Notification/Notification.tsx +69 -69
  69. package/src/components/NumberInput/NumberInput.tsx +137 -137
  70. package/src/components/NumberInput/index.ts +1 -1
  71. package/src/components/PhoneInput/PhoneInput.tsx +214 -214
  72. package/src/components/PhoneInput/components/PhoneInputCountryList/PhoneInputCountryList.tsx +155 -155
  73. package/src/components/PhoneInput/types.ts +16 -16
  74. package/src/components/RadioButton/RadioButton.stories.tsx +46 -46
  75. package/src/components/RadioButton/RadioButton.tsx +57 -57
  76. package/src/components/ScrollIntoViewIfNeeded/index.ts +1 -1
  77. package/src/components/Select/CustomSelect.stories.tsx +217 -217
  78. package/src/components/Select/MultiSelect.stories.tsx +240 -240
  79. package/src/components/Select/Select.stories.tsx +235 -235
  80. package/src/components/Select/Select.tsx +57 -28
  81. package/src/components/Select/components/SelectList/SelectList.tsx +8 -9
  82. package/src/components/Select/components/SelectListItem/SelectListItem.tsx +7 -3
  83. package/src/components/Select/constants.ts +2 -2
  84. package/src/components/Select/types.ts +1 -1
  85. package/src/components/Selector/Selector.stories.tsx +62 -62
  86. package/src/components/Selector/Selector.styles.ts +164 -164
  87. package/src/components/Selector/Selector.tsx +115 -115
  88. package/src/components/Selector/index.ts +2 -2
  89. package/src/components/Selector/types.ts +12 -12
  90. package/src/components/Skeleton/Skeleton.stories.tsx +19 -19
  91. package/src/components/SmartInput/SmartInput.tsx +134 -134
  92. package/src/components/Status/Status.stories.tsx +73 -73
  93. package/src/components/Status/Status.styles.ts +143 -143
  94. package/src/components/Status/Status.tsx +49 -49
  95. package/src/components/Status/constants.ts +11 -11
  96. package/src/components/Status/index.ts +3 -3
  97. package/src/components/Status/types.ts +5 -5
  98. package/src/components/Switch/Switch.stories.tsx +40 -40
  99. package/src/components/Switch/Switch.tsx +75 -75
  100. package/src/components/TextArea/TextArea.tsx +180 -180
  101. package/src/components/TextButton/TextButton.stories.tsx +46 -46
  102. package/src/components/TextButton/TextButton.styles.ts +129 -129
  103. package/src/components/TextButton/TextButton.tsx +103 -103
  104. package/src/components/TextButton/index.ts +4 -4
  105. package/src/components/TextWithInfo/TextWithInfo.stories.tsx +53 -53
  106. package/src/components/TextWithInfo/TextWithInfo.tsx +62 -62
  107. package/src/components/TextWithTooltip/TextWithTooltip.stories.tsx +58 -58
  108. package/src/components/ThemedPreloader/ThemedPreloader.stories.tsx +41 -41
  109. package/src/components/ThemedPreloader/ThemedPreloader.tsx +54 -54
  110. package/src/components/ThemedPreloader/components/DefaultPreloader/index.ts +1 -1
  111. package/src/components/Toaster/Toaster.stories.tsx +30 -30
  112. package/src/components/Toaster/Toaster.tsx +108 -108
  113. package/src/components/Tooltip/Tooltip.stories.tsx +19 -19
  114. package/src/components/Tooltip/Tooltip.tsx +35 -35
  115. package/src/components/Tooltip/types.ts +1 -1
  116. package/src/components/WithPopup/WithPopup.styles.ts +4 -0
  117. package/src/components/WithPopup/WithPopup.tsx +6 -1
  118. package/src/helpers/popper-helpers.ts +17 -17
  119. package/src/hooks/use-dropdown.ts +84 -84
  120. package/src/hooks/use-is-mounted.ts +15 -15
  121. package/src/theme/helpers.ts +76 -76
  122. package/src/vite-env.d.ts +1 -1
@@ -1,83 +1,83 @@
1
- import clsx from 'clsx';
2
- import { isNotEmpty } from '@true-engineering/true-react-platform-helpers';
3
- import { ICommonProps } from '../../../../types';
4
- import { Skeleton } from '../../../Skeleton';
5
- import { formatCellContent } from '../../helpers';
6
- import {
7
- ITableRow,
8
- IValueComponent,
9
- IFlexibleTableConfigType,
10
- IFlexibleTableRenderMode,
11
- } from '../../types';
12
- import { useStyles, IFlexibleTableCellStyles } from './FlexibleTableCell.styles';
13
-
14
- export interface IFlexibleTableCellProps<Row extends ITableRow>
15
- extends Pick<ICommonProps<IFlexibleTableCellStyles>, 'tweakStyles'>,
16
- Pick<
17
- Parameters<IValueComponent<Row, unknown>>[0],
18
- | 'isFocusedRow'
19
- | 'isNestedComponentExpanded'
20
- | 'isRowNestedComponentExpanded'
21
- | 'onSetNestedComponent'
22
- > {
23
- row: Row;
24
- columnName: keyof Row;
25
- config: IFlexibleTableConfigType<Row>;
26
- renderMode: IFlexibleTableRenderMode;
27
- isSecond?: boolean;
28
- isSticky?: boolean;
29
- isLoading?: boolean;
30
- }
31
-
32
- export function FlexibleTableCell<Row extends ITableRow>({
33
- row,
34
- columnName,
35
- config,
36
- renderMode,
37
- isSecond,
38
- isSticky,
39
- isLoading,
40
- tweakStyles,
41
- ...valueComponentProps
42
- }: IFlexibleTableCellProps<Row>): JSX.Element {
43
- const classes = useStyles({ theme: tweakStyles });
44
-
45
- const { component, left, right, position, cellAlign, cellVerticalAlign } =
46
- config[columnName] ?? {};
47
-
48
- const value = row[columnName];
49
-
50
- const TableCell = renderMode === 'divs' ? 'div' : 'td';
51
-
52
- return (
53
- <TableCell
54
- className={clsx(classes.root, {
55
- [classes.sticky]: isSticky,
56
- [classes.second]: isSecond,
57
- [classes.loading]: isLoading,
58
- })}
59
- style={{
60
- textAlign: cellAlign,
61
- position: isSticky ? 'sticky' : position,
62
- right,
63
- left: isSticky ? 0 : left,
64
- verticalAlign: cellVerticalAlign,
65
- }}
66
- >
67
- {isLoading ? (
68
- <div className={classes.skeleton}>
69
- <Skeleton />
70
- </div>
71
- ) : (
72
- isNotEmpty(value) && (
73
- <>
74
- {/* TODO: Рендерить как настоящий компонент */}
75
- {isNotEmpty(component)
76
- ? component({ ...valueComponentProps, value, row })
77
- : formatCellContent(value, config[columnName])}
78
- </>
79
- )
80
- )}
81
- </TableCell>
82
- );
83
- }
1
+ import clsx from 'clsx';
2
+ import { isNotEmpty } from '@true-engineering/true-react-platform-helpers';
3
+ import { ICommonProps } from '../../../../types';
4
+ import { Skeleton } from '../../../Skeleton';
5
+ import { formatCellContent } from '../../helpers';
6
+ import {
7
+ ITableRow,
8
+ IValueComponent,
9
+ IFlexibleTableConfigType,
10
+ IFlexibleTableRenderMode,
11
+ } from '../../types';
12
+ import { useStyles, IFlexibleTableCellStyles } from './FlexibleTableCell.styles';
13
+
14
+ export interface IFlexibleTableCellProps<Row extends ITableRow>
15
+ extends Pick<ICommonProps<IFlexibleTableCellStyles>, 'tweakStyles'>,
16
+ Pick<
17
+ Parameters<IValueComponent<Row, unknown>>[0],
18
+ | 'isFocusedRow'
19
+ | 'isNestedComponentExpanded'
20
+ | 'isRowNestedComponentExpanded'
21
+ | 'onSetNestedComponent'
22
+ > {
23
+ row: Row;
24
+ columnName: keyof Row;
25
+ config: IFlexibleTableConfigType<Row>;
26
+ renderMode: IFlexibleTableRenderMode;
27
+ isSecond?: boolean;
28
+ isSticky?: boolean;
29
+ isLoading?: boolean;
30
+ }
31
+
32
+ export function FlexibleTableCell<Row extends ITableRow>({
33
+ row,
34
+ columnName,
35
+ config,
36
+ renderMode,
37
+ isSecond,
38
+ isSticky,
39
+ isLoading,
40
+ tweakStyles,
41
+ ...valueComponentProps
42
+ }: IFlexibleTableCellProps<Row>): JSX.Element {
43
+ const classes = useStyles({ theme: tweakStyles });
44
+
45
+ const { component, left, right, position, cellAlign, cellVerticalAlign } =
46
+ config[columnName] ?? {};
47
+
48
+ const value = row[columnName];
49
+
50
+ const TableCell = renderMode === 'divs' ? 'div' : 'td';
51
+
52
+ return (
53
+ <TableCell
54
+ className={clsx(classes.root, {
55
+ [classes.sticky]: isSticky,
56
+ [classes.second]: isSecond,
57
+ [classes.loading]: isLoading,
58
+ })}
59
+ style={{
60
+ textAlign: cellAlign,
61
+ position: isSticky ? 'sticky' : position,
62
+ right,
63
+ left: isSticky ? 0 : left,
64
+ verticalAlign: cellVerticalAlign,
65
+ }}
66
+ >
67
+ {isLoading ? (
68
+ <div className={classes.skeleton}>
69
+ <Skeleton />
70
+ </div>
71
+ ) : (
72
+ isNotEmpty(value) && (
73
+ <>
74
+ {/* TODO: Рендерить как настоящий компонент */}
75
+ {isNotEmpty(component)
76
+ ? component({ ...valueComponentProps, value, row })
77
+ : formatCellContent(value, config[columnName])}
78
+ </>
79
+ )
80
+ )}
81
+ </TableCell>
82
+ );
83
+ }
@@ -1,25 +1,25 @@
1
- import { ITweakStyles, createThemedStyles } from '../../../../theme';
2
- import { IFlexibleTableCellStyles } from '../FlexibleTableCell';
3
-
4
- export const useStyles = createThemedStyles('FlexibleTableRow', {
5
- root: {
6
- position: 'relative',
7
- },
8
-
9
- active: {},
10
-
11
- editable: {
12
- cursor: 'pointer',
13
- },
14
-
15
- clickable: {
16
- cursor: 'pointer',
17
- },
18
-
19
- nestedComponent: {},
20
- });
21
-
22
- export type IFlexibleTableRowStyles = ITweakStyles<
23
- typeof useStyles,
24
- { tweakTableCell: IFlexibleTableCellStyles }
25
- >;
1
+ import { ITweakStyles, createThemedStyles } from '../../../../theme';
2
+ import { IFlexibleTableCellStyles } from '../FlexibleTableCell';
3
+
4
+ export const useStyles = createThemedStyles('FlexibleTableRow', {
5
+ root: {
6
+ position: 'relative',
7
+ },
8
+
9
+ active: {},
10
+
11
+ editable: {
12
+ cursor: 'pointer',
13
+ },
14
+
15
+ clickable: {
16
+ cursor: 'pointer',
17
+ },
18
+
19
+ nestedComponent: {},
20
+ });
21
+
22
+ export type IFlexibleTableRowStyles = ITweakStyles<
23
+ typeof useStyles,
24
+ { tweakTableCell: IFlexibleTableCellStyles }
25
+ >;
@@ -1,196 +1,196 @@
1
- import { ReactNode, useState, memo, MouseEvent } from 'react';
2
- import clsx from 'clsx';
3
- import {
4
- isEmpty,
5
- isFunction,
6
- isNotEmpty,
7
- isReactNodeNotEmpty,
8
- } from '@true-engineering/true-react-platform-helpers';
9
- import { addDataAttributes } from '../../../../helpers';
10
- import { useTweakStyles } from '../../../../hooks';
11
- import { ICommonProps, IDataAttributes } from '../../../../types';
12
- import {
13
- ITableRow,
14
- IFlexibleTableConfigType,
15
- IFlexibleTableRenderMode,
16
- INestedComponent,
17
- } from '../../types';
18
- import { FlexibleTableCell } from '../FlexibleTableCell';
19
- import { useStyles, IFlexibleTableRowStyles } from './FlexibleTableRow.styles';
20
-
21
- export interface IFlexibleTableRowProps<Row extends ITableRow>
22
- extends Pick<ICommonProps<IFlexibleTableRowStyles>, 'tweakStyles'> {
23
- item: Row;
24
- index: number;
25
- uniqueField?: keyof Row;
26
- renderMode: IFlexibleTableRenderMode;
27
- /** Индексы строк, на которые навешивается класс `active` */
28
- activeRows?: number[];
29
- /** @default false */
30
- isFirstColumnSticky?: boolean;
31
- /** @default false */
32
- isLoading?: boolean;
33
- config: IFlexibleTableConfigType<Row>;
34
- columns: Array<keyof Row & string>;
35
- rowAttributes?: Array<keyof Row>;
36
- /** @default false */
37
- isExpandableRowComponentInitiallyOpen?: boolean | ((row: Row, index: number) => boolean);
38
- /** Возвращает React-элемент, который отрисуется под строкой при нажатии на неё */
39
- expandableRowComponent?: (item: Row, isOpen: boolean, close: () => void) => ReactNode;
40
- // TODO: Заменить string на Generic Values[uniqueField]
41
- onRowHover?: (id?: string) => void;
42
- onRowClick?: (id: string) => void;
43
- }
44
-
45
- function FlexibleTableRowInner<Row extends ITableRow>({
46
- item,
47
- index,
48
- config,
49
- columns,
50
- uniqueField,
51
- renderMode,
52
- activeRows,
53
- isFirstColumnSticky,
54
- isLoading = false,
55
- rowAttributes,
56
- isExpandableRowComponentInitiallyOpen = false,
57
- tweakStyles,
58
- expandableRowComponent,
59
- onRowHover,
60
- onRowClick,
61
- }: IFlexibleTableRowProps<Row>): JSX.Element {
62
- const classes = useStyles({ theme: tweakStyles });
63
-
64
- const tweakTableCellStyles = useTweakStyles({
65
- tweakStyles,
66
- className: 'tweakTableCell',
67
- currentComponentName: 'FlexibleTableRow',
68
- });
69
-
70
- const [isFocused, setFocused] = useState(false);
71
- const [nestedComponent, setNestedComponent] = useState<INestedComponent>(() => {
72
- const isOpen = isFunction(isExpandableRowComponentInitiallyOpen)
73
- ? isExpandableRowComponentInitiallyOpen(item, index)
74
- : isExpandableRowComponentInitiallyOpen;
75
-
76
- const component = isOpen
77
- ? expandableRowComponent?.(item, true, () => {
78
- setNestedComponent({ isOpen: false });
79
- })
80
- : undefined;
81
-
82
- return isReactNodeNotEmpty(component) ? { isOpen: true, component } : { isOpen: false };
83
- });
84
-
85
- const isActive = activeRows?.includes(index) ?? false;
86
- const isEditable = !isLoading && (isNotEmpty(onRowClick) || isNotEmpty(onRowHover));
87
- const isClickable = !isLoading && (isNotEmpty(onRowClick) || isNotEmpty(expandableRowComponent));
88
-
89
- const { isOpen: isNestedComponentExpanded, cellKey: nestedComponentCellKey } = nestedComponent;
90
-
91
- // уникальная разработка, позволяющая прокидывать data-атрибуты в <tr>
92
- // например: rowAttributes={['id']} => <tr data-id="x" />
93
- const rowData = rowAttributes?.reduce<IDataAttributes>(
94
- (acc, cur) => ({ ...acc, [cur]: item[cur] }),
95
- {},
96
- );
97
-
98
- const handleMouseEnter = (event: MouseEvent) => {
99
- if (isNotEmpty(uniqueField) && isNotEmpty(onRowHover)) {
100
- event.stopPropagation();
101
- onRowHover(item[uniqueField]);
102
- setFocused(true);
103
- }
104
- };
105
-
106
- const handleMouseLeave = () => {
107
- onRowHover?.(undefined);
108
- setFocused(false);
109
- };
110
-
111
- const updateNestedComponent = (component?: ReactNode, cellKey?: string) => {
112
- setNestedComponent(
113
- component === undefined ? { isOpen: false } : { isOpen: true, component, cellKey },
114
- );
115
- };
116
-
117
- const closeNestedComponent = () => {
118
- setNestedComponent({ isOpen: false });
119
- };
120
-
121
- const handleRowClick = () => {
122
- if (isNotEmpty(uniqueField)) {
123
- onRowClick?.(item[uniqueField]);
124
- }
125
-
126
- if (isNotEmpty(expandableRowComponent)) {
127
- const newNestedComponent = expandableRowComponent(item, true, closeNestedComponent);
128
-
129
- if (!isNestedComponentExpanded && newNestedComponent !== null) {
130
- updateNestedComponent(newNestedComponent);
131
- return;
132
- }
133
-
134
- if (isNestedComponentExpanded && isEmpty(nestedComponentCellKey)) {
135
- closeNestedComponent();
136
- return;
137
- }
138
- }
139
- };
140
-
141
- const TableRow = renderMode === 'divs' ? 'div' : 'tr';
142
- const TableCell = renderMode === 'divs' ? 'div' : 'td';
143
-
144
- return (
145
- <>
146
- <TableRow
147
- className={clsx(classes.root, {
148
- [classes.active]: isActive,
149
- [classes.editable]: isEditable,
150
- [classes.clickable]: isClickable,
151
- })}
152
- {...(!isLoading && {
153
- onClick: handleRowClick,
154
- onMouseEnter: handleMouseEnter,
155
- onMouseLeave: handleMouseLeave,
156
- })}
157
- {...addDataAttributes({
158
- ...rowData,
159
- active: isActive ? true : undefined,
160
- editable: isEditable ? true : undefined,
161
- isExpandableComponentActive: isNestedComponentExpanded ? true : undefined,
162
- })}
163
- >
164
- {columns.map((key, i) => (
165
- <FlexibleTableCell
166
- key={key}
167
- isSticky={isFirstColumnSticky && i === 0}
168
- isSecond={isFirstColumnSticky && i === 1}
169
- isLoading={isLoading}
170
- row={item}
171
- config={config}
172
- columnName={key}
173
- tweakStyles={tweakTableCellStyles}
174
- renderMode={renderMode}
175
- isFocusedRow={isFocused}
176
- isNestedComponentExpanded={isNestedComponentExpanded && nestedComponentCellKey === key}
177
- isRowNestedComponentExpanded={
178
- isNestedComponentExpanded && isEmpty(nestedComponentCellKey)
179
- }
180
- onSetNestedComponent={(component) => updateNestedComponent(component, key)}
181
- />
182
- ))}
183
- </TableRow>
184
-
185
- {isNestedComponentExpanded && (
186
- <TableRow className={classes.root}>
187
- <TableCell className={classes.nestedComponent} colSpan={columns.length}>
188
- {nestedComponent.component}
189
- </TableCell>
190
- </TableRow>
191
- )}
192
- </>
193
- );
194
- }
195
-
196
- export const FlexibleTableRow = memo(FlexibleTableRowInner) as typeof FlexibleTableRowInner;
1
+ import { ReactNode, useState, memo, MouseEvent } from 'react';
2
+ import clsx from 'clsx';
3
+ import {
4
+ isEmpty,
5
+ isFunction,
6
+ isNotEmpty,
7
+ isReactNodeNotEmpty,
8
+ } from '@true-engineering/true-react-platform-helpers';
9
+ import { addDataAttributes } from '../../../../helpers';
10
+ import { useTweakStyles } from '../../../../hooks';
11
+ import { ICommonProps, IDataAttributes } from '../../../../types';
12
+ import {
13
+ ITableRow,
14
+ IFlexibleTableConfigType,
15
+ IFlexibleTableRenderMode,
16
+ INestedComponent,
17
+ } from '../../types';
18
+ import { FlexibleTableCell } from '../FlexibleTableCell';
19
+ import { useStyles, IFlexibleTableRowStyles } from './FlexibleTableRow.styles';
20
+
21
+ export interface IFlexibleTableRowProps<Row extends ITableRow>
22
+ extends Pick<ICommonProps<IFlexibleTableRowStyles>, 'tweakStyles'> {
23
+ item: Row;
24
+ index: number;
25
+ uniqueField?: keyof Row;
26
+ renderMode: IFlexibleTableRenderMode;
27
+ /** Индексы строк, на которые навешивается класс `active` */
28
+ activeRows?: number[];
29
+ /** @default false */
30
+ isFirstColumnSticky?: boolean;
31
+ /** @default false */
32
+ isLoading?: boolean;
33
+ config: IFlexibleTableConfigType<Row>;
34
+ columns: Array<keyof Row & string>;
35
+ rowAttributes?: Array<keyof Row>;
36
+ /** @default false */
37
+ isExpandableRowComponentInitiallyOpen?: boolean | ((row: Row, index: number) => boolean);
38
+ /** Возвращает React-элемент, который отрисуется под строкой при нажатии на неё */
39
+ expandableRowComponent?: (item: Row, isOpen: boolean, close: () => void) => ReactNode;
40
+ // TODO: Заменить string на Generic Values[uniqueField]
41
+ onRowHover?: (id?: string) => void;
42
+ onRowClick?: (id: string) => void;
43
+ }
44
+
45
+ function FlexibleTableRowInner<Row extends ITableRow>({
46
+ item,
47
+ index,
48
+ config,
49
+ columns,
50
+ uniqueField,
51
+ renderMode,
52
+ activeRows,
53
+ isFirstColumnSticky,
54
+ isLoading = false,
55
+ rowAttributes,
56
+ isExpandableRowComponentInitiallyOpen = false,
57
+ tweakStyles,
58
+ expandableRowComponent,
59
+ onRowHover,
60
+ onRowClick,
61
+ }: IFlexibleTableRowProps<Row>): JSX.Element {
62
+ const classes = useStyles({ theme: tweakStyles });
63
+
64
+ const tweakTableCellStyles = useTweakStyles({
65
+ tweakStyles,
66
+ className: 'tweakTableCell',
67
+ currentComponentName: 'FlexibleTableRow',
68
+ });
69
+
70
+ const [isFocused, setFocused] = useState(false);
71
+ const [nestedComponent, setNestedComponent] = useState<INestedComponent>(() => {
72
+ const isOpen = isFunction(isExpandableRowComponentInitiallyOpen)
73
+ ? isExpandableRowComponentInitiallyOpen(item, index)
74
+ : isExpandableRowComponentInitiallyOpen;
75
+
76
+ const component = isOpen
77
+ ? expandableRowComponent?.(item, true, () => {
78
+ setNestedComponent({ isOpen: false });
79
+ })
80
+ : undefined;
81
+
82
+ return isReactNodeNotEmpty(component) ? { isOpen: true, component } : { isOpen: false };
83
+ });
84
+
85
+ const isActive = activeRows?.includes(index) ?? false;
86
+ const isEditable = !isLoading && (isNotEmpty(onRowClick) || isNotEmpty(onRowHover));
87
+ const isClickable = !isLoading && (isNotEmpty(onRowClick) || isNotEmpty(expandableRowComponent));
88
+
89
+ const { isOpen: isNestedComponentExpanded, cellKey: nestedComponentCellKey } = nestedComponent;
90
+
91
+ // уникальная разработка, позволяющая прокидывать data-атрибуты в <tr>
92
+ // например: rowAttributes={['id']} => <tr data-id="x" />
93
+ const rowData = rowAttributes?.reduce<IDataAttributes>(
94
+ (acc, cur) => ({ ...acc, [cur]: item[cur] }),
95
+ {},
96
+ );
97
+
98
+ const handleMouseEnter = (event: MouseEvent) => {
99
+ if (isNotEmpty(uniqueField) && isNotEmpty(onRowHover)) {
100
+ event.stopPropagation();
101
+ onRowHover(item[uniqueField]);
102
+ setFocused(true);
103
+ }
104
+ };
105
+
106
+ const handleMouseLeave = () => {
107
+ onRowHover?.(undefined);
108
+ setFocused(false);
109
+ };
110
+
111
+ const updateNestedComponent = (component?: ReactNode, cellKey?: string) => {
112
+ setNestedComponent(
113
+ component === undefined ? { isOpen: false } : { isOpen: true, component, cellKey },
114
+ );
115
+ };
116
+
117
+ const closeNestedComponent = () => {
118
+ setNestedComponent({ isOpen: false });
119
+ };
120
+
121
+ const handleRowClick = () => {
122
+ if (isNotEmpty(uniqueField)) {
123
+ onRowClick?.(item[uniqueField]);
124
+ }
125
+
126
+ if (isNotEmpty(expandableRowComponent)) {
127
+ const newNestedComponent = expandableRowComponent(item, true, closeNestedComponent);
128
+
129
+ if (!isNestedComponentExpanded && newNestedComponent !== null) {
130
+ updateNestedComponent(newNestedComponent);
131
+ return;
132
+ }
133
+
134
+ if (isNestedComponentExpanded && isEmpty(nestedComponentCellKey)) {
135
+ closeNestedComponent();
136
+ return;
137
+ }
138
+ }
139
+ };
140
+
141
+ const TableRow = renderMode === 'divs' ? 'div' : 'tr';
142
+ const TableCell = renderMode === 'divs' ? 'div' : 'td';
143
+
144
+ return (
145
+ <>
146
+ <TableRow
147
+ className={clsx(classes.root, {
148
+ [classes.active]: isActive,
149
+ [classes.editable]: isEditable,
150
+ [classes.clickable]: isClickable,
151
+ })}
152
+ {...(!isLoading && {
153
+ onClick: handleRowClick,
154
+ onMouseEnter: handleMouseEnter,
155
+ onMouseLeave: handleMouseLeave,
156
+ })}
157
+ {...addDataAttributes({
158
+ ...rowData,
159
+ active: isActive ? true : undefined,
160
+ editable: isEditable ? true : undefined,
161
+ isExpandableComponentActive: isNestedComponentExpanded ? true : undefined,
162
+ })}
163
+ >
164
+ {columns.map((key, i) => (
165
+ <FlexibleTableCell
166
+ key={key}
167
+ isSticky={isFirstColumnSticky && i === 0}
168
+ isSecond={isFirstColumnSticky && i === 1}
169
+ isLoading={isLoading}
170
+ row={item}
171
+ config={config}
172
+ columnName={key}
173
+ tweakStyles={tweakTableCellStyles}
174
+ renderMode={renderMode}
175
+ isFocusedRow={isFocused}
176
+ isNestedComponentExpanded={isNestedComponentExpanded && nestedComponentCellKey === key}
177
+ isRowNestedComponentExpanded={
178
+ isNestedComponentExpanded && isEmpty(nestedComponentCellKey)
179
+ }
180
+ onSetNestedComponent={(component) => updateNestedComponent(component, key)}
181
+ />
182
+ ))}
183
+ </TableRow>
184
+
185
+ {isNestedComponentExpanded && (
186
+ <TableRow className={classes.root}>
187
+ <TableCell className={classes.nestedComponent} colSpan={columns.length}>
188
+ {nestedComponent.component}
189
+ </TableCell>
190
+ </TableRow>
191
+ )}
192
+ </>
193
+ );
194
+ }
195
+
196
+ export const FlexibleTableRow = memo(FlexibleTableRowInner) as typeof FlexibleTableRowInner;
@@ -1,13 +1,13 @@
1
- import { format } from 'date-fns';
2
- import { isNotEmpty } from '@true-engineering/true-react-platform-helpers';
3
- import { DEFAULT_DATE_FORMAT } from './constants';
4
- import { IFlexibleTableConfigType } from './types';
5
-
6
- export const hasHorizontalScrollBar = (el: HTMLElement | null | undefined): boolean =>
7
- isNotEmpty(el) && el.scrollWidth !== el.clientWidth;
8
-
9
- export const formatCellContent = <Values>(
10
- value: unknown,
11
- config?: IFlexibleTableConfigType<Values>[keyof Values],
12
- ): string =>
13
- value instanceof Date ? format(value, config?.dateFormat ?? DEFAULT_DATE_FORMAT) : String(value);
1
+ import { format } from 'date-fns';
2
+ import { isNotEmpty } from '@true-engineering/true-react-platform-helpers';
3
+ import { DEFAULT_DATE_FORMAT } from './constants';
4
+ import { IFlexibleTableConfigType } from './types';
5
+
6
+ export const hasHorizontalScrollBar = (el: HTMLElement | null | undefined): boolean =>
7
+ isNotEmpty(el) && el.scrollWidth !== el.clientWidth;
8
+
9
+ export const formatCellContent = <Values>(
10
+ value: unknown,
11
+ config?: IFlexibleTableConfigType<Values>[keyof Values],
12
+ ): string =>
13
+ value instanceof Date ? format(value, config?.dateFormat ?? DEFAULT_DATE_FORMAT) : String(value);