Skip to content

Commit 1c2571c

Browse files
authored
fix(crud): Fix adding a field in the CRUD table view COMPASS-6359 (#3900)
Fix adding a field in the CRUD table view
1 parent 6b18b1f commit 1c2571c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

packages/compass-crud/src/components/table-view/add-field-button.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
MenuItem,
99
} from '@mongodb-js/compass-components';
1010
import type { CellEditorProps } from './cell-editor';
11+
import type Document from 'hadron-document';
1112
import type { Element } from 'hadron-document';
1213

1314
/**
@@ -82,19 +83,23 @@ class AddFieldButton extends React.Component<
8283

8384
handleAddFieldClick() {
8485
const document = this.props.node.data.hadronDocument;
86+
let parent: Document | Element = this.props.node.data.hadronDocument;
8587
let editOnly = false;
8688

87-
if (!this.props.context.path.length) {
88-
return;
89+
// By default and for top-level fields the path is empty.
90+
// Once the user drills down, the path changes.
91+
// If the user clicks on the breadcrumb component, then the path also
92+
// changes.
93+
if (this.props.context.path.length) {
94+
parent = document.getChild(this.props.context.path)!;
8995
}
90-
const parent = document.getChild(this.props.context.path)!;
9196

9297
const isArray = !parent.isRoot() && parent.currentType === 'Array';
9398
let newElement: Element;
9499

95100
if (!this.empty) {
96101
/* Set key to $new even though for arrays, it will be a index */
97-
newElement = parent.insertAfter(this.props.value, '$new', '');
102+
newElement = parent.insertAfter(this.props.value, '$new', '')!;
98103
} else {
99104
newElement = parent.insertEnd('$new', '');
100105
}

0 commit comments

Comments
 (0)