import _ from "lodash" import React from "react" import * as formUtils from "../formUtils" import MatrixColumnCellComponent from "../MatrixColumnCellComponent" import { ResponseData } from "../response" import ResponseRow from "../ResponseRow" import ValidationCompiler from "./ValidationCompiler" import { InnerFormContext } from "../formContext" import { Schema } from "@mwater/expressions" import { MatrixColumn, Choices } from "../formDesign" export interface MatrixAnswerComponentProps { /** Array of matrix items */ items: Choices /** Array of matrix columns */ columns: MatrixColumn[] /** See answer format */ value?: any onValueChange: any /** Alternate value if selected */ alternate?: string | null /** Current data of response (for roster entry if in roster) */ data: ResponseData /** ResponseRow object (for roster entry if in roster) */ responseRow: ResponseRow /** Form schema */ schema: Schema /** Form context */ formCtx: InnerFormContext } interface MatrixAnswerComponentState { validationErrors: any } // Matrix with columns and items export default class MatrixAnswerComponent extends React.Component< MatrixAnswerComponentProps, MatrixAnswerComponentState > { constructor(props: MatrixAnswerComponentProps) { super(props) this.state = { validationErrors: {} // Map of "_" to validation error } } focus() { // TODO return null } // Validate a matrix answer. Returns true if invalid was found, false otherwise validate() { // Alternate selected means cannot be invalid if (this.props.alternate) { return false } const validationErrors: { [key: string]: string | true } = {} // Important to let know the caller if something has been found (so it can scrollToFirst properly) let foundInvalid = false // For each entry for (let rowIndex = 0; rowIndex < this.props.items.length; rowIndex++) { // For each column const item = this.props.items[rowIndex] for (let columnIndex = 0; columnIndex < this.props.columns.length; columnIndex++) { const column = this.props.columns[columnIndex] const key = `${item.id}_${column._id}` if (column._type == "TextColumn" || column._type == "Calculation") { continue } const data = this.props.value?.[item.id]?.[column._id] if (column.required && !data?.alternate && (data?.value == null || data?.value === "")) { foundInvalid = true validationErrors[key] = true continue } if (column.validations && column.validations.length > 0) { const validationError = new ValidationCompiler(this.props.formCtx.locale).compileValidations(column.validations)( data ) if (validationError) { foundInvalid = true validationErrors[key] = validationError } } // Reject a stored choice that is not allowed in this row. The answer itself is left // untouched so nothing is silently lost. if (formUtils.isMatrixCellAnswerIncompatibleWithRow(column, item.id, data?.value)) { foundInvalid = true validationErrors[key] = this.props.formCtx.T("Not an allowed choice for this row") } } } // Save state this.setState({ validationErrors }) return foundInvalid } handleCellChange = (item: any, column: any, answer: any) => { let matrixValue = this.props.value || {} // Get data of the item, which is indexed by item id in the answer let itemData = matrixValue[item.id] || {} // Set column in item let change: any = {} change[column._id] = answer itemData = _.extend({}, itemData, change) // Set item data within value change = {} change[item.id] = itemData matrixValue = _.extend({}, matrixValue, change) return this.props.onValueChange(matrixValue) } renderColumnHeader(column: any, index: any) { return ( {formUtils.localizeString(column.text, this.props.formCtx.locale)} {/* Required star */} {column.required ? * : undefined} ) } // Render the header row renderHeader() { return ( {/* First item */} {_.map(this.props.columns, (column, index) => this.renderColumnHeader(column, index))} ) } renderCell(item: any, itemIndex: any, column: any, columnIndex: any) { const matrixValue = this.props.value || {} // Get data of the item, which is indexed by item id in the answer const itemData = matrixValue[item.id] || {} // Get cell answer which is inside the item data, indexed by column id const cellAnswer = itemData[column._id] || {} // Determine if invalid const key = `${item.id}_${column._id}` const invalid = this.state.validationErrors[key] // Render cell return ( ) } renderItem(item: any, index: any) { return ( {item.hint ? [
,
{formUtils.localizeString(item.hint, this.props.formCtx.locale)}
] : undefined} {_.map(this.props.columns, (column, columnIndex) => this.renderCell(item, index, column, columnIndex))} ) } render() { // Create table return ( {this.renderHeader()} {_.map(this.props.items, (item, index) => this.renderItem(item, index))}
) } }