2020-12-01 14:00:13 +01:00
|
|
|
import "./TextInput.scss";
|
|
|
|
|
|
|
|
import React, { Component } from "react";
|
2021-04-13 01:29:25 +05:30
|
|
|
import { focusNearestParent } from "../utils";
|
2020-12-01 14:00:13 +01:00
|
|
|
|
2021-05-25 21:37:14 +02:00
|
|
|
import "./ProjectName.scss";
|
|
|
|
|
2020-12-01 14:00:13 +01:00
|
|
|
type Props = {
|
|
|
|
value: string;
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
label: string;
|
2021-03-20 16:08:03 +05:30
|
|
|
isNameEditable: boolean;
|
2020-12-01 14:00:13 +01:00
|
|
|
};
|
|
|
|
|
2021-03-20 21:57:58 +05:30
|
|
|
type State = {
|
|
|
|
fileName: string;
|
|
|
|
};
|
|
|
|
export class ProjectName extends Component<Props, State> {
|
|
|
|
state = {
|
|
|
|
fileName: this.props.value,
|
2020-12-01 14:00:13 +01:00
|
|
|
};
|
2021-03-20 21:57:58 +05:30
|
|
|
private handleBlur = (event: any) => {
|
2021-04-13 01:29:25 +05:30
|
|
|
focusNearestParent(event.target);
|
2021-03-20 21:57:58 +05:30
|
|
|
const value = event.target.value;
|
2020-12-01 14:00:13 +01:00
|
|
|
if (value !== this.props.value) {
|
|
|
|
this.props.onChange(value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
event.preventDefault();
|
|
|
|
if (event.nativeEvent.isComposing || event.keyCode === 229) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.currentTarget.blur();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public render() {
|
2021-03-20 21:57:58 +05:30
|
|
|
return (
|
2021-05-25 21:37:14 +02:00
|
|
|
<div className="ProjectName">
|
|
|
|
<label className="ProjectName-label" htmlFor="filename">
|
2021-03-20 21:57:58 +05:30
|
|
|
{`${this.props.label}${this.props.isNameEditable ? "" : ":"}`}
|
|
|
|
</label>
|
|
|
|
{this.props.isNameEditable ? (
|
|
|
|
<input
|
|
|
|
className="TextInput"
|
|
|
|
onBlur={this.handleBlur}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
2021-05-25 21:37:14 +02:00
|
|
|
id="filename"
|
2021-03-20 21:57:58 +05:30
|
|
|
value={this.state.fileName}
|
|
|
|
onChange={(event) =>
|
|
|
|
this.setState({ fileName: event.target.value })
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) : (
|
2021-05-25 21:37:14 +02:00
|
|
|
<span className="TextInput TextInput--readonly" id="filename">
|
2021-03-20 21:57:58 +05:30
|
|
|
{this.props.value}
|
|
|
|
</span>
|
|
|
|
)}
|
2021-05-25 21:37:14 +02:00
|
|
|
</div>
|
2020-12-01 14:00:13 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|