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

feat(app-headless-cms): create bind component on field config change #4086

Merged
merged 1 commit into from
Apr 22, 2024

Conversation

Pavel910
Copy link
Collaborator

@Pavel910 Pavel910 commented Apr 17, 2024

Changes

This PR improves the useBind hook in the Headless CMS, and ensures that the Bind component is recreated in case field definition changes at runtime. This allows use cases like conditional help texts, conditional validation, altering validation rules depending on related form fields, etc.

Example

import React from "react";
import { Admin } from "@webiny/app-serverless-cms";
import { Cognito } from "@webiny/app-admin-users-cognito";
import { useParentField, ContentEntryEditorConfig } from "@webiny/app-headless-cms";
import "./App.scss";

const { FieldElement } = ContentEntryEditorConfig;

const lowValidator = [
    {
        name: "lte",
        message: "Value is too high! Enter value lower than 100.",
        settings: {
            value: 99
        }
    }
];

const mediumValidator = [
    {
        name: "gte",
        message: "Value is too low! Enter value between 100 and 500.",
        settings: {
            value: 100
        }
    },
    {
        name: "lte",
        message: "Value is too high! Enter value between 100 and 500.",
        settings: {
            value: 500
        }
    }
];

const highValidator = [
    {
        name: "gte",
        message: "Value is too low! Enter value above 500.",
        settings: {
            value: 500
        }
    }
];

const ConditionalRendering = FieldElement.createDecorator(Original => {
    return function ConditionalRender(props) {
        const parent = useParentField();

        if (!parent || !parent.value) {
            return <Original {...props} />;
        }

        const field = props.field;
        if (field.fieldId === "price") {
            if (parent.value["pricingClass"] === "low") {
                return (
                    <Original
                        {...props}
                        field={{
                            ...field,
                            helpText: "Enter value up to 100.",
                            validation: lowValidator
                        }}
                    />
                );
            }

            if (parent.value["pricingClass"] === "medium") {
                return (
                    <Original
                        {...props}
                        field={{
                            ...field,
                            helpText: "Enter value between 100 and 500.",
                            validation: mediumValidator
                        }}
                    />
                );
            }

            if (parent.value["pricingClass"] === "high") {
                return (
                    <Original
                        {...props}
                        field={{
                            ...field,
                            helpText: "Enter value above 500.",
                            validation: highValidator
                        }}
                    />
                );
            }
        }

        return <Original {...props} />;
    };
});

export const App = () => {
    return (
        <Admin>
            <Cognito />
            <ContentEntryEditorConfig>
                <ConditionalRendering modelIds={["conditionalValidation"]} />
            </ContentEntryEditorConfig>
        </Admin>
    );
};

How Has This Been Tested?

Manually.

@Pavel910 Pavel910 added this to the 5.40.0 milestone Apr 17, 2024
@Pavel910 Pavel910 self-assigned this Apr 17, 2024
@Pavel910 Pavel910 changed the title fix(app-headless-cms): create bind component on field config change feat(app-headless-cms): create bind component on field config change Apr 17, 2024
@Pavel910 Pavel910 merged commit cc06068 into next Apr 22, 2024
87 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant