import React from "react" import * as formUtils from "../formUtils" import * as ui from "@mwater/react-library/lib/bootstrap" import { InnerFormContext } from "../formContext" export interface UnitsAnswerComponentProps { answer: any onValueChange: any units: any defaultUnits?: string prefix: boolean decimal: boolean onNextOrComments?: (ev: any) => void formCtx: InnerFormContext } interface UnitsAnswerComponentState { selectedUnits: any quantity: any } // Not tested export default class UnitsAnswerComponent extends React.Component< UnitsAnswerComponentProps, UnitsAnswerComponentState > { units: HTMLSelectElement | null = null quantity: ui.NumberInputHandle | null = null constructor(props: any) { super(props) this.state = { quantity: this.getSelectedQuantity(props.answer), selectedUnits: this.getSelectedUnit(props.answer) } } componentDidUpdate(prevProps: UnitsAnswerComponentProps) { if (prevProps.answer !== this.props.answer || prevProps.defaultUnits !== this.props.defaultUnits) { this.setState({ quantity: this.getSelectedQuantity(this.props.answer), selectedUnits: this.getSelectedUnit(this.props.answer) }) } } focus() { if (this.props.prefix) { return this.quantity!.focus() } else { return this.units!.focus() } } handleKeyDown = (ev: any) => { if (this.props.onNextOrComments != null) { // When pressing ENTER or TAB if (ev.keyCode === 13 || ev.keyCode === 9) { this.props.onNextOrComments(ev) // It's important to prevent the default behavior when handling tabs (or else the tab is applied after the focus change) return ev.preventDefault() } } } handleInternalNext = (ev: any) => { // When pressing ENTER or TAB if (ev.keyCode === 13 || ev.keyCode === 9) { if (this.props.prefix) { this.quantity!.focus() } else { this.units!.focus() } // It's important to prevent the default behavior when handling tabs (or else the tab is applied after the focus change) return ev.preventDefault() } } handleValueChange = (val: any) => { return this.changed(val, this.state.selectedUnits) } handleUnitChange = (val: any) => { return this.changed(this.state.quantity, val.target.value) } changed(quantity: any, unit: any) { unit = unit ? unit : this.props.defaultUnits return this.props.onValueChange({ quantity, units: unit }) } getSelectedUnit(answer: any) { if (answer.value != null) { return answer.value.units } if (this.props.defaultUnits != null) { return this.props.defaultUnits } return null } getSelectedQuantity(answer: any) { if (answer.value?.quantity != null) { return answer.value.quantity } return null } createNumberInput() { return ( { this.quantity = c }} decimal={this.props.decimal} value={this.state.quantity != null ? this.state.quantity : undefined} onChange={this.handleValueChange} onTab={this.props.prefix ? this.props.onNextOrComments : this.handleInternalNext} onEnter={this.props.prefix ? this.props.onNextOrComments : this.handleInternalNext} /> ) } render() { return ( {!this.props.prefix ? this.createNumberInput() : undefined} {this.props.prefix ? this.createNumberInput() : undefined}
) } }