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

[UI-270] Adds new option shouldValidateBranch to createValidatedForm for helping to create branching forms. #442

Merged
merged 3 commits into from
Jan 15, 2025

Conversation

George-Payne
Copy link
Member

When a parent validated form is validated, it will only call validation on a nested validated form if branchIsActive returns true (or is undefined).
branchIsActive is passed the data of the top level form which had validation called on it, it's own data, and the reason for validating.

Example usage

interface DinnerForm {
    mealType: string;
    pizzaToppings: {
        pepperoni: boolean;
        pineapple: boolean;
    };
}

const form = createValidatedForm<DinnerForm, DinnerForm>({
    mealType: '',
    pizzaToppings: createValidatedForm<DinnerForm['pizzaToppings'], DinnerForm>(
        {
            pepperoni: true,
            pineapple: true,
        },
        {
            branchIsActive: (root) => root.mealType === 'pizza',
        },
    ),
});

… to create branching forms.

When a parent validated form is validated, it will only call validation on a nested validated form if `branchIsActive` returns true (or isn't specified).
@George-Payne George-Payne self-assigned this Jan 15, 2025
@George-Payne George-Payne added the @kurrent-ui/forms Relating to @kurrent-ui/forms library label Jan 15, 2025
packages/forms/src/utils/expandOptions.ts Outdated Show resolved Hide resolved
@teamchong teamchong changed the title [UI-270] Adds new option branchIsActive to createValidatedForm for helping to create branching forms. [UI-270] Adds new option shouldValidateBranch to createValidatedForm for helping to create branching forms. Jan 15, 2025
@George-Payne George-Payne merged commit 85e2ad3 into main Jan 15, 2025
2 checks passed
@George-Payne George-Payne deleted the toggleable-nested-forms branch January 15, 2025 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@kurrent-ui/forms Relating to @kurrent-ui/forms library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants