import React from "react" export interface TextAnswerComponentProps { value?: string format: string readOnly?: boolean onValueChange: any onNextOrComments?: (ev: any) => void } interface TextAnswerComponentState { text: any lastValueProp?: string | null } export default class TextAnswerComponent extends React.Component { static defaultProps = { readOnly: false } input: HTMLTextAreaElement | HTMLInputElement | null = null constructor(props: any) { super(props) this.state = { text: props.value != null ? props.value : "", lastValueProp: props.value } } static getDerivedStateFromProps(props: TextAnswerComponentProps, state: TextAnswerComponentState) { if (props.value !== state.lastValueProp) { return { text: props.value != null ? props.value : "", lastValueProp: props.value } } return null } focus() { return this.input!.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() } } } handleBlur = (ev: any) => { return this.props.onValueChange(ev.target.value ? ev.target.value : null) } render() { if (this.props.format === "multiline") { return (