session-desktop/ts/components/session/SessionIdEditable.tsx

63 lines
1.5 KiB
TypeScript
Raw Normal View History

import React from 'react';
import classNames from 'classnames';
interface Props {
placeholder?: string;
text?: string;
editable?: boolean;
onChange?: any;
2020-01-29 04:41:12 +01:00
onPressEnter?: any;
}
export class SessionIdEditable extends React.PureComponent<Props> {
2020-01-29 04:41:12 +01:00
private readonly inputRef: any;
2020-01-28 06:39:12 +01:00
public constructor(props: Props) {
super(props);
this.inputRef = React.createRef();
2020-01-29 04:41:12 +01:00
this.handleChange = this.handleChange.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
}
2020-01-28 06:39:12 +01:00
public focus() {
if (this.inputRef.current) {
this.inputRef.current.focus();
}
}
public render() {
2020-01-29 04:41:12 +01:00
const { placeholder, editable, text } = this.props;
return (
<div className={classNames('session-id-editable', !editable && 'session-id-editable-disabled')}>
2020-01-30 00:55:28 +01:00
<textarea
className="session-id-editable-textarea"
2020-01-30 00:55:28 +01:00
ref={this.inputRef}
placeholder={placeholder}
disabled={!editable}
spellCheck={false}
onKeyDown={this.handleKeyDown}
onChange={this.handleChange}
value={text}
/>
</div>
);
}
2020-01-29 04:41:12 +01:00
private handleChange(e: any) {
const { editable, onChange } = this.props;
if (editable) {
onChange(e.target.value);
}
}
private handleKeyDown(e: any) {
const { editable, onPressEnter } = this.props;
if (editable && e.keyCode === 13) {
e.preventDefault();
// tslint:disable-next-line: no-unused-expression
onPressEnter && onPressEnter();
}
}
}