firecmsco / firecms

Awesome Firebase/Firestore-based CMS. The missing admin panel for your Firebase project!
https://firecms.co
Other
1.14k stars 185 forks source link

propertyKey is inconsistently provided to property builder function #659

Open lvl99 opened 4 weeks ago

lvl99 commented 4 weeks ago

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?