Skip to content

Commit

Permalink
#19 - PR feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sven van de Scheur committed May 9, 2023
1 parent dcb1ef6 commit 727269d
Showing 1 changed file with 4 additions and 27 deletions.
31 changes: 4 additions & 27 deletions src/lib/renderer/renderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {Utils} from 'formiojs';
import {ConditionalOptions} from 'formiojs/types/components/schema';
import React, {FormHTMLAttributes, useContext} from 'react';

const getComponent = Utils.getComponent;

export const DEFAULT_RENDER_CONFIGURATION: IRenderConfiguration = {
components: {
columns: Columns,
Expand Down Expand Up @@ -150,12 +148,14 @@ export const RenderComponent = ({
form,
}: IRenderComponentProps): React.ReactElement | null => {
const key = component.key || OF_MISSING_KEY;
const {setFieldValue} = useFormikContext();
const {setFieldValue, values} = useFormikContext();
const Component = useComponentType(component);
const field = useField(key);

// Basic Form.io conditional.
const show = useConditional(component, form);
const show = Utils.hasCondition(component)
? Utils.checkCondition(component, null, values, form, null)
: !component.hidden;

if (!show && component.clearOnHide) {
setFieldValue(key, null);
Expand Down Expand Up @@ -222,26 +222,3 @@ export const useComponentType = (
const ComponentType = renderConfiguration.components[component.type];
return ComponentType || Fallback;
};

/**
* Evaluates the `component.conditional` (if set) and returns whether the component should be shown.
* This does not evaluate complex form logic but merely the basic Form.io conditional logic (found
* in the "Advanced" tab).
*
* @external {FormikContext} Expects `Formik`/`FormikContext` to be available.
* @return {boolean} If a conditional passes, the show argument is returned to respect it's
* configuration. If a conditional does not pass, `!component.hidden` is used as return value.
*/
export const useConditional = (component: IRenderable, form: IFormioForm) => {
const {eq, show, when} = component?.conditional || {};
const isConditionalSet = typeof show == 'boolean' && when;
const otherComponent = getComponent(form.components, when as string, false);
const [{value}] = useField(otherComponent.key || OF_MISSING_KEY);
const isEqual = eq === value;

if (!isConditionalSet || !otherComponent || !isEqual) {
return !component.hidden;
}

return show;
};

0 comments on commit 727269d

Please sign in to comment.