import React from "react" import * as formUtils from "../formUtils" import { Choice } from "../formDesign" import { Schema } from "@mwater/expressions" import ResponseRow from "../ResponseRow" import { InnerFormContext } from "../formContext" import LargeCheckbox from "../LargeCheckbox" export interface MulticheckAnswerComponentProps { choices: Choice[] /** See answer format */ answer: any onAnswerChange: any data: any schema: Schema responseRow: ResponseRow formCtx: InnerFormContext } export interface MulticheckAnswerComponentState { /** Status of visibility of choices */ choiceVisibility: { [choiceId: string]: boolean } } // Multiple checkboxes where more than one can be checked export default class MulticheckAnswerComponent extends React.Component { constructor(props: MulticheckAnswerComponentProps) { super(props) this.state = { choiceVisibility: {} } // Set all initially visible for (const choice of this.props.choices) { this.state.choiceVisibility[choice.id] = true } } componentDidMount() { this.calculateChoiceVisibility() } componentDidUpdate(prevProps: MulticheckAnswerComponentProps) { // If visibility potentially changed, recalculate if (prevProps.data != this.props.data) { this.calculateChoiceVisibility() } } async calculateChoiceVisibility() { const choiceVisibility: { [choiceId: string]: boolean } = {} for (const choice of this.props.choices) { choiceVisibility[choice.id] = await formUtils.isChoiceVisible(choice, this.props.data, this.props.responseRow, this.props.schema) } this.setState({ choiceVisibility }) } focus() { // Nothing to focus return null } handleValueChange = (choice: Choice) => { const ids = this.props.answer.value || [] if (ids.includes(choice.id)) { let specify if (this.props.answer.specify != null) { specify = { ...this.props.answer.specify } if (specify[choice.id] != null) { delete specify[choice.id] } } else { specify = null } return this.props.onAnswerChange({ value: ids.filter((id: string) => id !== choice.id), specify }) } else { return this.props.onAnswerChange({ value: [...ids, choice.id], specify: this.props.answer.specify }) } } handleSpecifyChange = (id: any, ev: React.ChangeEvent) => { const change: any = {} change[id] = ev.target.value const specify = { ...this.props.answer.specify, ...change } return this.props.onAnswerChange({ value: this.props.answer.value, specify }) } // Render specify input box renderSpecify(choice: Choice) { let value if (this.props.answer.specify != null) { value = this.props.answer.specify[choice.id] } else { value = "" } return ( this.handleSpecifyChange(choice.id, ev)} /> ) } renderChoice(choice: Choice) { if (!this.state.choiceVisibility[choice.id]) { return null } const selected = Array.isArray(this.props.answer.value) && this.props.answer.value.includes(choice.id) return (
this.handleValueChange(choice)} value={selected} > {formUtils.localizeString(choice.label, this.props.formCtx.locale)} {choice.hint ? ( {formUtils.localizeString(choice.hint, this.props.formCtx.locale)} ) : undefined} {choice.specify && selected ? this.renderSpecify(choice) : undefined}
) } render() { return (
{this.props.choices.map((choice) => this.renderChoice(choice))}
) } }