Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,42 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
disableRemove,
disableReordering,
TransitionProps,
defaultValue,
getItemLabel = DefaultLabelFn,
} = props;
const { append, fields, move, remove } = useArrayInput(props);
const nodeRef = useRef();
// const { error, submitFailed } = meta;

// We need a unique id for each field for a proper enter/exit animation
// so we keep an internal map between the field position and an auto-increment id
const nextId = useRef(
fields && fields.length
? fields.length
: defaultValue
? defaultValue.length
: 0
);

// We check whether we have a defaultValue (which must be an array) before checking
// the fields prop which will always be empty for a new record.
// Without it, our ids wouldn't match the default value and we would get key warnings
// on the CssTransition element inside our render method
const ids = useRef(
nextId.current > 0 ? Array.from(Array(nextId.current).keys()) : []
);

const removeField = useCallback(
(index: number) => {
ids.current.splice(index, 1);
remove(index);
},
[remove]
);

const addField = useCallback(
(item: any = undefined) => {
ids.current.push(nextId.current++);
append(item);
},
[append]
Expand All @@ -76,6 +97,9 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {

const handleReorder = useCallback(
(origin: number, destination: number) => {
const item = ids.current[origin];
ids.current[origin] = ids.current[destination];
ids.current[destination] = item;
move(origin, destination);
},
[move]
Expand All @@ -99,7 +123,7 @@ export const SimpleFormIterator = (props: SimpleFormIteratorProps) => {
{fields.map((member, index) => (
<CSSTransition
nodeRef={nodeRef}
key={member.id}
key={ids.current[index]}
timeout={500}
classNames="fade"
{...TransitionProps}
Expand Down