import React from "react"; import { FlexTable } from "../.."; import { TableRow, TableCell, AddButton } from "../elements"; import { validateAll } from "../../validation"; // // Supporting functions // ---------------------------------------------------------------------- // - Nodes export const combineChildren = (children: any, newNode: any) => React.Children.toArray([].concat(children, newNode)); // - Rendering export function getItemRenderer({ form, getId, isEditing, editID, row }: FlexTable.EditableTableProps) { const FormComponent = form; return (item: any, children: React.ReactElement[], row: FlexTable.RowProps, defaultRenderer: any) => { return isEditing && getId(item) === editID ? : defaultRenderer(); } } export function getFooter(props: FlexTable.EditableTableProps) { const { form, isEditing, editID, disableInsert, onEdit, insertItem } = props; const FormComponent = form; if (isEditing && editID === null) { return ( ) } else if (disableInsert) { return null; } else { return ( onEdit(null)} /> ); } } // - Saving items export function getSaveHandler(props: FlexTable.FormProps) { const { onSave, items, getState, onValidation, validationTests } = props; const item = JSON.parse(JSON.stringify(getState())); if (!!validationTests) { const validationResult = validateAll(validationTests, { item, items }); if (validationResult.success) { onSave(item); } else { !!onValidation && onValidation(validationResult.results); } } else { onSave(item); } };