Skip to content

Commit b0d3c3f

Browse files
committed
feat: use nested key path
1 parent 7c152ed commit b0d3c3f

File tree

1 file changed

+27
-1
lines changed
  • packages/compass-components/src/components/document-list

1 file changed

+27
-1
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,29 @@ const isValidUrl = (str: string): boolean => {
420420
}
421421
};
422422

423+
/**
424+
* Helper function to get the nested key path of an element, skips array keys
425+
* Meant for keypaths used in query conditions from a selected element
426+
*/
427+
const getNestedKeyPath = (element: HadronElementType): string => {
428+
let keyPath = '';
429+
let currentElement: HadronElementType | HadronDocumentType | null = element;
430+
while (
431+
currentElement &&
432+
'parent' in currentElement &&
433+
currentElement.parent
434+
) {
435+
if (currentElement.parent.currentType !== 'Array') {
436+
keyPath =
437+
keyPath === ''
438+
? currentElement.currentKey.toString()
439+
: currentElement.currentKey.toString() + '.' + keyPath;
440+
}
441+
currentElement = currentElement.parent;
442+
}
443+
return keyPath;
444+
};
445+
423446
export const HadronElement: React.FunctionComponent<{
424447
value: HadronElementType;
425448
editable: boolean;
@@ -469,7 +492,10 @@ export const HadronElement: React.FunctionComponent<{
469492
{
470493
label: 'Add to query',
471494
onAction: () => {
472-
onAddToQuery(key.value, element.generateObject());
495+
onAddToQuery(
496+
getNestedKeyPath(element),
497+
element.generateObject()
498+
);
473499
},
474500
},
475501
]

0 commit comments

Comments
 (0)