Conditional fields from properties
When defining the properties of a collection, you can choose to use a builder
PropertyBuilder, instead of assigning the
property configuration directly.
This is useful for changing property configurations like available values on the fly, based on other values.
Example 1
Section titled “Example 1”Example of field that gets enabled or disabled based on other values:
import { buildCollection, EntityCollection, EntityReference} from "@firecms/core";
type Product = { name: string; main_image: string; available: boolean; price: number; related_products: EntityReference[]; publisher: { name: string; external_id: string; }}
export const productCollection: EntityCollection = buildCollection<Partial<Product>>({ name: "Product", properties: { available: { dataType: "boolean", name: "Available" }, price: ({ values }) => ({ dataType: "number", name: "Price", validation: { requiredMessage: "You must set a price between 0 and 1000", min: 0, max: 1000 }, disabled: !values.available && { clearOnDisabled: true, disabledMessage: "You can only set the price on available items" }, description: "Price with range validation" }) }});Example 2
Section titled “Example 2”A User type that has a source field that can be of type facebook
or apple, and its fields change accordingly
import { buildCollection, EntityCollection, buildProperty, buildProperties} from "@firecms/core";
type User = { source: { type: "facebook", facebookId: string } | { type: "apple", appleId: number }}
export const userSchema: EntityCollection = buildCollection<User>({ name: "User", properties: { source: ({ values }) => { const properties = buildProperties<any>({ type: { dataType: "string", enumValues: { "facebook": "FacebookId", "apple": "Apple" } } });
if (values.source) { if ((values.source as any).type === "facebook") { properties["facebookId"] = buildProperty({ dataType: "string" }); } else if ((values.source as any).type === "apple") { properties["appleId"] = buildProperty({ dataType: "number" }); } }
return ({ dataType: "map", name: "Source", properties: properties }); } }});