diff --git a/src/components/editor/Bindings/FieldSelection/index.tsx b/src/components/editor/Bindings/FieldSelection/index.tsx index 9fcc61c06..ca8a6fc02 100644 --- a/src/components/editor/Bindings/FieldSelection/index.tsx +++ b/src/components/editor/Bindings/FieldSelection/index.tsx @@ -45,7 +45,10 @@ import { } from 'stores/FormState/hooks'; import { FormStatus } from 'stores/FormState/types'; import { Schema } from 'types'; -import { evaluateRequiredIncludedFields } from 'utils/workflow-utils'; +import { + evaluateRequiredIncludedFields, + getBindingIndex, +} from 'utils/workflow-utils'; interface Props { collectionName: string; @@ -162,11 +165,14 @@ function FieldSelectionViewer({ collectionName }: Props) { ) )?.constraints; + const bindingIndex: number = getBindingIndex( + draftSpecs[0].spec.bindings, + collectionName + ); const selectedBinding: Schema | undefined = - draftSpecs[0].spec.bindings.find( - (binding: any) => binding.source === collectionName - ); - + bindingIndex > -1 + ? draftSpecs[0].spec.bindings[bindingIndex] + : undefined; let evaluatedFieldMetadata: FieldMetadata | undefined; if ( diff --git a/src/components/editor/Bindings/FieldSelection/useFieldSelection.ts b/src/components/editor/Bindings/FieldSelection/useFieldSelection.ts index afeee4456..c351f60b6 100644 --- a/src/components/editor/Bindings/FieldSelection/useFieldSelection.ts +++ b/src/components/editor/Bindings/FieldSelection/useFieldSelection.ts @@ -12,6 +12,7 @@ import { omit } from 'lodash'; import { useCallback } from 'react'; import { Schema } from 'types'; import { hasLength } from 'utils/misc-utils'; +import { getBindingIndex } from 'utils/workflow-utils'; function useFieldSelection(collectionName: string) { // Bindings Editor Store @@ -24,8 +25,12 @@ function useFieldSelection(collectionName: string) { return useCallback( async (draftSpec: DraftSpecQuery) => { - const bindingIndex: number = draftSpec.spec.bindings.findIndex( - (binding: any) => binding.source === collectionName + // TODO (field selection) we should make it so this does not need to be figured out + // every call as it is pretty wasteful. Since we are passing in the spec already maybe + // we just pass in the index along with it? Not sure how to do this and make it feel good. + const bindingIndex: number = getBindingIndex( + draftSpec.spec.bindings, + collectionName ); if (!mutateDraftSpecs || bindingIndex === -1) { diff --git a/src/utils/workflow-utils.ts b/src/utils/workflow-utils.ts index 68cf25134..c50e1522b 100644 --- a/src/utils/workflow-utils.ts +++ b/src/utils/workflow-utils.ts @@ -44,6 +44,17 @@ export const getCollectionName = (binding: any) => { return getCollectionNameDirectly(scopedBinding); }; +export const getBindingIndex = ( + bindings: any[] | null | undefined, + collectionName: string +) => { + return bindings?.findIndex + ? bindings.findIndex( + (binding: any) => getCollectionName(binding) === collectionName + ) + : -1; +}; + export const getDisableProps = (disable: boolean | undefined) => { return disable ? { disable } : {}; };