mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-18 03:01:05 +01:00
handle submit key press on mac osx
This commit is contained in:
56
scm-ui/ui-components/src/forms/Textarea.stories.tsx
Normal file
56
scm-ui/ui-components/src/forms/Textarea.stories.tsx
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import React, {useState} from "react";
|
||||||
|
import { storiesOf } from "@storybook/react";
|
||||||
|
import styled from "styled-components";
|
||||||
|
import Textarea from "./Textarea";
|
||||||
|
|
||||||
|
const Spacing = styled.div`
|
||||||
|
padding: 2em;
|
||||||
|
`;
|
||||||
|
|
||||||
|
const OnChangeTextarea = () => {
|
||||||
|
const [value, setValue] = useState("Start typing");
|
||||||
|
return (
|
||||||
|
<Spacing>
|
||||||
|
<Textarea value={value} onChange={v => setValue(v)} />
|
||||||
|
<hr />
|
||||||
|
<p>{value}</p>
|
||||||
|
</Spacing>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const OnSubmitTextare = () => {
|
||||||
|
const [value, setValue] = useState("Use the ctrl/command + Enter to submit the textarea");
|
||||||
|
const [submitted, setSubmitted] = useState("");
|
||||||
|
|
||||||
|
const submit = () => {
|
||||||
|
setSubmitted(value);
|
||||||
|
setValue("");
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Spacing>
|
||||||
|
<Textarea value={value} onChange={v => setValue(v)} onSubmit={submit} />
|
||||||
|
<hr />
|
||||||
|
<p>{submitted}</p>
|
||||||
|
</Spacing>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const OnCancelTextare = () => {
|
||||||
|
const [value, setValue] = useState("Use the escape key to clear the textarea");
|
||||||
|
|
||||||
|
const cancel = () => {
|
||||||
|
setValue("");
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Spacing>
|
||||||
|
<Textarea value={value} onChange={v => setValue(v)} onCancel={cancel} />
|
||||||
|
</Spacing>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
storiesOf("Forms|Textarea", module)
|
||||||
|
.add("OnChange", () => <OnChangeTextarea />)
|
||||||
|
.add("OnSubmit", () => <OnSubmitTextare />)
|
||||||
|
.add("OnCancel", () => <OnCancelTextare />);
|
||||||
@@ -27,17 +27,16 @@ 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();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
onKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {
|
onKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {
|
||||||
const { onCancel } = this.props;
|
const { onCancel } = this.props;
|
||||||
if (onCancel && event.key === "Escape") {
|
if (onCancel && event.key === "Escape") {
|
||||||
onCancel();
|
onCancel();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { onSubmit } = this.props;
|
||||||
|
if (onSubmit && event.key === "Enter" && (event.ctrlKey || event.metaKey)) {
|
||||||
|
onSubmit();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -57,7 +56,6 @@ class Textarea extends React.Component<Props> {
|
|||||||
onChange={this.handleInput}
|
onChange={this.handleInput}
|
||||||
value={value}
|
value={value}
|
||||||
disabled={!!disabled}
|
disabled={!!disabled}
|
||||||
onKeyPress={this.onKeyPress}
|
|
||||||
onKeyDown={this.onKeyDown}
|
onKeyDown={this.onKeyDown}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user