Skip to content

Commit 3246d0e

Browse files
authored
chore(compass-components): Do not use uuid as a key for Documents and Elements (#3080)
1 parent abbc025 commit 3246d0e

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

packages/compass-components/src/components/document-list/document.tsx

+11-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import { fontFamilies, spacing } from '@leafygreen-ui/tokens';
99
import { AutoFocusContext } from './auto-focus-context';
1010
import { useForceUpdate } from './use-force-update';
1111
import { HadronElement } from './element';
12+
import { usePrevious } from './use-previous';
1213

1314
function useHadronDocument(doc: HadronDocumentType) {
15+
const prevDoc = usePrevious(doc);
1416
const forceUpdate = useForceUpdate();
1517

1618
const onDocumentFieldsAddedOrRemoved = useCallback(
@@ -28,6 +30,13 @@ function useHadronDocument(doc: HadronDocumentType) {
2830
[doc, forceUpdate]
2931
);
3032

33+
useEffect(() => {
34+
// Force update if the document that was passed to the component changed
35+
if (prevDoc && prevDoc !== doc) {
36+
forceUpdate();
37+
}
38+
}, [prevDoc, doc, forceUpdate]);
39+
3140
useEffect(() => {
3241
doc.on(ElementEvents.Added, onDocumentFieldsAddedOrRemoved);
3342
doc.on(ElementEvents.Removed, onDocumentFieldsAddedOrRemoved);
@@ -91,11 +100,11 @@ const HadronDocument: React.FunctionComponent<{
91100
data-id={document.uuid}
92101
>
93102
<AutoFocusContext.Provider value={autoFocus}>
94-
{visibleElements.map((el) => {
103+
{visibleElements.map((el, idx) => {
95104
return (
96105
<HadronElement
106+
key={idx}
97107
value={el}
98-
key={el.uuid}
99108
editable={editable}
100109
editingEnabled={editing}
101110
allExpanded={expanded}

packages/compass-components/src/components/document-list/element.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { EditActions, AddFieldActions } from './element-actions';
1919
import { FontAwesomeIcon } from './font-awesome-icon';
2020
import { useAutoFocusContext } from './auto-focus-context';
2121
import { useForceUpdate } from './use-force-update';
22+
import { usePrevious } from './use-previous';
2223

2324
function getEditorByType(type: HadronElementType['type']) {
2425
switch (type) {
@@ -54,6 +55,7 @@ function useElementEditor(el: HadronElementType) {
5455

5556
function useHadronElement(el: HadronElementType) {
5657
const forceUpdate = useForceUpdate();
58+
const prevEl = usePrevious(el);
5759
const editor = useElementEditor(el);
5860
// NB: Duplicate key state is kept local to the component and not derived on
5961
// every change so that only the changed key is highlighed as duplicate
@@ -82,6 +84,12 @@ function useHadronElement(el: HadronElementType) {
8284
[el, forceUpdate]
8385
);
8486

87+
useEffect(() => {
88+
if (prevEl && prevEl !== el) {
89+
forceUpdate();
90+
}
91+
}, [el, prevEl, forceUpdate]);
92+
8593
useEffect(() => {
8694
el.on(ElementEvents.Converted, onElementChanged);
8795
el.on(ElementEvents.Edited, onElementChanged);
@@ -550,10 +558,10 @@ export const HadronElement: React.FunctionComponent<{
550558
</div>
551559
{expandable &&
552560
expanded &&
553-
children.map((el) => {
561+
children.map((el, idx) => {
554562
return (
555563
<HadronElement
556-
key={el.uuid}
564+
key={idx}
557565
value={el}
558566
editable={editable}
559567
editingEnabled={editingEnabled}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { useEffect, useRef } from 'react';
2+
3+
export function usePrevious<T>(value: T): T | undefined {
4+
const ref = useRef<T | undefined>();
5+
useEffect(() => {
6+
ref.current = value;
7+
}, [value]);
8+
return ref.current;
9+
}

packages/compass-crud/src/components/document-list-view.jsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ class DocumentListView extends React.Component {
2929
* @return {Array} The document list item components.
3030
*/
3131
renderDocuments() {
32-
return this.props.docs.map((doc) => {
32+
return this.props.docs.map((doc, index) => {
3333
return (
34-
<li className={LIST_ITEM_CLASS} data-test-id={LIST_ITEM_TEST_ID} key={doc.uuid}>
34+
<li className={LIST_ITEM_CLASS} data-test-id={LIST_ITEM_TEST_ID} key={index}>
3535
<Document
36-
key={doc.uuid}
3736
doc={doc}
3837
editable={this.props.isEditable}
3938
isTimeSeries={this.props.isTimeSeries}

0 commit comments

Comments
 (0)