@applica-software-guru/react-admin 1.3.125 → 1.3.126

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applica-software-guru/react-admin",
3
- "version": "1.3.125",
3
+ "version": "1.3.126",
4
4
  "private": false,
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,14 +15,14 @@ import {
15
15
  TableRow,
16
16
  Typography
17
17
  } from '@mui/material';
18
- import { RaRecord, useRecordContext, useTranslate, useTranslateLabel } from 'ra-core';
18
+ import { FormDataConsumer, RaRecord, useRecordContext, useTranslate, useTranslateLabel } from 'ra-core';
19
19
  import { styled, useTheme } from '@mui/material/styles';
20
20
 
21
21
  import { PlusCircleOutlined } from '@ant-design/icons';
22
22
  import PropTypes from 'prop-types';
23
23
  import { TableFormIteratorContext } from './TableFormIteratorContext';
24
24
  import { TableFormIteratorItem } from './TableFormIteratorItem';
25
- import { UseFieldArrayReturn } from 'react-hook-form';
25
+ import { UseFieldArrayReturn, useFormContext } from 'react-hook-form';
26
26
  import get from 'lodash/get';
27
27
 
28
28
  /**
@@ -44,13 +44,27 @@ import get from 'lodash/get';
44
44
  * @returns
45
45
  */
46
46
  const TableFormIterator = (props: TableFormIteratorProps) => {
47
- const { children, resource, source, label, disableActions = false, disableAdd = false, disableRemove = false, className, empty, template = {} } = props;
47
+ const {
48
+ children,
49
+ resource,
50
+ source,
51
+ label,
52
+ disableActions = false,
53
+ disableAdd = false,
54
+ disableRemove = false,
55
+ className,
56
+ empty,
57
+ template = {}
58
+ } = props;
48
59
  const [confirmIsOpen, setConfirmIsOpen] = useState<boolean>(false);
49
- const { fields, remove, replace } = useArrayInput(props);
60
+ const { fields, remove, replace, append } = useArrayInput(props);
61
+ const { resetField } = useFormContext();
62
+
50
63
  const theme = useTheme();
51
64
  const translate = useTranslate();
52
65
  const record = useRecordContext(props);
53
- const initialDefaultValue = useRef({});
66
+ const initialDefaultValue = useRef(template || {});
67
+
54
68
  const translateLabel = useTranslateLabel();
55
69
  const removeField = useCallback(
56
70
  (index: number) => {
@@ -66,6 +80,50 @@ const TableFormIterator = (props: TableFormIteratorProps) => {
66
80
  for (const k in initialDefaultValue.current) initialDefaultValue.current[k] = null;
67
81
  }
68
82
 
83
+ const addField = useCallback(
84
+ (item: any = undefined) => {
85
+ let defaultValue = item;
86
+ if (item == null) {
87
+ defaultValue = initialDefaultValue.current;
88
+ if (
89
+ Children.count(children) === 1 &&
90
+ React.isValidElement(Children.only(children)) &&
91
+ // @ts-ignore
92
+ !Children.only(children).props.source &&
93
+ // Make sure it's not a FormDataConsumer
94
+ // @ts-ignore
95
+ Children.map(children, (input) => React.isValidElement(input) && input.type !== FormDataConsumer).some(Boolean)
96
+ ) {
97
+ // ArrayInput used for an array of scalar values
98
+ // (e.g. tags: ['foo', 'bar'])
99
+ defaultValue = '';
100
+ } else {
101
+ // ArrayInput used for an array of objects
102
+ // (e.g. authors: [{ firstName: 'John', lastName: 'Doe' }, { firstName: 'Jane', lastName: 'Doe' }])
103
+ defaultValue = defaultValue || ({} as Record<string, unknown>);
104
+ Children.forEach(children, (input) => {
105
+ if (React.isValidElement(input) && input.type !== FormDataConsumer && input.props.source) {
106
+ defaultValue[input.props.source] = input.props.defaultValue ?? null;
107
+ }
108
+ });
109
+ }
110
+ }
111
+ const newField = { ...defaultValue, ...template };
112
+ append(newField);
113
+ // Make sure the newly added inputs are not considered dirty by react-hook-form
114
+ resetField(`${source}.${fields.length}`, { defaultValue });
115
+ },
116
+ [append, children, resetField, source, fields.length, template]
117
+ );
118
+
119
+ // add field and call the onClick event of the button passed as addButton prop
120
+ const handleAddButtonClick = (originalOnClickHandler: any) => (event: MouseEvent) => {
121
+ addField();
122
+ if (originalOnClickHandler) {
123
+ originalOnClickHandler(event);
124
+ }
125
+ };
126
+
69
127
  const handleArrayClear = useCallback(() => {
70
128
  replace([]);
71
129
  setConfirmIsOpen(false);
@@ -95,7 +153,7 @@ const TableFormIterator = (props: TableFormIteratorProps) => {
95
153
  source,
96
154
  disableAdd,
97
155
  template,
98
- addButton: props?.addButton,
156
+ onClick: handleAddButtonClick((props?.addButton as any)?.props?.onClick)
99
157
  })}
100
158
 
101
159
  <TableContainer component={Paper} sx={{ mt: 2, border: `1px solid ${tableBorderColor}` }}>
@@ -177,34 +235,25 @@ const TableFormIterator = (props: TableFormIteratorProps) => {
177
235
  };
178
236
 
179
237
  const AddTableRow = (props: any) => {
180
- const { label, disableAdd, template = {} } = props;
181
- const { append } = useArrayInput(props);
238
+ const { label, disableAdd, onClick } = props;
182
239
  const theme = useTheme();
183
240
  // @ts-ignore
184
241
  const tableBorderColor = theme.palette.mode === 'dark' ? theme.palette.grey.A400 : theme.palette.grey.A800;
185
242
  const iconColor = theme.palette.mode === 'light' ? '#000000' : '#FFFFFF';
186
243
 
187
- const handleAddButtonClick = useCallback((
188
- originalOnClickHandler: React.MouseEventHandler<Element>
189
- ) => (event: React.MouseEvent<Element, MouseEvent>) => {
190
- append(template);
191
- originalOnClickHandler && originalOnClickHandler(event);
192
- }, [append, template]);
193
-
194
244
  return (
195
245
  <Stack justifyContent={'space-between'} alignItems={'center'} flexDirection={'row'}>
196
- <Typography>{label}</Typography>
197
- {!disableAdd && (
198
- React.isValidElement(props?.addButton) ?
246
+ {label !== false && <Typography>{label}</Typography>}
247
+ {!disableAdd &&
248
+ (React.isValidElement(props?.addButton) ? (
199
249
  React.cloneElement(props?.addButton, {
200
- onClick: handleAddButtonClick(
201
- props?.addButton?.props.onClick
202
- ),
203
- }) :
204
- <IconButton size="small" color="secondary" sx={{ border: `1px solid ${tableBorderColor}` }} onClick={() => append(template)}>
250
+ onClick: onClick
251
+ })
252
+ ) : (
253
+ <IconButton size="small" color="secondary" sx={{ border: `1px solid ${tableBorderColor}` }} onClick={onClick}>
205
254
  <PlusCircleOutlined style={{ color: iconColor }} />
206
255
  </IconButton>
207
- )}
256
+ ))}
208
257
  </Stack>
209
258
  );
210
259
  };
@@ -222,7 +271,7 @@ const StyledTableFormIterator = styled(TableFormIterator, {
222
271
  }));
