import PropTypes from "prop-types" import _ from "lodash" import React, { ChangeEvent } from "react" import * as formUtils from "./formUtils" import NumberAnswerComponent from "./answers/NumberAnswerComponent" import DateAnswerComponent from "./answers/DateAnswerComponent" import UnitsAnswerComponent from "./answers/UnitsAnswerComponent" import SiteColumnAnswerComponent from "./answers/SiteColumnAnswerComponent" import TextExprsComponent from "./TextExprsComponent" import { Choice, MatrixColumn } from "./formDesign" import { Schema } from "@mwater/expressions" import ResponseRow from "./ResponseRow" import { Answer, ResponseData } from "./response" import { InnerFormContext } from "./formContext" import LargeCheckbox from "./LargeCheckbox" export interface MatrixColumnCellComponentProps { /** Column. See designSchema */ column: MatrixColumn /** Current data of response (for roster entry if in roster) */ data: ResponseData /** ResponseRow object (for roster entry if in roster) */ responseRow: ResponseRow /** Answer of the cell */ answer?: Answer /** Called with new answer of cell */ onAnswerChange: (answer: Answer) => void /** True if invalid */ invalid?: boolean /** Validation message */ invalidMessage?: string /** Schema to use, including form */ schema: Schema /** Form context */ formCtx: InnerFormContext /** Id of the row of the fixed MatrixQuestion that the cell is in. Undefined for roster matrices, * which have no fixed rows and therefore ignore choice row restrictions. */ matrixRowId?: string } export interface MatrixColumnCellComponentState { /** Status of visibility of choices */ choiceVisibility: { [choiceId: string]: boolean } } // Cell of a matrix column export default class MatrixColumnCellComponent extends React.Component { constructor(props: MatrixColumnCellComponentProps) { super(props) this.state = { choiceVisibility: {} } // Set all initially visible, except ones excluded from this row, which can never become visible if (this.props.column._type == "DropdownColumnQuestion") { for (const choice of this.props.column.choices!) { this.state.choiceVisibility[choice.id] = formUtils.isChoiceAllowedInMatrixRow(choice, this.props.matrixRowId) } } } componentDidMount() { this.calculateChoiceVisibility() } componentDidUpdate(prevProps: MatrixColumnCellComponentProps) { // If visibility potentially changed, recalculate if (prevProps.data != this.props.data || prevProps.matrixRowId != this.props.matrixRowId || prevProps.column != this.props.column) { this.calculateChoiceVisibility() } } async calculateChoiceVisibility() { if (this.props.column._type != "DropdownColumnQuestion") { return } const choiceVisibility: { [choiceId: string]: boolean } = {} for (const choice of this.props.column.choices!) { // Row restrictions and choice conditions both apply choiceVisibility[choice.id] = formUtils.isChoiceAllowedInMatrixRow(choice, this.props.matrixRowId) && (await formUtils.isChoiceVisible(choice, this.props.data, this.props.responseRow, this.props.schema)) } this.setState({ choiceVisibility }) } handleValueChange = (value: any) => { return this.props.onAnswerChange(_.extend({}, this.props.answer, { value })) } /** Renders an option for a stored answer that is not allowed in this row, so that the existing * answer remains visible (and correctable) instead of the dropdown appearing empty. */ renderIncompatibleStoredChoice(choices: Choice[], value: any) { if (value == null || value === "") { return null } const choice = _.find(choices, { id: value }) if (!choice || formUtils.isChoiceAllowedInMatrixRow(choice, this.props.matrixRowId)) { return null } const text = formUtils.localizeString(choice.label, this.props.formCtx.locale) return ( ) } render() { let className, elem const { column } = this.props const value = this.props.answer?.value // Create element switch (column._type) { case "Calculation": elem = ( ) break case "TextColumn": elem = ( ) break case "UnitsColumnQuestion": elem = ( ) break case "TextColumnQuestion": elem = ( ) => this.handleValueChange(ev.target.value || null)} /> ) break case "NumberColumnQuestion": elem = ( ) break case "CheckColumnQuestion": elem = ( this.handleValueChange(!value)} > {"\u200B"} ) // ZWSP break case "DropdownColumnQuestion": // Width must stay 100%, never auto. A native select sizes itself to its widest *visible* // option, so once choices are filtered per row (matrixRowIds) an auto-width select renders // at a different width in every row of the same column. Filling the cell keeps the column // aligned no matter which choices a given row shows. elem = ( ) break case "SiteColumnQuestion": elem = ( ) break case "DateColumnQuestion": elem = (
) break } if (this.props.invalid) { className = "invalid" } return ( {elem} {this.props.invalid && !!this.props.invalidMessage ? {this.props.invalidMessage} : undefined} ) } }