Add optional submit function for text areas

This commit is contained in:
Rene Pfeuffer
2019-12-10 11:29:55 +01:00
parent d521de6d07
commit 0ae185ef01

View File

@@ -1,4 +1,4 @@
import React, { ChangeEvent } from "react"; import React, { ChangeEvent, KeyboardEvent } from "react";
import LabelWithHelpIcon from "./LabelWithHelpIcon"; import LabelWithHelpIcon from "./LabelWithHelpIcon";
type Props = { type Props = {
@@ -10,6 +10,7 @@ type Props = {
onChange: (value: string, name?: string) => void; onChange: (value: string, name?: string) => void;
helpText?: string; helpText?: string;
disabled?: boolean; disabled?: boolean;
onSubmit?: () => void;
}; };
class Textarea extends React.Component<Props> { class Textarea extends React.Component<Props> {
@@ -25,6 +26,13 @@ class Textarea extends React.Component<Props> {
this.props.onChange(event.target.value, this.props.name); this.props.onChange(event.target.value, this.props.name);
}; };
onKeyPress = (event: KeyboardEvent<HTMLTextAreaElement>) => {
const { onSubmit } = this.props;
if (onSubmit && event.key === "Enter" && event.ctrlKey) {
onSubmit();
}
};
render() { render() {
const { placeholder, value, label, helpText, disabled } = this.props; const { placeholder, value, label, helpText, disabled } = this.props;
@@ -41,6 +49,7 @@ class Textarea extends React.Component<Props> {
onChange={this.handleInput} onChange={this.handleInput}
value={value} value={value}
disabled={!!disabled} disabled={!!disabled}
onKeyPress={this.onKeyPress}
/> />
</div> </div>
</div> </div>