Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

propertyKey is inconsistently provided to property builder function #659

Open
lvl99 opened this issue Aug 14, 2024 · 0 comments
Open

propertyKey is inconsistently provided to property builder function #659

lvl99 opened this issue Aug 14, 2024 · 0 comments

Comments

@lvl99
Copy link

lvl99 commented Aug 14, 2024

Hiya! I've been having a blast with FireCMS v3, and so far all is well. I have found one minor issue, when trying to create a reference to a document within a subcollection whose parent is defined by another property that's within a repeated group property.

Here's a basic example:

import { get } from "lodash";
import { buildCollection } from "@firecms/core";

const exampleCollection = buildCollection({
  // Yadda yadda yadda...,
  properties: {
    example: {
      name: "Example",
      dataType: "array",
      of: {
        dataType: "map",
        properties: {
          parent: {
            name: "Parent",
            dataType: "reference",
            path: "parents",
          },
          child: ({ propertyKey, values }) => {
            // propertyKey is not always defined, so we need to wrap in a conditional
            if (propertyKey) {
              // Get path relative to parent, since we're in an array
              // propertyKey looks something like "example.0.child", so to get the parent
              // we need to remove "child" and replace with "parent", and the retrieve
              // from the values object.
              const parentPropPath = propertyKey.split(".").slice(0, -1);
              parentPropPath.push("parent");
              const parent = get(values, parentPropPath);
              return {
                name: "Child",
                dataType: "reference",
                path: `${parent.pathWithId}/children`,
              }
            } else {
              return {
                name: "Child",
                dataType: "reference",
                path: "parents",
                disabled: {
                  clearOnDisabled: true,
                  disabledMessage: "Please select a parent",
                }
              }
            }
          }
        }
      }
    }
  }
});

The propertyKey seems to only sometimes be populated:

devtools console screenshot from my real-world app I'm working on where you can see propertyKey is null

Meanwhile, is there a better way to point a reference field to a document's subcollection path?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant