@applica-software-guru/react-admin 1.5.298 → 1.5.300

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/package.json CHANGED
@@ -33,6 +33,7 @@
33
33
  "devDependencies": {
34
34
  "@applica-software-guru/crud-client": "^1.1",
35
35
  "@applica-software-guru/iam-client": "^1.1",
36
+ "@types/inflection": "^1.13.2",
36
37
  "@types/node": "^20.12.13",
37
38
  "@types/react": "^18.3.3",
38
39
  "@types/react-dom": "^18.3.0",
@@ -106,5 +107,5 @@
106
107
  "type": "module",
107
108
  "types": "dist/index.d.ts",
108
109
  "typings": "dist/index.d.ts",
109
- "version": "1.5.298"
110
+ "version": "1.5.300"
110
111
  }
@@ -21,7 +21,8 @@ type ThemeFontFamily =
21
21
  | `'Inter', sans-serif`
22
22
  | `'Poppins', sans-serif`
23
23
  | `'Roboto', sans-serif`
24
- | `'Public Sans', sans-serif`;
24
+ | `'Public Sans', sans-serif`
25
+ | /** Any font family name as string */ string;
25
26
 
26
27
  type ThemeConfig = {
27
28
  fontFamily: ThemeFontFamily;
@@ -0,0 +1,210 @@
1
+ import * as React from 'react';
2
+ import { ReactElement } from 'react';
3
+ import PropTypes from 'prop-types';
4
+ import ActionDelete from '@mui/icons-material/Delete';
5
+ import inflection from 'inflection';
6
+ import { alpha, styled } from '@mui/material/styles';
7
+ import {
8
+ DeleteManyParams,
9
+ MutationMode,
10
+ RaRecord,
11
+ useDeleteMany,
12
+ useListContext,
13
+ useNotify,
14
+ useRefresh,
15
+ useResourceContext,
16
+ useSafeSetState,
17
+ useTranslate
18
+ } from 'ra-core';
19
+ import { BulkActionProps, Button, ButtonProps, Confirm } from 'react-admin';
20
+ import { UseMutationOptions } from 'react-query';
21
+ import _ from 'lodash';
22
+
23
+ /**
24
+ * BulkDeleteWithConfirmButton component allows you to delete multiple records with a confirmation dialog.
25
+ *
26
+ * This component extends the functionality of the react-admin BulkDeleteWithConfirmButton by adding
27
+ * two optional methods, `onSuccess` and `onError`, to the props. These methods allow users
28
+ * to extend what happens in the `onSuccess` and `onError` methods of the `useDeleteMany` hook.
29
+ *
30
+ * @example
31
+ * <BulkDeleteWithConfirmButton
32
+ * onSuccess={() => {
33
+ * // Custom success logic
34
+ * }}
35
+ * onError={() => {
36
+ * // Custom error logic
37
+ * }}
38
+ * />
39
+ */
40
+ function BulkDeleteWithConfirmButton(props: BulkDeleteWithConfirmButtonProps) {
41
+ const {
42
+ confirmTitle = 'ra.message.bulk_delete_title',
43
+ confirmContent = 'ra.message.bulk_delete_content',
44
+ confirmColor = 'primary',
45
+ icon = defaultIcon,
46
+ label = 'ra.action.delete',
47
+ mutationMode = 'pessimistic',
48
+ mutationOptions = {},
49
+ onClick,
50
+ onSuccess: externalOnSuccess,
51
+ onError: externalOnError,
52
+ ...rest
53
+ } = props;
54
+ const { meta: mutationMeta, ...otherMutationOptions } = mutationOptions;
55
+ const { selectedIds, onUnselectItems } = useListContext(props);
56
+ const [isOpen, setOpen] = useSafeSetState(false);
57
+ const notify = useNotify();
58
+ const resource = useResourceContext(props);
59
+ const refresh = useRefresh();
60
+ const translate = useTranslate();
61
+ const [deleteMany, { isLoading }] = useDeleteMany(
62
+ resource,
63
+ { ids: selectedIds, meta: mutationMeta },
64
+ {
65
+ // @ts-ignore
66
+ onSuccess: () => {
67
+ refresh();
68
+ notify('ra.notification.deleted', {
69
+ type: 'info',
70
+ messageArgs: { smart_count: selectedIds.length },
71
+ undoable: mutationMode === 'undoable'
72
+ });
73
+ onUnselectItems();
74
+ setOpen(false);
75
+ if (externalOnSuccess && _.isFunction(externalOnSuccess)) {
76
+ externalOnSuccess();
77
+ }
78
+ },
79
+ // @ts-ignore
80
+ onError: (error: Error) => {
81
+ notify(typeof error === 'string' ? error : error.message || 'ra.notification.http_error', {
82
+ type: 'error',
83
+ messageArgs: {
84
+ _: typeof error === 'string' ? error : error && error.message ? error.message : undefined
85
+ }
86
+ });
87
+ setOpen(false);
88
+ if (externalOnError && _.isFunction(externalOnError)) {
89
+ externalOnError();
90
+ }
91
+ },
92
+ mutationMode,
93
+ ...otherMutationOptions
94
+ }
95
+ );
96
+
97
+ function handleClick(e: any) {
98
+ setOpen(true);
99
+ e.stopPropagation();
100
+ }
101
+
102
+ function handleDialogClose() {
103
+ setOpen(false);
104
+ }
105
+
106
+ function handleDelete(e: any) {
107
+ deleteMany();
108
+
109
+ if (typeof onClick === 'function') {
110
+ onClick(e);
111
+ }
112
+ }
113
+
114
+ return (
115
+ <>
116
+ <StyledButton onClick={handleClick} label={label} {...sanitizeRestProps(rest)}>
117
+ {icon}
118
+ </StyledButton>
119
+ <Confirm
120
+ isOpen={isOpen}
121
+ loading={isLoading}
122
+ title={confirmTitle}
123
+ content={confirmContent}
124
+ confirmColor={confirmColor}
125
+ translateOptions={{
126
+ smart_count: selectedIds.length,
127
+ name: translate(`resources.${resource}.forcedCaseName`, {
128
+ smart_count: selectedIds.length,
129
+ _: inflection.humanize(
130
+ translate(`resources.${resource}.name`, {
131
+ smart_count: selectedIds.length,
132
+ _: inflection.inflect(resource, selectedIds.length)
133
+ }),
134
+ true
135
+ )
136
+ })
137
+ }}
138
+ onConfirm={handleDelete}
139
+ onClose={handleDialogClose}
140
+ />
141
+ </>
142
+ );
143
+ }
144
+
145
+ function sanitizeRestProps({
146
+ classes,
147
+ filterValues,
148
+ label,
149
+ selectedIds,
150
+ ...rest
151
+ }: Omit<BulkDeleteWithConfirmButtonProps, 'resource' | 'icon' | 'mutationMode'>) {
152
+ return rest;
153
+ }
154
+
155
+ interface BulkDeleteWithConfirmButtonProps<RecordType extends RaRecord = any, MutationOptionsError = unknown>
156
+ extends BulkActionProps,
157
+ ButtonProps {
158
+ confirmContent?: React.ReactNode;
159
+ confirmTitle?: React.ReactNode;
160
+ confirmColor?: 'primary' | 'warning';
161
+ icon?: ReactElement;
162
+ mutationMode: MutationMode;
163
+ mutationOptions?: UseMutationOptions<RecordType, MutationOptionsError, DeleteManyParams<RecordType>> & {
164
+ meta?: any;
165
+ };
166
+ /**
167
+ * Optional custom success logic to be executed after the default onSuccess logic.
168
+ * This method allows users to extend what happens in the onSuccess method of the useDeleteMany hook.
169
+ */
170
+ onSuccess?: () => void;
171
+ /**
172
+ * Optional custom error logic to be executed after the default onError logic.
173
+ * This method allows users to extend what happens in the onError method of the useDeleteMany hook.
174
+ */
175
+ onError?: () => void;
176
+ }
177
+
178
+ const PREFIX = 'RaBulkDeleteWithConfirmButton';
179
+
180
+ const StyledButton = styled(Button, {
181
+ name: PREFIX,
182
+ overridesResolver: (styles) => styles.root
183
+ })(({ theme }) => ({
184
+ color: theme.palette.error.main,
185
+ '&:hover': {
186
+ backgroundColor: alpha(theme.palette.error.main, 0.12),
187
+ // Reset on mouse devices
188
+ '@media (hover: none)': {
189
+ backgroundColor: 'transparent'
190
+ }
191
+ }
192
+ }));
193
+
194
+ const defaultIcon = <ActionDelete />;
195
+
196
+ BulkDeleteWithConfirmButton.propTypes = {
197
+ confirmTitle: PropTypes.node,
198
+ confirmContent: PropTypes.node,
199
+ confirmColor: PropTypes.string,
200
+ icon: PropTypes.element,
201
+ label: PropTypes.string,
202
+ mutationMode: PropTypes.oneOf(['pessimistic', 'optimistic', 'undoable']),
203
+ resource: PropTypes.string,
204
+ selectedIds: PropTypes.arrayOf(PropTypes.any),
205
+ onSuccess: PropTypes.func,
206
+ onError: PropTypes.func
207
+ };
208
+
209
+ export { BulkDeleteWithConfirmButton };
210
+ export type { BulkDeleteWithConfirmButtonProps };
@@ -1,3 +1,4 @@
1
+ export * from './BulkDeleteWithConfirmButton';
1
2
  export * from './Button';
2
3
  export * from './CreateButton';
3
4
  export * from './CreateInDialogButton';
package/src/index.ts CHANGED
@@ -12,7 +12,6 @@ export {
12
12
  ArrayField,
13
13
  ArrayInputContext,
14
14
  BooleanField,
15
- BulkDeleteWithConfirmButton,
16
15
  ChipField,
17
16
  Confirm,
18
17
  CreateContextProvider,