import { FCC, FormContext, FormDisplayerProps, useFieldContainer, useForm, useFormDecorator, useFormMetadata, } from 'onekijs-framework'; import React, { useEffect, useRef } from 'react'; import Card from '.'; import { FormCardProps } from './typings'; import { titlelize } from '../../utils/misc'; import FormCardDisplayer from './FormCardDisplayer'; import Alert from '../alert'; const FormCard: FCC = ({ name, help, children, visible, disabled, ...cardProps }) => { const decorator = useFormDecorator(name, { Displayer: (displayerProps: FormDisplayerProps) => { return ( ); }, visible, disabled, }); const form = useForm(); const metadata = useFormMetadata(decorator.name); const fieldContainer = useFieldContainer({ container: name, }); const { hide, show, disable, enable } = fieldContainer; const initializedRef = useRef(false); useEffect(() => { const visible = metadata.visible ?? true; const disabled = metadata.disabled ?? false; if (!visible) { if (disabled) { disable(); } else { if (initializedRef.current === true) { // we only use the enable method if this is a change and not a initialization enable(); } } hide(); } else { if (initializedRef.current === true) { // we only use the show method if this is a change and not a initialization show(); } if (disabled) { disable(); } else { if (initializedRef.current === true) { // we only use the enable method if this is a change and not a initialization enable(); } } } initializedRef.current = true; }, [metadata.visible, metadata.disabled, hide, show, disable, enable]); if (metadata.visible === false) { return null; } return ( (metadata.disabled ? form.enable(decorator.name) : form.disable(decorator.name))} > <> {help && ( {help} )} {children} ); }; export default FormCard;