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

Update useConditional.js with docs #462

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions src/hooks/useConditional.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@ import { ScopeContext } from '../Context';
import { useFormController } from './useFormController';
import { useFieldSubscription } from './useFieldSubscription';
import { Debug } from '../debug';

const logger = Debug('informed:useConditional' + '\t');

/* ----------------------- useDepends ----------------------- */
/**
* Custom hook for handling conditional logic based on form state.
*
* @param {object} options - The options for conditional evaluation.
* @param {string} options.name - The name of the conditional field.
* @param {function} options.evaluate - The evaluation function for conditional logic.
* @param {Array<string|number|symbol>} [options.evaluateWhen=[]] - The fields to watch for triggering re-evaluation.
* @param {Array<string>} [options.dependsOn=[]] - The fields that the evaluation depends on.
* @param {boolean} [options.native=false] - Whether to use native events for evaluation.
* @returns {object} - The props object with conditional properties.
*/
export const useConditional = ({
name,
evaluate,
Expand Down Expand Up @@ -38,18 +49,15 @@ export const useConditional = ({

const check = typeof evaluateWhen === 'function' ? [] : evaluateWhen;

const fields = useMemo(
() => {
if (typeof evaluateWhen === 'function') {
// Generate fields array with scope
// Example: evaluateWhen = scope => [`${scope}.foo`, `${scope}.bar`]
return evaluateWhen(scope);
}
// Example evaluateWhen = ["name", "age"]
return evaluateWhen;
},
[...check, scope]
);
const fields = useMemo(() => {
if (typeof evaluateWhen === 'function') {
// Generate fields array with scope
// Example: evaluateWhen = scope => [`${scope}.foo`, `${scope}.bar`]
return evaluateWhen(scope);
}
// Example evaluateWhen = ["name", "age"]
return evaluateWhen;
}, [...check, scope]);

const event = native ? 'field-native' : 'field-value';

Expand All @@ -73,22 +81,19 @@ export const useConditional = ({
!(typeof evaluateWhen === 'function')
);

useEffect(
() => {
if (evaluate) {
// When name changes we always check if relevant
setProps(
evaluate({
formState: formController.getFormState(),
formApi: formController.getFormApi(),
scope,
dependsOn
})
);
}
},
[name, ...dependsOn]
);
useEffect(() => {
if (evaluate) {
// When name changes we always check if relevant
setProps(
evaluate({
formState: formController.getFormState(),
formApi: formController.getFormApi(),
scope,
dependsOn
})
);
}
}, [name, ...dependsOn]);

// Trigger evaluate on a reset of form
useEffect(() => {
Expand Down