223
272
 
224
273
  AddTableRow.propTypes = {
225
- label: PropTypes.string,
274
+ label: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
226
275
  source: PropTypes.string,
227
276
  defaultValue: PropTypes.any
228
277
  };
@@ -237,7 +286,7 @@ TableFormIterator.propTypes = {
237
286
  formState: PropTypes.object,
238
287
  record: PropTypes.object,
239
288
  source: PropTypes.string,
240
- label: PropTypes.string,
289
+ label: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
241
290
  resource: PropTypes.string,
242
291
  translate: PropTypes.func,
243
292
  disableAdd: PropTypes.bool,
@@ -261,7 +310,7 @@ export interface TableFormIteratorProps extends Partial<UseFieldArrayReturn> {
261
310
  submitFailed?: boolean;
262
311
  };
263
312
  record?: RaRecord;
264
- label?: string;
313
+ label?: string | boolean;
265
314
  resource?: string;
266
315
  source?: string;
267
316
  sx?: SxProps;
@@ -47,14 +47,14 @@ export const TableFormIteratorItem = React.forwardRef((props: TableFormIteratorI
47
47
  resource,
48
48
  disabled,
49
49
  ...inputProps,
50
- label: false
50
+ label: ''
51
51
  })}
52
52
  </TableCell>
53
53
  );
54
54
  })}
55
55
 
56
56
  {!disableActions && (
57
- <TableCell sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', border: 0, pt: 2 }}>
57
+ <TableCell sx={{ display: 'flex', justifyContent: 'center', alignItems: 'center', border: 0, pt: 2.5 }}>
58
58
  <ActionsMenu horizontal>
59
59
  <Typography color="error" onClick={() => setConfirmIsOpen(true)}>
60
60
  {translate('ra.action.delete')}