import _ from "lodash" 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" export interface DropdownAnswerComponentProps { choices: Choice[] onAnswerChange: any /** See answer format */ answer: any data: any formCtx: InnerFormContext schema: Schema responseRow: ResponseRow } export interface DropdownAnswerComponentState { /** Status of visibility of choices */ choiceVisibility: { [choiceId: string]: boolean } } export default class DropdownAnswerComponent extends React.Component { select: HTMLSelectElement | null = null constructor(props: DropdownAnswerComponentProps) { 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: DropdownAnswerComponentProps) { // 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() { return this.select?.focus() } handleValueChange = (ev: any) => { if (ev.target.value != null && ev.target.value !== "") { return this.props.onAnswerChange({ value: ev.target.value, specify: null }) } else { return this.props.onAnswerChange({ value: null, specify: null }) } } handleSpecifyChange = (id: any, ev: any) => { const change: any = {} change[id] = ev.target.value const specify = _.extend({}, this.props.answer.specify, change) return this.props.onAnswerChange({ value: this.props.answer.value, specify }) } // Render specify input box renderSpecify() { let value const choice = _.findWhere(this.props.choices, { id: this.props.answer.value }) if (choice && choice.specify && this.props.answer.specify != null) { value = this.props.answer.specify[choice.id] } else { value = "" } if (choice && choice.specify) { return ( ) } return null } render() { return (
{this.renderSpecify()}
) } }