Skip to content

Commit 28c92f3

Browse files
author
Travis CI
committed
Revert unecessary change
1 parent 39ed62a commit 28c92f3

File tree

1 file changed

+63
-26
lines changed

1 file changed

+63
-26
lines changed

packages/ra-ui-materialui/src/input/ArrayInput/SimpleFormIterator.tsx

+63-26
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ import {
77
ReactNode,
88
useCallback,
99
useMemo,
10+
useRef,
1011
} from 'react';
1112
import { FormHelperText } from '@material-ui/core';
1213
import classNames from 'classnames';
1314
import get from 'lodash/get';
1415
import PropTypes from 'prop-types';
1516
import { Record, ValidationError } from 'ra-core';
1617
import { FieldArrayRenderProps } from 'react-final-form-arrays';
18+
import { CSSTransition, TransitionGroup } from 'react-transition-group';
19+
import { CSSTransitionProps } from 'react-transition-group/CSSTransition';
1720

1821
import { ClassesOverride } from '../../types';
1922
import { useArrayInput } from './useArrayInput';
@@ -51,16 +54,37 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
5154
const classes = useSimpleFormIteratorStyles(props);
5255
const { fields, meta } = useArrayInput(props);
5356
const { error, submitFailed } = meta;
57+
const nodeRef = useRef(null);
58+
59+
// We need a unique id for each field for a proper enter/exit animation
60+
// so we keep an internal map between the field position and an auto-increment id
61+
const nextId = useRef(
62+
fields && fields.length
63+
? fields.length
64+
: defaultValue
65+
? defaultValue.length
66+
: 0
67+
);
68+
69+
// We check whether we have a defaultValue (which must be an array) before checking
70+
// the fields prop which will always be empty for a new record.
71+
// Without it, our ids wouldn't match the default value and we would get key warnings
72+
// on the CssTransition element inside our render method
73+
const ids = useRef(
74+
nextId.current > 0 ? Array.from(Array(nextId.current).keys()) : []
75+
);
5476

5577
const removeField = useCallback(
5678
(index: number) => {
79+
ids.current.splice(index, 1);
5780
fields.remove(index);
5881
},
5982
[fields]
6083
);
6184

6285
const addField = useCallback(
6386
(item: any = undefined) => {
87+
ids.current.push(nextId.current++);
6488
fields.push(item);
6589
},
6690
[fields]
@@ -78,6 +102,9 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
78102

79103
const handleReorder = useCallback(
80104
(origin: number, destination: number) => {
105+
const item = ids.current[origin];
106+
ids.current[origin] = ids.current[destination];
107+
ids.current[destination] = item;
81108
fields.move(origin, destination);
82109
},
83110
[fields]
@@ -102,32 +129,41 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
102129
<ValidationError error={error as string} />
103130
</FormHelperText>
104131
)}
105-
{fields.map((member, index) => (
106-
<SimpleFormIteratorItem
107-
key={member}
108-
basePath={basePath}
109-
classes={classes}
110-
disabled={disabled}
111-
disableRemove={disableRemove}
112-
disableReordering={disableReordering}
113-
fields={fields}
114-
getItemLabel={getItemLabel}
115-
index={index}
116-
margin={margin}
117-
member={member}
118-
meta={meta}
119-
onRemoveField={removeField}
120-
onReorder={handleReorder}
121-
record={(records && records[index]) || {}}
122-
removeButton={removeButton}
123-
reOrderButtons={reOrderButtons}
124-
resource={resource}
125-
source={source}
126-
variant={variant}
127-
>
128-
{children}
129-
</SimpleFormIteratorItem>
130-
))}
132+
<TransitionGroup component={null}>
133+
{fields.map((member, index) => (
134+
<CSSTransition
135+
nodeRef={nodeRef}
136+
key={ids.current[index]}
137+
timeout={500}
138+
classNames="fade"
139+
{...TransitionProps}
140+
>
141+
<SimpleFormIteratorItem
142+
basePath={basePath}
143+
classes={classes}
144+
disabled={disabled}
145+
disableRemove={disableRemove}
146+
disableReordering={disableReordering}
147+
fields={fields}
148+
getItemLabel={getItemLabel}
149+
index={index}
150+
margin={margin}
151+
member={member}
152+
meta={meta}
153+
onRemoveField={removeField}
154+
onReorder={handleReorder}
155+
record={(records && records[index]) || {}}
156+
removeButton={removeButton}
157+
reOrderButtons={reOrderButtons}
158+
resource={resource}
159+
source={source}
160+
variant={variant}
161+
>
162+
{children}
163+
</SimpleFormIteratorItem>
164+
</CSSTransition>
165+
))}
166+
</TransitionGroup>
131167
{!disabled && !disableAdd && (
132168
<li className={classes.line}>
133169
<span className={classes.action}>
@@ -198,6 +234,7 @@ export interface SimpleFormIteratorProps
198234
reOrderButtons?: ReactElement;
199235
resource?: string;
200236
source?: string;
237+
TransitionProps?: CSSTransitionProps;
201238
variant?: 'standard' | 'outlined' | 'filled';
202239
}
203240

0 commit comments

Comments
 (0)