import { VevProps } from '@vev/utils'; import React, { useEffect, useState } from 'react'; import { SilkeFormProps } from '../silke-form'; import { SilkeSchemaField } from './silke-schema-field'; import { SilkeBox } from '../silke-box'; import { isFunction } from 'lodash'; import { useSchemaContext } from './silke-schema-context'; type SilkeSchemaFieldsProps = { schema: VevProps[]; noPad?: boolean; row?: boolean; disabled?: boolean; readonly?: boolean; itemIndex?: number; } & SilkeFormProps; export function SilkeSchemaFields( props: SilkeSchemaFieldsProps, ) { const { schema = [], row, ...rest } = props; const context = useSchemaContext(); const [filteredSchema, setFilteredSchema] = useState(schema); // Filter hidden fields here, so they don't use any space in the sidebar useEffect(() => { const ctxWithValue = { ...context, value: rest?.value ?? context.value }; const isHidden = async (field: VevProps) => { if (isFunction(field?.hidden)) { return await field?.hidden(ctxWithValue, rest?.itemIndex); } else if (field?.hidden) { return true; } return false; }; Promise.all( schema.map(async (field) => { const hidden = await isHidden(field); return hidden ? null : field; }), ).then((value) => { const filtered = value.filter((field) => { return field !== null; }) as VevProps[]; setFilteredSchema(filtered); }); }, [context, schema, rest?.itemIndex, rest?.value]); return ( {filteredSchema.map((field, index) => { return ( ); })} ); }